0

table3 of Receipt Sheet

This table has two the first two columns which I want to paste in another table which is "table9"

table9 of IndividualProfitLoss Sheet

the rows on both tables can vary however this is the sequence of the task. First clear table9, then take the first two columns of table3 and paste them into the first two columns of table9.

I tried this using the following from help on my other questions:

Private Sub CommandButton2_Click()  'ActiveX button's click event handler
    Dim lo As ListObject, TABLE_NAME, arr
    For Each TABLE_NAME In Array("IndividualProfitLoss|Table9") 'and so on
        On Error Resume Next
        arr = Split(TABLE_NAME, "|")
        Set lo = Me.Parent.Sheets(arr(0)).ListObjects(arr(1))
        If Err.Number <> 0 Then
            MsgBox TABLE_NAME & " was not found. Check the table name", vbCritical + vbOKOnly, "Sub CommandButton1_Click()"
            Exit Sub
        End If
        On Error GoTo 0
        If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.Delete
    Next
    
Dim Table3 As ListObject, Table9 As ListObject
Dim h As ListColumn

Set Table3 = Receipt.ListObjects("Table3")
Set Table9 = IndividualProfitLoss.ListObjects("Table9")

'loop over the headers from the source table
For Each h In Table3.ListColumns
    'is the column name in the "excluded" list?
    If IsError(Application.Match(h.Name, Array("??? ???????", "??????"), 0)) Then

        'ok to copy...
        h.DataBodyRange.Copy Table9.ListColumns(h.Name).DataBodyRange(1)

    End If
Next h
  
End Sub

this gives me runtime error 424. "Object Required".

any help would be appreciated

EDIT: I believe the code to clear table9 works but pasting the values of the first two columns of table3 into table9 is a problem. So a help there would be appreciated :)

  • which line throws the error? – Алексей Р Aug 09 '21 at 17:07
  • how can I check it? – Hamza Ahmed Aug 09 '21 at 17:11
  • when you get the error message window, click on the "Debug" button and see the yellow line – Алексей Р Aug 09 '21 at 17:13
  • this is where the yellow line is = Set Table3 = Receipt.ListObjects("Table3") – Hamza Ahmed Aug 09 '21 at 17:24
  • Most likely either CodeName `Receipt` does not exist. Try `Set Table3 = Sheets("Receipt").ListObjects("Table3")` – Алексей Р Aug 09 '21 at 17:30
  • Another error; Subscript out of range in line " h.DataBodyRange.Copy Table9.ListColumns(h.Name).DataBodyRange(1)" – Hamza Ahmed Aug 09 '21 at 17:57
  • Most likely the column named as value in `h.Name` does not exist in `Table9` – Алексей Р Aug 09 '21 at 18:00
  • i changed the column names but still same error in same line – Hamza Ahmed Aug 09 '21 at 18:04
  • This line: `For Each TABLE_NAME In Array("IndividualProfitLoss|Table9")` uses a table called `IndividualProfitLoss`, this line `Set Table9 = IndividualProfitLoss.ListObjects("Table9")` has `IndividualProfitLoss` as the codename for a sheet. – Darren Bartrup-Cook Aug 10 '21 at 07:20
  • I'm extremely sorry but I dont understand how thats a problem. I barely know vba so its difficult but that part of code is working just fine. The second part which has to do with copying/pasting the first two columns from Table3 to Table9 begins from ' Dim Table3 As ListObject, Table9 As ListObject' The code from here till below is where the problem lies; its not working. – Hamza Ahmed Aug 10 '21 at 07:34
  • My bad - didn't notice the `arr(0)` bit which is referencing the sheet. That whole block could be rewritten as `With IndividualProfitLoss.ListObjects("Table9"): If Not .DataBodyRange Is Nothing Then .DataBodyRange.Delete: End With` though. – Darren Bartrup-Cook Aug 10 '21 at 08:04
  • No options are working Im extremely sorry. Do you have a method via which I can copy the first two columns of Table3 to Table9 (which is in another sheet) from scratch? The clearing part is taken care of as visible from the code but only the copying one remains. – Hamza Ahmed Aug 10 '21 at 13:34
  • I tried the following code `Sub plgen2() ' ' plgen2 Macro ' ' Receipt.Range("Table3[[Column1]:[Column2]]").Select Selection.Copy Sheets("IndividualProfitLoss").Select ActiveCell.Offset(-11, -2).Range("A1").Select ActiveSheet.Paste End Sub` and it gives me "Object Required error" on the first line. This is to copy paste. – Hamza Ahmed Aug 10 '21 at 14:03

0 Answers0