Modify code to copy data from one sheet to another VBA

sofas

Active Member
Joined
Sep 11, 2022
Messages
484
Office Version
  1. 2019
Platform
  1. Windows
Welcome
How do I copy the value of a d2 cell cellin column b ; F2 cellin column D It is repeated over the number of rows copied


VBA Code:
Sub Bouton1_Cliquer()

    Dim LastRow As Integer
    Dim NextRow As Integer
    Dim RowCount As Integer
    
    Feuil1.Activate
    NextRow = Feuil2.Cells(Rows.Count, 6).End(xlUp).Row + 1
    
    LastRow = Cells(Rows.Count, 2).End(xlUp).Row
    RowCount = LastRow - 8
    
    
    Feuil2.Cells(NextRow, 5).Resize(RowCount, 7).Value = Cells(9, 1).Resize(RowCount, 7).Value
    
    
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
1.png

Screenshot 2022-11-22 011219.png
 
Last edited:
Upvote 0
I am not really clear on what you need but try this as a starting point.

VBA Code:
Sub Bouton1_Cliquer()

    Dim LastRowF1 As Integer
    Dim NextRowF2 As Integer
    Dim RowCount As Integer
    Dim rngF1 As Range
    
'    For my testing
'    Dim Feuil1 As Worksheet, Feuil2 As Worksheet
'    Set Feuil1 = Worksheets("Sheet1")
'    Set Feuil2 = Worksheets("Sheet2")
    
    With Feuil1
        NextRowF2 = Feuil2.Cells(Rows.Count, 6).End(xlUp).Row + 1
        If NextRowF2 < 7 Then NextRowF2 = 7                             '<--- Change this to the default starting row
        
        LastRowF1 = .Cells(Rows.Count, 2).End(xlUp).Row
        Set rngF1 = .Range(.Cells(9, "D"), .Cells(LastRowF1, "J"))
        RowCount = rngF1.Rows.Count
            
        Feuil2.Cells(NextRowF2, "E").Resize(RowCount, rngF1.Columns.Count).Value = rngF1.Value
        ' Copy Parameters including format
        .Range("D2").Copy Destination:=Feuil2.Cells(NextRowF2, "B").Resize(RowCount)
        .Range("F2").Copy Destination:=Feuil2.Cells(NextRowF2, "D").Resize(RowCount)
    End With
    
End Sub

My test data in case anyone else wants to have a go:
20221122 VBA Copy Data sofas.xlsm
ABCDEFGHIJ
1
2MACHINE212/11/2022
3
4
5
6
7
8
925612561R121231433101
1025612561R131231433101
1125612561R141231433101
1225612561R151231433101
1325612561R161231433101
1425612561R171231433101
1525612561R181231433101
1625612561R191231433101
1725612561R201231433101
1825612561R211231433101
19
Sheet1
 
Upvote 0
Solution
Thank you very much, this is what is really needed
 
Upvote 0

Forum statistics

Threads
1,215,597
Messages
6,125,738
Members
449,255
Latest member
whatdoido

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