我正在嘗試創建一個在一堆xlsm工作簿上運行命令的宏。這些工作簿以數字(例如11.xlsm,12.xlsm,13.xlsm等)命名,我希望宏在每個工作簿的工作表(「1」)上使用循環運行簡單的命令。該命令僅包括清除單元格的內容和寫入取決於工作簿名稱的內容。以循環文件名爲條件的命令
我已經給它去了,但沒有成功。此外,我收到以下錯誤「對象不支持此屬性或方法」。
您的幫助將不勝感激。
Sub IDCell()
Dim wbpath As String
Dim wbnames() As String
Dim ext As String
Dim wbTarget As Workbook
Dim ws As Worksheet
Dim rgID As Range
ext = ".xlsm"
wbpath = "Z:\\User\Documents"
wbnames() = Split("11,12,13", ",")
Dim i As Integer
For i = 0 To UBound(wbnames)
Set wbTarget = Workbooks.Open(wbpath & "\" & wbnames(i) & ext)
For x = 1 To 1
If wbTarget = "11.xslm" Then
Set ws = Worksheets(CStr(x))
ws.Activate
Set rgID = ws.Range("K4:K4")
rgID.ClearContents
rgID.Value = "XXXX"
End If
If wbTarget = "12.xslm" Then
Set ws = Worksheets(CStr(x))
ws.Activate
Set rgID = ws.Range("K4:K4")
rgID.ClearContents
rgID.Value = "YYYY"
End If
If wbTarget = "13.xlsm" Then
Set ws = Worksheets(CStr(x))
ws.Activate
Set rgID = ws.Range("K4:K4")
rgID.ClearContents
rgID.Value = "ZZZZ"
End If
Next
wbTarget.Save
wbTarget.Close
Next i
End Sub
如果您調試代碼,你看到的是上行時發生的錯誤。如果wbTarget =「11 .xslm「然後... wbTarget什麼是?!?!?嘗試debug.print wbTarget並嘗試debug.print wbTarget.name。你會發現wbTarget.name會和你的字符串相媲美 – Fabrizio