0
我正在嘗試編寫一個宏,該文件循環遍歷文件夾m中的所有文件,然後將其打開並查找K列中以「Page」開頭的單詞然後將其轉移到下一個欄,我使用的代碼是通過文件夾循環並將數據移動到下一列的Excel VBA
Public Sub Loop_through_folder_page_no()
'DECLARE AND SET VARIABLES
Dim wbk As Workbook
Dim Filename As String
Dim Path As String
Path = "C:\xlsFolder\"
Filename = Dir(Path & "*.xlsx")
'--------------------------------------------
'OPEN EXCEL FILES
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN
Set wbk = Workbooks.Open(Path & Filename)
Dim K As Range
Dim r As Range
Set K = Intersect(ActiveSheet.UsedRange, Range("K:K"))
For Each r In K
If Left(r.Text, 4) = "Page" Then
r.Copy r.Offset(0, 1)
r.Clear
End If
Next r
ActiveWorkbook.Save
wbk.Close True
Filename = Dir
Loop
End Sub
這段代碼是給我不能弄清楚什麼地方錯了一個錯誤。
你得到了什麼錯誤,並在什麼行? – rryanp
我對它進行了調試,它在以「For Each r In K」開頭的行上給出錯誤, – mb1987