EXCEL vba
(1)读取指定文件夹下所有文件夹的名字,并储存在指定的excel表格中。
(2)根据储存在excel表格中的文件夹名字顺序,打开每个文件夹,然后再关闭该文件夹,如此循环,直到依次打开和关闭指定文件夹所有文件夹位置。
'------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------
Option Explicit
Private row As Integer, col As Integer
Private Sub CloseWindows_Click()
If TextStartRow.Text = "" Then TextStartRow = 0
If TextStartCol = "" Then TextStartCol = 0
If TextPath = "" Then TextPath = "D:\"
CloseMyDialog TextStartRow, TextStartCol
End Sub
Private Sub GetDir_Click()
If TextStartRow.Text = "" Then TextStartRow = 0
If TextStartCol = "" Then TextStartCol = 0
If TextPath = "" Then
TextPath = "D:\"
ElseIf Right(TextPath, 1) <> "\" Then
TextPath = TextPath & "\"
End If
doGetDir TextPath, Val(TextStartRow), Val(TextStartCol)
End Sub
Private Sub ShowWindows_Click()
If TextStartRow.Text = "" Then TextStartRow = 0
If TextStartCol = "" Then TextStartCol = 0
If TextPath = "" Then TextPath = "D:\"
ShowMyDialog Application.hWnd, TextStartRow, TextStartCol
End Sub
上面是Form上面的
Option Explicit
Dim MyFile, Mypath, MyName
Dim i%, j%
Dim DirPath() As String
Sub GetDir(ByVal Mypath As String, row As Integer, col As Integer)
' 显示 C:\ 目录下的名称。
' MyPath = "d:\电大\" ' 指定路径。
MyName = Dir(Mypath, vbDirectory) ' 找寻第一项。
Do While MyName <> "" ' 开始循环。
' 跳过当前的目录及上层目录。
If MyName <> "." And MyName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(Mypath & MyName) And vbDirectory) = vbDirectory Then
Cells(row + i, col) = Mypath & MyName ' 如果它是一个目录,将其名称显示出来。
ReDim Preserve DirPath(i)
DirPath(i) = Mypath & MyName & "\"
i = i + 1
End If
End If
MyName = Dir ' 查找下一个目录。
Loop
End Sub
Public Sub doGetDir(ByVal TextPath$, ByVal TextStartRow%, ByVal TextStartCol%)
j = 1
i = 1
Mypath = TextPath
GetDir Mypath, TextStartRow, TextStartCol
For j = 1 To i - 1
GetDir DirPath(j), TextStartRow, TextStartCol
Next
End Sub
'end code---------------------------------------------------
Option Explicit
Public Const OFN_ALLOWMULTISELECT As Long = &H200
Public Const OFN_CREATEPROMPT As Long = &H2000
Public Const OFN_ENABLEHOOK As Long = &H20
Public Const OFN_ENABLETEMPLATE As Long = &H40
Public Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
Public Const OFN_EXPLORER As Long = &H80000
Public Const OFN_EXTENSIONDIFFERENT As Long = &H400
Public Const OFN_FILEMUSTEXIST As Long = &H1000
Public Const OFN_HIDEREADONLY As Long = &H4
Public Const OFN_LONGNAMES As Long = &H200000
Public Const OFN_NOCHANGEDIR As Long = &H8
Public Const OFN_NODEREFERENCELINKS As Long = &H100000
Public Const OFN_NOLONGNAMES As Long = &H40000
Public Const OFN_NONETWORKBUTTON As Long = &H20000
Public Const OFN_NOREADONLYRETURN As Long = &H8000& '*see comments
Public Const OFN_NOTESTFILECREATE As Long = &H10000
Public Const OFN_NOVALIDATE As Long = &H100
Public Const OFN_OVERWRITEPROMPT As Long = &H2
Public Const OFN_PATHMUSTEXIST As Long = &H800
Public Const OFN_READONLY As Long = &H1
Public Const OFN_SHAREAWARE As Long = &H4000
Public Const OFN_SHAREFALLTHROUGH As Long = 2
Public Const OFN_SHAREWARN As Long = 0
Public Const OFN_SHARENOWARN As Long = 1
Public Const OFN_SHOWHELP As Long = &H10
Public Const OFS_MAXPATHNAME As Long = 260
Public Const OFS_FILE_OPEN_FLAGS = OFN_EXPLORER _
Or OFN_LONGNAMES _
Or OFN_CREATEPROMPT _
Or OFN_NODEREFERENCELINKS
Public Const OFS_FILE_SAVE_FLAGS = OFN_EXPLORER _
Or OFN_LONGNAMES _
Or OFN_OVERWRITEPROMPT _
Or OFN_HIDEREADONLY
Public Type OPENFILENAME
nStructSize As Long
hWndOwner As Long
hInstance As Long
sFilter As String
sCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
sFile As String
nMaxFile As Long
sFileTitle As String
nMaxTitle As Long
sInitialDir As String
sDialogTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
sDefFileExt As String
nCustData As Long
fnHook As Long
sTemplateName As String
End Type
Public OFN As OPENFILENAME
Public Const WM_CLOSE = &H10
Public Declare Function GetOpenFileName Lib "comdlg32" _
Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32" _
Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long
Public Const WM_INITDIALOG = &H110
Private Const SW_SHOWNORMAL = 1
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function GetParent Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare Function SetWindowText Lib "user32" _
Alias "SetWindowTextA" _
(ByVal hWnd As Long, _
ByVal lpString As String) As Long
Public Declare Function MoveWindow Lib "user32" _
(ByVal hWnd As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long
Public Declare Function GetWindowRect Lib "user32" _
(ByVal hWnd As Long, _
lpRect As RECT) As Long
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As Long, _
ByVal lpWindowName As String) As Long
Public Function FARPROC(ByVal pfn As Long) As Long
FARPROC = pfn
End Function
Public Function OFNHookProc(ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim hwndParent As Long
Dim rc As RECT
Dim newLeft As Long
Dim newTop As Long
Dim dlgWidth As Long
Dim dlgHeight As Long
Dim scrWidth As Long
Dim scrHeight As Long
Select Case uMsg
Case WM_INITDIALOG
hwndParent = GetParent(hWnd)
If hwndParent <> 0 Then
Call GetWindowRect(hwndParent, rc)
dlgWidth = rc.Right - rc.Left
dlgHeight = rc.Bottom - rc.Top
Call MoveWindow(hwndParent, newLeft, newTop, dlgWidth, dlgHeight, True)
OFNHookProc = 1
End If
Case Else:
End Select
End Function
Public Sub ShowFolder(hWnd As Long, Mypath$)
Dim sFilters As String
Dim pos As Long
Dim buff As String
Dim sLongname As String
Dim sShortname As String
With OFN
.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_FLAGS Or _
OFN_ALLOWMULTISELECT Or _
OFN_EXPLORER Or _
OFN_ENABLEHOOK
.fnHook = FARPROC(AddressOf OFNHookProc)
End With
GetOpenFileName OFN
End Sub
Public Sub CloseFolder(Mypath As String)
Dim hWnd As Long
hWnd = FindWindow(0, Mypath)
Call SendMessage(hWnd, WM_CLOSE, 0&, ByVal 0&)
End Sub
Public Sub ShowMyDialog(MyhWnd As Long, TextStartRow As Integer, TextStartCol As Integer)
Dim row, col
Dim i
Dim hWnd As Long
hWnd = MyhWnd
i = 1: row = TextStartRow: col = TextStartCol
Do While Cells(i + row, col) <> ""
Shell "C:\Windows\explorer.exe " & Cells(i + row, col)
' ShowFolder hWnd, Cells(i + row, col)
' hWnd = FindWindow(0, Cells(i + row, col))
i = i + 1
Loop
End Sub
Public Sub CloseMyDialog(TextStartRow As Integer, TextStartCol As Integer)
Dim row, col
Dim i
i = 1: row = TextStartRow: col = TextStartCol
Do While Cells(i + row, col) <> ""
CloseFolder pathToName(Cells(i + row, col))
i = i + 1
Loop
End Sub
Private Function pathToName(Mypath$) As String
Dim str() As String
str = Split(Mypath, "\")
pathToName = str(UBound(str))
End Function
EXCEL vba 读取指定文件夹的名字和循环打开文件夹
EXCELvba(1)读取指定文件夹下所有文件夹的名字,并储存在指定的excel表格中。(2)根据储存在excel表格中的文件夹名字顺序,打开每个文件夹,然后再关闭该文件夹,如此循环,直到依... EXCEL vba(1)读取指定文件夹下所有文件夹的名字,并储存在指定的excel表格中。(2)根据储存在excel表格中的文件夹名字顺序,打开每个文件...
Excel VBA和文件夹-1.5遍历文件夹并打开
代码执行的思路是首先定位到指定的文件夹,使用VBA的`dir`函数遍历文件夹中的文件。在循环中,`dir`会返回当前找到的文件名,通过判断非空值来控制循环的进行。为了打开文件,需要将文件夹路径与找到的文件名拼接,形成完整的文件路径。具体代码部分,首先定义文件夹路径,然后使用`do while`循环,每次循环...
用EXCEL VBA获取指定目录下的文件名(包括文件夹名)
1、首先利用快捷键“Windows键+R”打开“运行”窗口 2、接着输入“cmd”并点击“打开”3、比如是需要复制输出c盘下的Progam Data中文件夹的名字 4、在命令窗口中输入“cd c:\\Progam Data”5、点击“回车键”后出现下面情况 6、然后输入“dir \/b>d:21.xls”命令并点击“回车键”7、接着就在D...
如何利用vba依次打开指定文件夹里的所有excel表,进行某种
为了打开文件夹中的所有Excel文件并执行操作,首先需要使用FSO对象或通过DIR循环目录下所有文件。一旦文件被选中,使用VBA的Open方法可以打开Excel工作簿。具体操作步骤如下:Sub 保存你的工作簿()ThisWorkbook.Save 'Save相当于你手工单击保存按钮;这个函数无参数 ThisWorkbook.SaveAs ' 另存为工作簿,把当前...
excel怎样用vba自动提取文件夹内的文件名
excel中用vba实现自动提取文件夹内的文件名的方法如下:1、新建一个vba宏脚本 2、写入如下代码:Function GetFileList(FileSpec As String) As Variant ' Returns an array of filenames that match FileSpec ' If no matching files are found, it returns False Dim FileArray() As Variant ...
Excel VBA和文件夹-1.7通过对话框灵活选定文件夹并打开对应文件
代码操作如下:首先,我们创建一个对话框,让用户直接在VBA中选择所需的文件夹,无需预先指定路径,代码如下:对话框代码部分:这部分代码的作用是打开文件选择对话框:vb Sub OpenFolder()Dim fdlg As FileDialog Set fdlg = Application.FileDialog(msoFileDialogFolderPicker)If fdlg.Show = -1 Then ...
Excel 提取指定目录的文件夹名和文件名怎么实现
可以用VBA来完成 Sub WriteFolderInfo()Dim shApp As Object, Path1 As Object, Path2 As StringSet shApp = CreateObject("Shell.application")Set Path1 = shApp.BrowseForFolder(0, "请选择文件夹", 0, 17)If Path1 Is Nothing Then Exit SubPath2 = IIf(IsError(Path1.Items.Item.Path), ...
如何用excel vba按关键字选择性的遍历文件夹搜索文件?
Excel怎样批量提取文件夹和子文件夹所有文件 怎样批量提取文件夹下文件名
如何用VBA遍历指定目录下的所有子文件夹Excel文件的所有工作表_百度知 ...
功能: 查找指定文件夹含子文件夹内所有文件名或文件夹名(含路径)'函数名: FileAllArr'参数1: Filename 需查找的文件夹名 不含最后的""'参数2: FileFilter 需要过滤的文件名,可省略
excel,怎么用vba写段关于查找指定文件夹内的文件名,并将其提取值至表...
Alt+F11插入下面的代码到模块 '查找某个文件是否存在 Function IsExistFile(ByRef strDir As String, ByRef fileName As String)Dim s As String If (Right(strDir, 1) <> "\\") Then strDir = strDir & "\\"End If s = dir(strDir & fileName, vbArchive + vbDirectory + vbHidden + ...