在excel中如何使用vba

VBA算得上是Excel的頂級應用技術了 , 使用VBA幾乎可以解決99.99%的Excel應用問題 , 本問題也不例外 。通過VBA遍歷同一路徑下的所有文檔 , 查找文檔中的的特定內容 , 并把對應值提取出來 。具體圖文演示如下:

在excel中如何使用vba



在excel中如何使用vba



在excel中如何使用vba



在excel中如何使用vba



在excel中如何使用vba


Sub VBA應用()
Dim mypath As String, file As String
Dim wb As Workbook
Dim rng As Range

mypath = ThisWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
file = Dir(mypath & "*.xlsx")
Do While Len(file) > 0
If file <> ThisWorkbook.Name Then
Workbooks.Open mypath & Application.PathSeparator & file, False
Set wb = ActiveWorkbook
Set rng = wb.Sheets(1).UsedRange.Find("測試").Offset(0, 1)
If rng Is Nothing Then
Exit Do
Else
n = n + 1
With ThisWorkbook.Sheets(1)
.Cells(n + 1, 2) = rng.Value
.Cells(n + 1, 3) = Left(file, Len(file) - 5)
End With
End If
file = Dir
wb.Close
【在excel中如何使用vba】End If
Loop
Set wb = Nothing
Application.ScreenUpdating =True
Application.DisplayAlerts = True
End Sub
在excel中如何使用vba


代碼放入測試工作薄VBE編輯界面的模塊中 , 按F5運行即可提取到各文檔中測試對應的值 。歡迎關注@Excel泥瓦匠 , Excel學習 , E路有你!

    推薦閱讀