VBA code copy data from cell to another sheet in table

vpseuro

New Member
Joined
Sep 8, 2021
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Dear all,
I`m having trouble in creating a correct VBA code for a copy paste function.
Main sheet "home"
Second sheet "clients"

On "home" sheet i have 4 cells ( C column - "C4:C7") representing data: id nr, name from a dropdown, name entered manually , and a date.
This data should be copied in the second "clients" sheet in a table "clientstable" header starting on row 5, first table row should be 6.

i made progress but the paste entry goes to row 7 and not in the table,
second progress , data goes in the table but fills the entire table and not specific row by row as i need it.

the goal is that data should go from one cell C4 , c5 , c6 ,c7 home sheet to A6 B6 C6 D6 clients sheet in table clientstable to next row in line available

Rich (BB code):
Sub InsertNewclient()
'
' Newclient Macro
'
'
Dim countrow As Long
Dim countcol As Long
Dim i As Integer
 Dim CurrentSheet As Object


countrow = Sheets("Clients").UsedRange.Rows.Count
countcol = Sheets("Clients").UsedRange.Columns.Count

 
    Sheets("Home").Select 
    Range("C4:C7").Select
  
    Selection.Copy
    Sheets("Clients").Select

    Range("Clientstable").Select
  
    Cells(Rows.Count, 1).End(xlUp)(1).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
  
          
End Sub
 

Attachments

  • Screenshot 2021-09-09 045258.png
    Screenshot 2021-09-09 045258.png
    12.6 KB · Views: 71
  • clientstable abcd collums.png
    clientstable abcd collums.png
    9 KB · Views: 71

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
?
Sheets("Home").Range("C4:C7").Copy Sheets("Clients").Range("A6:D6")
 
Upvote 0
For One Record Try This:
VBA Code:
Sheets("Clients").Range("A6:D6") = Application.Transpose(Sheets("Home").Range("C4:C7"))

if You want Repeat it for New Records one by One:
VBA Code:
Sub AddData()
Dim Lr As Long
Lr = Sheets("Clients").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Clients").Range("A" & Lr & ":D" & Lr) = Application.Transpose(Sheets("Home").Range("C4:C7"))
End Sub
 
Upvote 0
Dear @johnnyL and @maabadi

Thank you for the help , I implemented the one below. The thing as in previous cases/attempts with various approaches I could not succeed to add data to the actual table in the first row.

Tablename is Clientstable with four columns A:D in this case as described in the post.

VBA Code:
Sub AddData()
Dim Lr As Long
Lr = Sheets("Clients").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Clients").Range("A" & Lr & ":D" & Lr) = Application.Transpose(Sheets("Home").Range("C4:C7"))
End Sub

Can we use this and target the table?
or do we need to use specific .Range for every cell?

thank you
 

Attachments

  • rownr1intable.png
    rownr1intable.png
    16.7 KB · Views: 28
Upvote 0
Hi guys,
All formulas i have tried are actually checking the first available row and recognises the first empty row in my table as having data.
Nevertheless that first row has validated data or conditional formatting used from copy.

it seems that a solution would be to enter some IF clause to check if table is available under the name "Clientstable" and if not to be created then to enter data in first row.
i can`t find a solution or code to focus/target an already created table and copy / replace the data starting from row 6 and continue to next row in line.
 
Upvote 0
1. You want to add data at Row 6 ( first row after header)?
2. Then Are you want Insert Row and then first row will be empty for new record or Not?
Please Upload your Table with Xl2BB to we know to correct Structure of Your Table.
 
Upvote 0
once again thank you for your effort and time

this is my testing file


testinsertdatatotablemacro.xlsm
ABCDEFGHIJK
1
2
3
4Order ID1
5Entry byManager 2
6Client Namehfhhfhfh
7Entry date09/09/2021 18:05
8
9
10
11
Home
Cells with Data Validation
CellAllowCriteria
C5List=data!$A$2:$A$4



testinsertdatatotablemacro.xlsm
ABCDE
1
2
3C4C5C6C7
4
5Order IDEntry byClient nameEntry Date
6
71Manager 2hfhhfhfh44448.75381
8
9
10
11
Clients



testinsertdatatotablemacro.xlsm
ABCDEFGHIJK
1
2
3
4C4C6C7C2
5
6Order IDClient nameColumn4Column3Column1Entry DateDue DateTimeColumn2Entry by
71hfhhfhfh09/09/2021 18:05#VALUE!Manager 2
8
9
10
11
12
13
Clientstargetcollumn
Cell Formulas
RangeFormula
H7H7=TEXT(G7-F7, "dd:hh:mm")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
J6Cell Valuecontains "YES"textNO
E6Cell Valuecontains "Critical"textNO
Cells with Data Validation
CellAllowCriteria
J7List=data!$A$2:$A$4



testinsertdatatotablemacro.xlsm
AB
1Choose name
2Manager 1
3Manager 2
4Manager 3
5
data
 
Upvote 0
1. Are you want to add Data at 2nd Table Uploaded or 3rd?
2. if 2nd you want add data at row 6?
3. If you want 3rd Table, you want to add data to Column A, J , B , & F respectively?
4. Are you have unique ID? Can We use it to fid that data exist at new Table or Not?
 
Upvote 0
two cases / two macro

1. case C4:C7 from Home to go in Clients sheet / table Clientstable and every next entry to have a new row, the row can always be first or last , i assume it does not matter much i can sort it by ID .
2. case the C4:C7 from Home i have to put them in Clientstargetcollumn sheet in specific cell also a new row should be created in that table.


answer to your question
1: one macro will add data to second sheet called Clients in the table Clientstable another macro will add data to third sheet in table Clienttargetcollumn
2.the first macro should add data to second sheet that table Clintstable starting from first row of the table in this case row 6 , i assume it will be a transpose function so we can keep the formatting, formulas, data value etc., i managed to add data to the table wen i have first row with data inserted but the case will be that new file will be opened and table should be built with data from beginning in our case row 1 of the table. after entering one row of data , next macro run should create second row of data and so on..
3. second macro correct - to add data to Column A, J , B , & F respectively , same issue above with first row propagating in the table.

4. not sure of the unique ID - tables are named Clientstable and Clienttargetcollumn
 
Upvote 0
Try this for Both without searching for existing Data at Tables:
VBA Code:
Sub AddData()
Dim Lr As Long, Ar1() As Variant, Ar2() As Variant, Lr2 As Long
Lr = Sheets("Clients").Range("A5").End(xlDown).Row + 1
Lr2 = Sheets("Clienttargetcollumn").Range("A6").End(xlDown).Row + 1
Ar1 = Application.Transpose(Sheets("Home").Range("C4:C7"))

Ar2 = Array(Ar1(1), Ar1(3), "", "", "", Ar1(4), "", "", "", Ar1(2))
Sheets("Clients").Range("A" & Lr & ":D" & Lr) = Ar1
Sheets("Clienttargetcollumn").Range("A" & Lr).Resize(, UBound(Ar2)) = Ar2
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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