excelvba打开指定文件夹
❶ vba怎样打开指定路径的excel文件
打开文件如下:Workbooks.Open ("C:\对账单\对帐单.xlsx")这里你可以把“C:\对账单\”换成你的绝对路径,你可以用:a = ThisWorkbook.Patha = ThisWorkbook.PathWorkbooks.Open ("" & a & "&" \ "&对帐单.xlsx")来获取你当前表的绝对路径,然后再打开,因为不知道你的具体情况,如果有问题可以hai我 如果当前文件夹下还有A文件夹可以用代码:Workbooks.Open ("" & a & "&" \ A\"&b.xlsx")Microsoft Excel是Microsoft为使用Windows和Apple Macintosh操作系统的电脑编写的一款电子表格软件。直观的界面、出色的计算功能和图表工具,再加上成功的市场营销,使Excel成为最流行的个人计算机数据处理软件。在1993年,作为Microsoft Office的组件发布了5.0版之后,Excel就开始成为所适用操作平台上的电子制表软件的霸主。
❷ EXCEL vba 读取指定文件夹的名字和循环打开文件夹
'------------------------------------------------------------------------------
'
'FormCode
'
'------------------------------------------------------------------------------
OptionExplicit
PrivaterowAsInteger,colAsInteger
PrivateSubCloseWindows_Click()
IfTextStartRow.Text=""ThenTextStartRow=0
IfTextStartCol=""ThenTextStartCol=0
IfTextPath=""ThenTextPath="D:"
CloseMyDialogTextStartRow,TextStartCol
EndSub
PrivateSubGetDir_Click()
IfTextStartRow.Text=""ThenTextStartRow=0
IfTextStartCol=""ThenTextStartCol=0
IfTextPath=""Then
TextPath="D:"
ElseIfRight(TextPath,1)<>""Then
TextPath=TextPath&""
EndIf
doGetDirTextPath,Val(TextStartRow),Val(TextStartCol)
EndSub
PrivateSubShowWindows_Click()
IfTextStartRow.Text=""ThenTextStartRow=0
IfTextStartCol=""ThenTextStartCol=0
IfTextPath=""ThenTextPath="D:"
ShowMyDialogApplication.hWnd,TextStartRow,TextStartCol
EndSub
上面是Form上面的
OptionExplicit
DimMyFile,Mypath,MyName
Dimi%,j%
DimDirPath()AsString
SubGetDir(ByValMypathAsString,rowAsInteger,colAsInteger)
'显示C:目录下的名称。
'MyPath="d:电大"'指定路径。
MyName=Dir(Mypath,vbDirectory)'找寻第一项。
DoWhileMyName<>""'开始循环。
'跳过当前的目录及上层目录。
IfMyName<>"."AndMyName<>".."Then
'使用位比较来确定MyName代表一目录。
If(GetAttr(Mypath&MyName)AndvbDirectory)=vbDirectoryThen
Cells(row+i,col)=Mypath&MyName'如果它是一个目录,将其名称显示出来。
ReDimPreserveDirPath(i)
DirPath(i)=Mypath&MyName&""
i=i+1
EndIf
EndIf
MyName=Dir'查找下一个目录。
Loop
EndSub
PublicSubdoGetDir(ByValTextPath$,ByValTextStartRow%,ByValTextStartCol%)
j=1
i=1
Mypath=TextPath
GetDirMypath,TextStartRow,TextStartCol
Forj=1Toi-1
GetDirDirPath(j),TextStartRow,TextStartCol
Next
EndSub
'endcode---------------------------------------------------
OptionExplicit
PublicConstOFN_ALLOWMULTISELECTAsLong=&H200
PublicConstOFN_CREATEPROMPTAsLong=&H2000
PublicConstOFN_ENABLEHOOKAsLong=&H20
PublicConstOFN_ENABLETEMPLATEAsLong=&H40
PublicConstOFN_ENABLETEMPLATEHANDLEAsLong=&H80
PublicConstOFN_EXPLORERAsLong=&H80000
PublicConstOFN_EXTENSIONDIFFERENTAsLong=&H400
PublicConstOFN_FILEMUSTEXISTAsLong=&H1000
PublicConstOFN_HIDEREADONLYAsLong=&H4
PublicConstOFN_LONGNAMESAsLong=&H200000
PublicConstOFN_NOCHANGEDIRAsLong=&H8
PublicConstOFN_NODEREFERENCELINKSAsLong=&H100000
PublicConstOFN_NOLONGNAMESAsLong=&H40000
PublicConstOFN_NONETWORKBUTTONAsLong=&H20000
PublicConstOFN_NOREADONLYRETURNAsLong=&H8000&'*seecomments
PublicConstOFN_NOTESTFILECREATEAsLong=&H10000
PublicConstOFN_NOVALIDATEAsLong=&H100
PublicConstOFN_OVERWRITEPROMPTAsLong=&H2
PublicConstOFN_PATHMUSTEXISTAsLong=&H800
PublicConstOFN_READONLYAsLong=&H1
PublicConstOFN_SHAREAWAREAsLong=&H4000
PublicConstOFN_SHAREFALLTHROUGHAsLong=2
PublicConstOFN_SHAREWARNAsLong=0
PublicConstOFN_SHARENOWARNAsLong=1
PublicConstOFN_SHOWHELPAsLong=&H10
PublicConstOFS_MAXPATHNAMEAsLong=260
PublicConstOFS_FILE_OPEN_FLAGS=OFN_EXPLORER_
OrOFN_LONGNAMES_
OrOFN_CREATEPROMPT_
OrOFN_NODEREFERENCELINKS
PublicConstOFS_FILE_SAVE_FLAGS=OFN_EXPLORER_
OrOFN_LONGNAMES_
OrOFN_OVERWRITEPROMPT_
OrOFN_HIDEREADONLY
PublicTypeOPENFILENAME
nStructSizeAsLong
hWndOwnerAsLong
hInstanceAsLong
sFilterAsString
sCustomFilterAsString
nMaxCustFilterAsLong
nFilterIndexAsLong
sFileAsString
nMaxFileAsLong
sFileTitleAsString
nMaxTitleAsLong
sInitialDirAsString
sDialogTitleAsString
flagsAsLong
nFileOffsetAsInteger
nFileExtensionAsInteger
sDefFileExtAsString
nCustDataAsLong
fnHookAsLong
sTemplateNameAsString
EndType
PublicOFNAsOPENFILENAME
PublicConstWM_CLOSE=&H10
"comdlg32"_
Alias"GetOpenFileNameA"_
(pOpenfilenameAsOPENFILENAME)AsLong
"comdlg32"_
Alias"GetSaveFileNameA"_
(pOpenfilenameAsOPENFILENAME)AsLong
"kernel32"_
Alias"GetShortPathNameA"_
(ByVallpszLongPathAsString,_
ByVallpszShortPathAsString,_
ByValcchBufferAsLong)AsLong
PublicConstWM_INITDIALOG=&H110
PrivateConstSW_SHOWNORMAL=1
PublicTypeRECT
LeftAsLong
TopAsLong
RightAsLong
BottomAsLong
EndType
"user32"_
(ByValhWndAsLong)AsLong
"user32"_
Alias"SetWindowTextA"_
(ByValhWndAsLong,_
ByVallpStringAsString)AsLong
"user32"_
(ByValhWndAsLong,_
ByValxAsLong,_
ByValyAsLong,_
ByValnWidthAsLong,_
ByValnHeightAsLong,_
ByValbRepaintAsLong)AsLong
"user32"_
(ByValhWndAsLong,_
lpRectAsRECT)AsLong
"user32"_
Alias"SendMessageA"_
(ByValhWndAsLong,_
ByValwMsgAsLong,_
ByValwParamAsLong,_
lParamAsAny)AsLong
"user32"_
Alias"FindWindowA"_
(ByVallpClassNameAsLong,_
ByVallpWindowNameAsString)AsLong
PublicFunctionFARPROC(ByValpfnAsLong)AsLong
FARPROC=pfn
EndFunction
PublicFunctionOFNHookProc(ByValhWndAsLong,_
ByValuMsgAsLong,_
ByValwParamAsLong,_
ByVallParamAsLong)AsLong
DimhwndParentAsLong
DimrcAsRECT
DimnewLeftAsLong
DimnewTopAsLong
DimdlgWidthAsLong
DimdlgHeightAsLong
DimscrWidthAsLong
DimscrHeightAsLong
SelectCaseuMsg
CaseWM_INITDIALOG
hwndParent=GetParent(hWnd)
IfhwndParent<>0Then
CallGetWindowRect(hwndParent,rc)
dlgWidth=rc.Right-rc.Left
dlgHeight=rc.Bottom-rc.Top
CallMoveWindow(hwndParent,newLeft,newTop,dlgWidth,dlgHeight,True)
OFNHookProc=1
EndIf
CaseElse:
EndSelect
EndFunction
PublicSubShowFolder(hWndAsLong,Mypath$)
DimsFiltersAsString
DimposAsLong
DimbuffAsString
DimsLongnameAsString
DimsShortnameAsString
WithOFN
.nStructSize=Len(OFN)
.hWndOwner=hWnd
.sFilter=sFilters
.nFilterIndex=2
.sFile=Space$(1024)&vbNullChar&vbNullChar
.nMaxFile=Len(.sFile)
.sDefFileExt="bas"&vbNullChar&vbNullChar
.sFileTitle=vbNullChar&Space$(512)&vbNullChar&vbNullChar
.nMaxTitle=Len(OFN.sFileTitle)
.sInitialDir=Mypath&vbNullChar&vbNullChar
.sDialogTitle=Mypath&vbNullChar&vbNullChar
.flags=OFS_FILE_OPEN_FLAGSOr_
OFN_ALLOWMULTISELECTOr_
OFN_EXPLOREROr_
OFN_ENABLEHOOK
.fnHook=FARPROC(AddressOfOFNHookProc)
EndWith
GetOpenFileNameOFN
EndSub
PublicSubCloseFolder(MypathAsString)
DimhWndAsLong
hWnd=FindWindow(0,Mypath)
CallSendMessage(hWnd,WM_CLOSE,0&,ByVal0&)
EndSub
PublicSubShowMyDialog(MyhWndAsLong,TextStartRowAsInteger,TextStartColAsInteger)
Dimrow,col
Dimi
DimhWndAsLong
hWnd=MyhWnd
i=1:row=TextStartRow:col=TextStartCol
DoWhileCells(i+row,col)<>""
Shell"C:Windowsexplorer.exe"&Cells(i+row,col)
'ShowFolderhWnd,Cells(i+row,col)
'hWnd=FindWindow(0,Cells(i+row,col))
i=i+1
Loop
EndSub
PublicSubCloseMyDialog(TextStartRowAsInteger,TextStartColAsInteger)
Dimrow,col
Dimi
i=1:row=TextStartRow:col=TextStartCol
DoWhileCells(i+row,col)<>""
CloseFolderpathToName(Cells(i+row,col))
i=i+1
Loop
EndSub
PrivateFunctionpathToName(Mypath$)AsString
Dimstr()AsString
str=Split(Mypath,"")
pathToName=str(UBound(str))
EndFunction
❸ 如何在Excel中用VBA打开指定的文件夹
打开E盘
Shell "explorer.exe " & "E:\", vbNormalFocus
❹ excel运用VBA打开文件夹并搜索打开文件:
wb=range("B1")&""&range("C5")&".xls"
workbooks.open(wb)
❺ 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")
❻ 用EXCEL VBA获取指定目录下的文件名(包括文件夹名)
1.点开始菜单,就能看到上面的界面,选择“运行”!点了运行程序后,在里面输入“cmd”点击确定会进入命令提示符串口。
❼ Excel 如何使用VBA打开指定路径下的文件夹
将当前文件夹设为默认即可。
在你的语句前面加上下面两句:
ChDir ThisWorkbook.Path
ChDrive ThisWorkbook.Path
❽ EXCEL2010 vba 循环打开某些文件夹下的excel文件
就是遍历文件夹嘛,关键代码如下
在工程-引用里添加 microsoft scripting run time
然后用下面的两个函数递归遍历 就可以了
sub main()
Dim objFSO As Object
Dim objTemplateFolder As Object
Set objFSO = New Scripting.FileSystemObject
Set objTemplateFolder = objFSO.GetFolder(“D:\”)
getFiles objTemplateFolder
end sub
Sub getFiles(ByRef theFolder As Object)
Dim folder As Object
Dim c As New Scripting.FileSystemObject
‘此处执行你的操作:打开关闭文件夹,取名字等
For Each folder In theFolder.subFolders
getFiles folder ’递归遍历子文件夹
Next
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打开相对路径下的文件或文件夹急!!!
打开文件如下:
Workbooks.Open ("C:对账单对帐单.xlsx")
这里你可以把“C:对账单”换成你的绝对路径,
你可以用:a = ThisWorkbook.Path
a = ThisWorkbook.Path
Workbooks.Open ("" & a & "&" "&对帐单.xlsx")
来获取你当前表的绝对路径,然后再打开,因为不知道你的具体情况,如果有问题可以hai我
如果当前文件夹下还有A文件夹可以用代码:
Workbooks.Open ("" & a & "&" A"&b.xlsx")