vba打开目录
① vba打开文件后如何获得打开文件的路径
S=activeworkbook.Path & "\" & activeworkbook.Name
或
S=workbooks("test.xls").Path & "\" & workbooks("test.xls").Name
② excel运用VBA打开文件夹并搜索打开文件:
Dim c As Worksheet, addr As String, cou As Integer, coun As Integer
Dim path As String, str As String, actsh As Worksheet, keyword As String, i As Integer
Dim fil As String
path = InputBox("Please input the folder path of excel file need combine")
'or path="\\svr001\公用文档"
keyword = activecell.value
If InStr(1, path, ":") < 1 And InStr(1, path, "\\") < 1 Then GoTo ne
If Right(path, 1) = "\" Then path = left(path, Len(path) - 1)
If Len(Trim(keyword)) < 1 Then GoTo ne
cou = 0
Application.ScreenUpdating = False
With Application.FileSearch
.LookIn = path
.FileName = "*" & keyword & "*.xls"
If .Execute > 0 Then
coun = .FoundFiles.count
For i = 1 To coun
Workbooks.Open (.FoundFiles(i))
Next i
End If
End With
Range("A1:A2").EntireRow.Delete
Application.ScreenUpdating = True
MsgBox ("Total processed " & cou & " files!")
ne:
End Sub
③ EXCEL如何使用VBA打开相对路径下的文件或文件夹
打开文件如下:
Workbooks.Open ("C:对账单对帐单.xlsx")
这里你可以把“C:对账单”换成你的绝对路径,
你可以用:a = ThisWorkbook.Path
a = ThisWorkbook.Path
Workbooks.Open ("" & a & "&" "&对帐单.xlsx")
来获取你当前表的绝对路径,然后再打开,因为不知道你的具体情况,如果有问题可以hai我
如果当前文件夹下还有A文件夹可以用代码:
Workbooks.Open ("" & a & "&" A"&b.xlsx")
④ VBA中如何打开一个文件夹内的所有EXCEL文件
'这段代码是读取一个文件夹下的所有文件,也可以根据扩展名筛选其它格式的.
'有了文件名,就是打开文件,获得每个文件的SHEET名字.然后写到你想要的地方
Sub
Macro1()
Dim
myDialog
As
FileDialog,
oFile
As
Object,
strName
As
String,
n
As
Integer
Dim
FSO
As
Object,
myFolder
As
Object,
myFiles
As
Object
,Dim
fn$
Set
myDialog
=
Application.FileDialog(msoFileDialogFolderPicker)
n
=
1
With
myDialog
If
.Show
<>
-1
Then
Exit
Sub
Set
FSO
=
CreateObject("Scripting.FileSystemObject")
'这是文件夹选择,点选到你存放文件的那个
Set
myFolder
=
FSO.GetFolder(.InitialFileName)
Set
myFiles
=
myFolder.Files
For
Each
oFile
In
myFiles
strName
=
UCase(oFile.Name)
strName
=
VBA.Right(strName,
3)
If
strName
=
"xls"
Or
strName
=
"XLS"
Then
'这是扩展名选择
'下面就可接着写打开文件读取数据再写入的语句了,如下:
fn
=
myFolder
&
"\"
&
oFile.Name
Workbooks.Open
Filename:=fn
Worksheets(1).Select
'假设你读取SHEET1的数据
RANGE_
=
Range("A2:F50")
'需要数据的区域,自己修改
Windows("外部表格数据自动导入.xls").Activate
'这个是新表的文件名,自己修改下
Worksheets(n).Select
'打开第几个文件就选择SHEET几,如果没有可用ADD代码添加
Range("a2:f5")
=
RANGE_
'写入数据
Workbooks(2).Close
n
=
n
+
1
End
If
Next
End
With
End
Sub
查看原帖>>
⑤ 关于Excel VBA打开文件夹
Sub openmyexcel()
Dim fs, f, i, j, k, f1, f2
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.getfolder("f:\hk")
For Each i In f.subfolders
If InStr(i.Name, "102471") <> 0 Then
Set f1 = fs.getfolder(i.Path)
For Each j In f1.subfolders
If InStr(j.Name, "prem") <> 0 Then
Set f2 = fs.getfolder(j.Path)
For Each k In f2.Files
If InStr(k.Name, "00") <> 0 Then
Workbooks.Open (k.Path)
Set f2 = Nothing
Exit For
End If
Next
Set f1 = Nothing
Exit For
End If
Next
Set f = Nothing
Exit For
End If
Next
End Sub
————————————————————————————
在thisworkbook中执行
⑥ EXCEL vba 打开上级目录文件
Sub test()
p = ThisWorkbook.Path
p = Mid(p, 1, InStrRev(p, "\"))
Debug.Print p
End Sub
⑦ vba 打开指定路径的文件
打开文件一般是用Open语句的
Open "C:\1.txt" for input As #1
........
Close #1
⑧ VBA 怎么打开当前文件夹下的excel
你只要运行语句,抄然后找到你那个文袭件夹,Office下次再打开是会自动记录位置的,直接定位到那个文件夹。
而且要指出的是:
GetOpenFilename 方法:显示标准的“打开”对话框,并获取用户文件名,而没有真正打开任何文件。
要真正打开文件要使用:Workbook的Open方法
xlapp.Workbooks.Open (Filename)
注意:其中Filename参数需要指定文件的全路径。