Attribute VB_Name = "模块1"Option Explicit,我来为大家科普一下关于vba保存excel文件当前路径?以下内容希望对你有帮助!

vba保存excel文件当前路径(导出Excel文件中的所以vba模块到文本文件)

vba保存excel文件当前路径

Attribute VB_Name = "模块1"

Option Explicit

Sub 逐个打开文件()

Excel.Application.DisplayAlerts = False

Excel.Application.ScreenUpdating = False

Dim i, str, path, s1, namee

path = InputBox("请输入路径:")

If path = "" Then

Exit Sub

End If

path = path & "\"

namee = Dir(path)

Set s1 = ThisWorkbook

s1.Sheets(1).Activate

i = 1

Do While namee <> ""

Workbooks.Open (path & namee)

Call 导出代码到txt里

Workbooks(ActiveWorkbook.Name).Close

namee = Dir

i = i 1

Loop

Excel.Application.DisplayAlerts = True

Excel.Application.ScreenUpdating = True

MsgBox "代码导出完成"

End Sub

'列出模块名称和代码

Sub 导出代码到txt里()

Dim nname, str, i, codemod

Dim lastrow As Integer

str = ActiveWorkbook.Name

For Each codemod In ActiveWorkbook.VBProject.VBComponents

lastrow = codemod.CodeModule.CountOfLines

nname = "C:\Users\Administrator\Desktop\代码导出\" & Mid(str, 1, InStr(str, ".xl")) & ".txt" '要保存文件的位置

If lastrow > 2 Then

Open nname For Output As #1 '以读写方式打开文件,每次写内容都会覆盖原先的内容

Print #1, codemod.CodeModule.Lines(1, lastrow) '把数据写到文本文件里

End If

Next

Close #1 '关闭文件

End Sub

,