Copying info from one table to another table

Boua

New Member
Joined
Dec 28, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I hope that someone can help me with the following problem. I have a workbook with different sheets and tables. A commandbutton copies a ListedRow to another table. I created a small workbook that explains my problem.
The code that copies the full ListedRow works perfect. Based upon a "yes" in the last column the row is copied to the other table. But i don't need the full row, but only the first 8 columns. Thus Name, FirstName, FullName, State, Address, Zip, City and Info1. So if i select a "yes" from the dropdown in the last column and i click on the commandbutton, the corresponding 8 columns must be copied to the other table and then the full row has to be deleted. This have to work in both directions. From table1 to table2 and from table2 to table1.

The code for the commandbutton
Option Explicit

Sub CopyStudentInfoToOtherTable()

'Declaration
Dim tblShRead As Object 'table that's been read
Dim tblShWrite As Object 'table where info will be written too
Dim RowsInTable As Long, x As Long 'last row in a table, x used as counter
Dim srcRow As Range 'row in table that's been read
Dim dstRow As Object 'row that will be written

'assignments
'checks which sheet is active
'source and destination will be adapted
If ActiveSheet.CodeName = "Blad1" Then
Set tblShRead = Blad1.ListObjects("table1")
Set tblShWrite = Blad2.ListObjects("table2")
Else
Set tblShRead = Blad2.ListObjects("table2")
Set tblShWrite = Blad1.ListObjects("table1")
End If

RowsInTable = tblShRead.ListRows.Count

For x = RowsInTable To 1 Step -1
'copy of fullrow from sourcetable to estinationtable if column 8 in sourcetable is yes
'The copy in the destinationtable is on the first line of the Databody

If tblShRead.DataBodyRange.Cells(x, 10) = "yes" Then
Set srcRow = tblShRead.ListRows(x).Range
Set dstRow = tblShWrite.ListRows.Add(1) 'insert empty line in destinationtable
srcRow.Copy
dstRow.Range(1, 1).PasteSpecial xlPasteValues
srcRow.Delete 'delete fullrow from sourcetable
End If
Next x

End Sub
 

Attachments

  • sheet1_table1.jpg
    sheet1_table1.jpg
    102.3 KB · Views: 20
  • sheet2_table2.jpg
    sheet2_table2.jpg
    98.2 KB · Views: 19

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You made my day. It works perfect.
Thanks.

Can i ask an extra small question?
Suppose i want to copy only the first 2 columns and then columns 5,6 and 7. Based upon the same condition ('yes" in column 8)

Thx
 
Upvote 0
Can i ask an extra small question?
Suppose i want to copy only the first 2 columns and then columns 5,6 and 7. Based upon the same condition ('yes" in column 8)
The quickest change would be:

Rich (BB code):
'Replace this:-
'srcRow.Resize(, 8).Copy
'dstRow.Range(1, 1).PasteSpecial xlPasteValues

'With this
srcRow.Resize(, 2).Copy
dstRow.Range(1, 1).PasteSpecial xlPasteValues

srcRow.Resize(, 3).Offset(, 4).Copy
dstRow.Range(1, 1).Offset(, 4).PasteSpecial xlPasteValues
 
Upvote 0
thank you for all the info.
Very much appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top