VBA Copy and Paste Macro doesnt line up correctly

LostInEverything

New Member
Joined
May 27, 2024
Messages
6
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
Hi,

To begin with I'm quite new to excel VBA and trying to learn it as I go, so if anyone has good starting points or materials I would love to see them.

But onto my actual question. I am trying to make a spreadsheet that uses a macro to send info form specific cells in another sheet to a second. Now I have managed to make that happen, but the way I have to do it requires me to separate the copy and paste action. Therefore, I am encountering an issue where the data doesnt line up as I want it to.

It comes out like this:
1716790790566.png


When I need it to look like this:
1716791023637.png


This is the code I am using:

Sub Copy3()

If IsEmpty(Sheets("SourceSheet").Range("D2:D3").Value) Then
End
Else: Sheets("SourceSheet").Select
Range("D2:D3").Select
Selection.Copy
Sheets("TargetSheet").Select
Sheets("TargetSheet").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

End If

If IsEmpty(Sheets("SourceSheet").Range("B7:C10").Value) Then
End
Else: Sheets("SourceSheet").Select
Range("B7:C10").Select
Selection.Copy
Sheets("TargetSheet").Select
Sheets("TargetSheet").Range("D" & Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

End If

End Sub

If anyone knows a way to solve this, it would be greatly appreciated.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Welcome to the Forum.
Please show us what the source data looks like ideally using the Forum's XL2BB to save us having to manually create test data.
If you have to use an image please include the row and column references.

Using XL2BB
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Book1.xlsm
ABCDEF
1
2Date27/05/2024 20:27
3SellerSeller1
4Price$ 30.00
5
6productamountprice
7Item1120
8Item2210
9
10
SourceSheet
Cell Formulas
RangeFormula
D2D2=NOW()
D4D4=SUM(D7:D8)
Cells with Data Validation
CellAllowCriteria
D3ListSeller1, Seller2


Book1.xlsm
ABCDEFG
1
2DateSellerProductAmountPrice
327/05/2024 20:27Seller1Item11
4Item22
5
6
7
8
9
TargetSheet
Cell Formulas
RangeFormula
B3B3=NOW()
Cells with Data Validation
CellAllowCriteria
C7ListSeller1, Seller2
C5ListSeller1, Seller2
C3ListSeller1, Seller2


Is this what you meant? Does this help?
 
Upvote 0
Sorry I completely forgot a piece of information that probably helps. The SourceSheet is meant to act as a sale form where that is the only data on that sheet. the area where it says seller is meant to be able to be swapped between both seller 1 and 2. Additionally there is a save button that runs a macro using the code from earlier next to it, it just didnt copy over for some reason. So basically in sourcesheet you only change out what is needed and hit save and it puts it in targetsheet, however when it runs it comes out with the issue I had in my intital post. If that is clear?
 
Upvote 0
If you are happy to preformat the columns in the target sheet eg column B as Date-Time, columns E & F with the appropriate number format, then this might work for you.

VBA Code:
Sub CopySalesForm()

    Dim shtSrc As Worksheet, shtTgt As Worksheet
    Dim rowlastSrc As Long, rownextTgt As Long
    Dim rngProductSrc As Range
    
    Set shtSrc = Worksheets("SourceSheet")
    Set shtTgt = Worksheets("TargetSheet")
    
    rowlastSrc = shtSrc.Range("C" & Rows.Count).End(xlUp).Row
    rownextTgt = shtTgt.Range("D" & Rows.Count).End(xlUp).Row + 1
    
    With shtSrc
        Set rngProductSrc = .Range(.Cells(7, "B"), .Cells(rowlastSrc, "D"))
    End With
    
    shtTgt.Cells(rownextTgt, "B").Value2 = shtSrc.Cells(2, "D").Value2
    shtTgt.Cells(rownextTgt, "C").Value2 = shtSrc.Cells(3, "D").Value2
    
    With rngProductSrc
        shtTgt.Cells(rownextTgt, "D").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
    End With
    
End Sub
 
Upvote 1
Solution
Yes this works thank you. However, if it isnt too much trouble could i have an explanation on how it works, so that i can utilise it other times and/or add to it in the future. If not that is ok as you have been an incredible help.
 
Upvote 0
The target value = source value is the equivalent of PasteSpecial Values but is more efficient.
The code works out what the last row used is in the target sheet using the last cell in column C with data in it. It then adds 1 to that row number and uses that for the output.
If you have specific question I am happy to answer them.
 
Upvote 0
Thankyou, you have been a massive help. Was just wondering on a slight change to the code, as I have made a small change to the sheet. therefore certain positionings alter sligthly.

Book1.xlsm
ABCDEFGHIJK
1
2Date27/05/2024 23:40
3SellerSeller1Item11
4Price$ 15.40Item22
5Markup1.1Item33
6Item44
7ProductAmountPriceItem55
8Item11$ 1.10Item66
9Item22$ 4.40
10Item33$ 9.90
11$ -
12$ -
13$ -
14$ -
15$ -
16$ -
17$ -
SourceSheet
Cell Formulas
RangeFormula
D2D2=NOW()
D4D4=SUM(D8:D17)
D8:D17D8=IF(B8=0,0,IF($D$5=0,0,IF(C8=0,0,VLOOKUP(B8,$J$3:$K$8,2,FALSE))*C8*$D$5))
Cells with Data Validation
CellAllowCriteria
D3ListSeller1, Seller2
B8:B17List=$J:$J
D5List1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2


Book1.xlsm
ABCDEFGH
1
2DateSellerProductAmountMarkup Price Total
327/05/2024Seller1Item111.10$ 1.10$ 15.40
4Item22$ 4.40
5Item33$ 9.90
TargetSheet
Cells with Data Validation
CellAllowCriteria
C5ListSeller1, Seller2
C3ListSeller1, Seller2


I made this change to the code to get the outcome ive shown.

VBA Code:
Sub CopySalesForm()

    Dim shtSrc As Worksheet, shtTgt As Worksheet
    Dim rowlastSrc As Long, rownextTgt As Long
    Dim rngProductSrc As Range
    
    Set shtSrc = Worksheets("SourceSheet")
    Set shtTgt = Worksheets("TargetSheet")
    
    rowlastSrc = shtSrc.Range("C" & Rows.Count).End(xlUp).Row
    rownextTgt = shtTgt.Range("D" & Rows.Count).End(xlUp).Row + 1
    
    With shtSrc
        Set rngProductSrc = .Range(.Cells(8, "B"), .Cells(rowlastSrc, "C"))
    End With
    
    shtTgt.Cells(rownextTgt, "B").Value2 = shtSrc.Cells(2, "D").Value2
    shtTgt.Cells(rownextTgt, "C").Value2 = shtSrc.Cells(3, "D").Value2
    shtTgt.Cells(rownextTgt, "H").Value2 = shtSrc.Cells(4, "D").Value2
    shtTgt.Cells(rownextTgt, "F").Value2 = shtSrc.Cells(5, "D").Value2
    
    With rngProductSrc
        shtTgt.Cells(rownextTgt, "D").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
    End With
    
    With shtSrc
        Set rngProductSrc = .Range(.Cells(8, "D"), .Cells(rowlastSrc, "D"))
    End With
    
    With rngProductSrc
        shtTgt.Cells(rownextTgt, "G").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
    End With
    
End Sub

But wanted to know if its possible to make it more concise like this, though i believe there is an issue with the 3rd last line

VBA Code:
Sub CopySalesForm()

    Dim shtSrc As Worksheet, shtTgt As Worksheet
    Dim rowlastSrc As Long, rownextTgt As Long
    Dim rngProductSrc As Range
    Dim rngProductSrc2 As Range
    
    Set shtSrc = Worksheets("SourceSheet")
    Set shtTgt = Worksheets("TargetSheet")
    
    rowlastSrc = shtSrc.Range("C" & Rows.Count).End(xlUp).Row
    rownextTgt = shtTgt.Range("D" & Rows.Count).End(xlUp).Row + 1
    
    With shtSrc
        Set rngProductSrc = .Range(.Cells(8, "B"), .Cells(rowlastSrc, "C"))
        Set rngProductSrc2 = .Range(.Cells(8, "D"), .Cells(rowlastSrc, "D"))
    End With
    
    shtTgt.Cells(rownextTgt, "B").Value2 = shtSrc.Cells(2, "D").Value2
    shtTgt.Cells(rownextTgt, "C").Value2 = shtSrc.Cells(3, "D").Value2
    shtTgt.Cells(rownextTgt, "H").Value2 = shtSrc.Cells(4, "D").Value2
    shtTgt.Cells(rownextTgt, "F").Value2 = shtSrc.Cells(5, "D").Value2
    
    With rngProductSrc
        shtTgt.Cells(rownextTgt, "D").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
        shtTgt.Cells(rownextTgt, "G").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
    End With
    
End Sub

Sorry about how much this is, I just keep wanting to know and change more to make it better
 
Upvote 0
It will come down to what you find easier to follow and modify in the future
This is a shorter option
(I have added turning off/on the screenupdating)
Note:
Offset(0,1) -> moves 1 column to the right
Resize(,1) -> changes the number of columns to 1 (was initially 2)

Rich (BB code):
Sub CopySalesForm_OPMod1_adj()

    Dim shtSrc As Worksheet, shtTgt As Worksheet
    Dim rowlastSrc As Long, rownextTgt As Long
    Dim rngProductSrc As Range
    
    Application.ScreenUpdating = False
    
    Set shtSrc = Worksheets("SourceSheet")
    Set shtTgt = Worksheets("TargetSheet")
    
    rowlastSrc = shtSrc.Range("C" & Rows.Count).End(xlUp).Row
    rownextTgt = shtTgt.Range("D" & Rows.Count).End(xlUp).Row + 1
    
    With shtSrc
        Set rngProductSrc = .Range(.Cells(8, "B"), .Cells(rowlastSrc, "C"))
    End With
    
    shtTgt.Cells(rownextTgt, "B").Value2 = shtSrc.Cells(2, "D").Value2
    shtTgt.Cells(rownextTgt, "C").Value2 = shtSrc.Cells(3, "D").Value2
    shtTgt.Cells(rownextTgt, "H").Value2 = shtSrc.Cells(4, "D").Value2
    shtTgt.Cells(rownextTgt, "F").Value2 = shtSrc.Cells(5, "D").Value2
    
    With rngProductSrc
        shtTgt.Cells(rownextTgt, "D").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
        shtTgt.Cells(rownextTgt, "G").Resize(.Rows.Count, 1).Value2 = .Offset(0, 1).Resize(, 1).Value2
    End With
    
    Application.ScreenUpdating = False
    
End Sub

Breaking it up into 2 parts would be longer but might be clearer:
(you could put the 2nd "With" inside the first and just use With .Offset(0, 1).Resize(, 1) dropping the rngProductSrc but it is the same number of lines and probably less clear)

VBA Code:
    With rngProductSrc
        shtTgt.Cells(rownextTgt, "D").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
    End With
    
    With rngProductSrc.Offset(0, 1).Resize(, 1)
        shtTgt.Cells(rownextTgt, "G").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2
    End With
 
Upvote 1

Forum statistics

Threads
1,224,813
Messages
6,181,116
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