2016-08-19 41 views
0

我正在嘗試創建一個在一堆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 
+0

如果您調試代碼,你看到的是上行時發生的錯誤。如果wbTarget =「11 .xslm「然後... wbTarget什麼是?!?!?嘗試debug.print wbTarget並嘗試debug.print wbTarget.name。你會發現wbTarget.name會和你的字符串相媲美 – Fabrizio

回答

0

你的問題,你需要wbTarget.Name代替wbTarget,但站在你的敘述,你可以:

  • 避免遍歷X

  • 避免IF-THEN-ELSE

喜歡如下:

Option Explicit 

Sub IDCell() 
    Dim wbPath As String, ext As String 
    Dim wbNames As Variant 
    Dim wbValues As Variant 
    Dim i As Long 

    ext = ".xlsm" 
    wbpath = "Z:\\User\Documents" 
    wbNames = Array(11, 12, 13) '<--| set an array of your workbook names 
    wbValues = Array("XXXX", "YYYY", "ZZZZ") '<--| set an array of texta to be written in each corresponding workbook 

    For i = 0 To UBound(wbNames) '<--| loop over workbook names 
     With Workbooks.Open(wbPath & "\" & wbNames(i) & ext).Worksheets("1").Range("K4") '<--| open workbook and assume as reference the range "K4" of its "1" worksheet 
      .ClearContents 
      .Value = wbValues(i) 
     End With 
     ActiveWorkbook.Close True '<--| close active workbook and save it 
    Next i 
End Sub 
0

可以縮短塊:

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.Sheets("1").Range("K4").value = iif(wbnames(i)="11","XXXX",iif(wbnames(i)="12","YYYYY","ZZZZZ")) 
+0

如果你需要更多的選擇,你應該使用'Select case wbTarget.name','Case「11.xlsm'等。 – Jochen

+0

感謝您的輸入。 – korone