I am looking to copy a row in worksheet called "Updates" to a table in "6.2022 Basis". I have my VBA set up, but I am having trouble getting it to work without it overwriting a row of data in my table. Is there a way to make my VBA add a new row in my table before it pastes? I have a sorting VBA in the table that requires the new row to be included in the table. My table has no blank rows and I need this command button to automatically copy the row selected and paste it (this excel sheet will be used by others and it will be locked for certain functions). Here is my current code
Private Sub CommandButton3_Click()
Dim rngToCopy As Range
On Error Resume Next
Set rngToCopy = Application.InputBox("Select range in Updates", Type:=8)
If rngToCopy Is Nothing Then Exit Sub
On Error GoTo 0
ThisWorkbook.Worksheets("6.2022 Basis").Activate
Dim rngToPaste As Range
On Error Resume Next
Set rngToPaste = Application.InputBox("Select range to Paste in 6.2022 Basis", Type:=8)
If rngToPaste Is Nothing Then Exit Sub
On Error GoTo 0
rngToPaste.ClearContents
Dim r As Long, c As Long
For r = 1 To rngToCopy.Rows.Count
For c = 1 To rngToCopy.Columns.Count
If rngToCopy.Cells(r, c) <> "" Then
rngToPaste.Cells(r, c).Formula = "=" & rngToCopy.Cells(r, c).Address(External:=True)
End If
Next
Next
End Sub
I appreciate any advice or help!
My new working code is as of 6.27.2022, I am running into a run-time error '438' any advice would be greatly appreciated!
Private Sub CommandButton3_Click()
Dim rngToCopy As Range
On Error Resume Next
Set rngToCopy = Application.InputBox("Select range in Updates", Type:=8)
If rngToCopy Is Nothing Then Exit Sub
On Error GoTo 0
ThisWorkbook.Worksheets("6.2022 Basis").Activate
Dim DataTable As ListObject
Set DataTable = ThisWorkbook.Worksheets("6.2022 Basis").Basis_Table ' Change this to match the name of your table
'alternatively, you can refer to the table by number.
'If there is only one table on the sheet, then you can write ListObjects(1)
Dim r As Long, c As Long
For r = 1 To rngToCopy.Rows.Count
Dim DataRow As ListRow
Set DataRow = DataTable.ListRows.Add
For c = 1 To rngToCopy.Columns.Count
If rngToCopy.Cells(r, c) <> "" Then
DataRow.Range.Cells(1, c).Formula = "=" & rngToCopy.Cells(r, c).Address(External:=True)
End If
Next
Next
End Sub