Search code examples
excelvbacopy-pasteworksheet

In reference to "Copy a row in excel if it matches a specific criteria into a new worksheet"


In reference to: Copy a row in excel if it matches a specific criteria into a new worksheet

I attempted applying the above hyperlink code to the needs of my own workbook. The only notable differences are: Object names, My data begins in "A2" instead of "A1", and my data is being copied to "L" column in a new worksheet instead of "A" column

Also... you can assume I have generated tabs in excel that correspond with each SelectCell.Value.

Sub Consolidate_Sheets()
    Dim MyCell As Range
    Dim MyRange As Range
    Dim ws As Worksheet
    Set MyRange = Sheets("Install_Input").Range("A2")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))

Call superSizeMe(MyCell, MyRange)


Sub superSizeMe(SelectCell As Range, SelectRange As Range)
    Dim InstallInput As Worksheet
    Dim strPasteToSheet As String

   'New worksheet to paste into
    Dim DestinationSheet As Worksheet
    Dim DestinationRow As Range

    'Define worksheet with input data
    Set InstallInput = ThisWorkbook.Worksheets("Install_Input")

    For Each SelectCell In SelectRange.Cells

    InstallInput.Select

    If SelectCell.Value <> "" Then
        SelectCell.EntrieRow.Select ''''LOCATION OF RUN-TIME ERROR 438''''
        Selection.Copy
        Set DestinationSheet = Worksheets(SelectCell.Value)
        Set DestinationRow = DestinationSheet.Range("L1:L" & DestinationSheet.Cells(Rows.Count, "L").End(xlUp).Row)
        Range("L" & DestinationRow.Rows.Count + 1).Select
        ActiveSheet.Paste
    End If

    Next SelectCell

InstallInput.Select
InstallInput.Cells(1, 1).Select

If IsObject(InstallInput) Then Set InstallInput = Nothing
If IsObject(SelectRange) Then Set SelectRange = Nothing
If IsObject(SelectCell) Then Set SelectCell = Nothing
If IsObject(DestinationSheet) Then Set DestinationSheet = Nothing
If IsObject(DestinationRow) Then Set DestinationRow = Nothing


End Sub

I am getting a Run-time error'438' "Object doesn't support this property or method" on "SelectCell.EntireRow.Select"


Solution

  • Well your code has a typo

    SelectCell.EntrieRow.Select
    

    should say entire not Entrie. Personally I would use this method anyway, It selects the entire row based on the number you put in. FYI there is also a corresponding Columns().select if you need it in the future

    sel_cell_row = SelectCell.Row
    Rows(sel_cell_row).select
    

    edit addressed to comment

    The reason you get the 1004 error is like it says, the copy and paste areas don't match. Think of copying 10 rows, and trying to paste it into 2 rows, simply wouldn'y work. I'm guessing the problem actually stems from your destinationrows code. I'm not entirely sure what its trying to do, but here are two generic fixes

    1)keep the copy code as it is, and modify the paste. Instead of selecting a range of cells to paste into, select the first cell (if your range was a1:a10, selecting a1 is sufficient) excel will then paste all the data starting at that first cell. so in your code do this

    'comment out all this destination row stuff
    'Set DestinationRow = DestinationSheet.Range("L1:L" & DestinationSheet.Cells(Rows.Count, "L").End(xlUp).Row)
    'Range("L" & DestinationRow.Rows.Count + 1).Select
    Range("L1").select   'only referencing the first cell to paste into
    ActiveSheet.Paste
    

    2)rather than selecting an entire row, why not select only the populated values in that row something like

    sel_cell_row = SelectCell.Row
    lastColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
    range(Cells(sel_cell_row ,1),Cells(sel_cell_row ,lastColumn )).select
    

    then do your copy as usual. the 1 is for column 1, or A. I'm assuming the data you want is in one row starting at column A and going till lastColumn. Maybe now this will match your destinationrows code.

    3)Com,bine options 1 and 2. so copy only the populated cells, and paste to the first cell in the range