當前位置:首頁 » 文件管理 » excelvba打開指定的文件夾

excelvba打開指定的文件夾

發布時間: 2023-07-26 06:19:20

⑴ 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參數需要指定文件的全路徑。

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:433
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:744
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:147
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:240
java駝峰 發布:2025-02-02 09:13:26 瀏覽:652
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726