簡體   English   中英

Excel VBA:對象不支持此屬性或方法

[英]Excel VBA: Object doesn't support this property or method

嘗試運行以下代碼時,出現標題中所述的錯誤消息。 到目前為止,所有通過故障排除和在stackoverflow中搜索其他此錯誤來解決此問題的嘗試均未成功。 相反,我很困惑,不確定如何在不了解VBA的情況下進一步解決此問題(我目前還不了解,希望很快會得到解決。途中有一些書)

為了提供此代碼的上下文,我試圖獲取通過將數據從源工作簿(此代碼位於其中)中的行傳輸到另一個設計為報表的工作簿中的特定單元格而創建的文檔。 這將需要在源工作簿中的所有行中循環執行。

任何幫助是極大的贊賞。

Sub Transfer()

  Dim sourceDataWb As Workbook
  Dim destinationDataWb As Workbook
  Dim strpath As String
  Dim strfolderpath As String
  Dim numberOfRows As Long, z As Long

  On Error GoTo error_catch

  Application.ScreenUpdating = False
  Application.DisplayAlerts = False

  'Assign active workbook (this one!) to sourcedatawb
  Set sourceDataWb = ActiveWorkbook

    numberOfRows = sourceDataWb.Range("A1", Range("A1").End(xlDown)).Rows.Count

  For z = 1 To numberOfRows
    ' Open template workbook & assign to destinationdatawb
    Set destinationDataWb = Workbooks.Open("C:\Users\Matthew.Banks\Desktop\client data\output template.xlsx")
    ' Transfer data to output template
    destinationDataWb.Sheets("Inhibit Sheet").Range(C9).Value = sourceDataWb.Sheets("data").Cells(z, 1).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C7).Value = sourceDataWb.Sheets("data").Cells(z, 2).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C8).Value = sourceDataWb.Sheets("data").Cells(z, 3).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(F7).Value = sourceDataWb.Sheets("data").Cells(z, 4).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(F8).Value = sourceDataWb.Sheets("data").Cells(z, 5).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(F9).Value = sourceDataWb.Sheets("data").Cells(z, 6).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C11).Value = sourceDataWb.Sheets("data").Cells(z, 7).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C10).Value = sourceDataWb.Sheets("data").Cells(z, 8).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C21).Value = sourceDataWb.Sheets("data").Cells(z, 9).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C22).Value = sourceDataWb.Sheets("data").Cells(z, 10).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 11).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 12).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 13).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 14).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 15).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 16).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 17).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 18).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 19).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 20).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 21).Value
    destinationDataWb.Sheets("Inhibit Sheet").Range(C23).Value = sourceDataWb.Sheets("data").Cells(z, 22).Value

    ' CREATE THE PATH
    strpath = "C:\" & destinationDataWb.Sheets("Inhibit Sheet").Range("A1").Value & " Report" & ".xlsx"
    ' SAVE
    destinationDataWb.SaveAs Filename:=strpath
    destinationDataWb.Close
    'REPEAT
  Next

  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
  Exit Sub

error_catch:
  MsgBox "Error: " & Err.Description
  Err.Clear
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

范圍對象是工作表的子級,但是您將其用作工作簿的子級。 要使其正常工作,您可以更改以下行:

numberOfRows = sourceDataWb.Range("A1", Range("A1").End(xlDown)).Rows.Count

對此:

numberOfRows = application.ActiveSheet.Range("A1", Range("A1").End(xlDown)).Rows.Count

我們不能直接使用已選擇的范圍值的工作簿,而是可以通過使用工作表屬性按以下方式使用。

numberOfRows = sourceDataWb.Worksheets("Sheet1").Range("A1", Range("A1").End(xlDown)).Rows.Count

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM