我正在尝试编写一个宏来循环遍历文件夹中的所有文件米然后打开它并查找以“页”在钾列,然后将其移至下一列。我使用的代码是
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 'ERRORS HERE WITH Runtime error 1004 Application-defined or object-defined error
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
此代码出现错误。我不明白它出了什么问题。
答案1
尝试 activesheet.range("K:K") 而不是仅仅 range。您的文件中是否只有一张工作表/您确定选择了所需的工作表吗?请再次检查使用的范围和 K 列的交集是否确实存在。