Home » excel » Excel VBA: Object doesn't support this property or method

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

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m getting the error message stated in the title when attempting to run the following code. All attempts to fixing this issue myself via troubleshooting and searching for other this error in stackoverflow have so far not worked. Rather stumped and unsure how I could troubleshoot this further without a greater knowledge of VBA (which I currently do not have, hopefully soon to be fixed. Got some books on the way)

To give some context for this code, I am trying to get documents created by transferring data from rows in the source workbook (where this code sits) to specific cells in another workbook designed as a report. This will need to be looped for all rows in the source workbook.

Any help is greatly appreciated.

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
How to&Answers:

We cannot use workbook directly with range value selected as you mentioned, instead you can use as below by using sheet property.

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

Answer:

Range object is a child of a Worksheet but you are using it as a child of a Workbook.
To make it work, you can change this line:

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

to this:

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