excelvba打开指定的文件夹
⑴ Excel 如何使用VBA打开指定路径下的文件夹
将当前文件夹设为默认即可。
在你的语句前面加上下面两句:
ChDir ThisWorkbook.Path
ChDrive ThisWorkbook.Path
⑵ 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)
⑸ VBA 怎么打开当前文件夹下的excel
你只要运行语句,然后找到你那个文件夹,Office下次再打开是会自动记录位置的,直接定位到那个文件夹。
而且要指出的是:
GetOpenFilename 方法:显示标准的“打开”对话框,并获取用户文件名,而没有真正打开任何文件。
要真正打开文件要使用:Workbook的Open方法
xlapp.Workbooks.Open (Filename)
注意:其中Filename参数需要指定文件的全路径。