VBA - Easy (?) copy operation

oggy3000

Board Regular
Joined
Jul 13, 2011
Messages
51
Hey,
I do have two little problems with a copy operation in VBA.
Here are the Facts:
1. I want to copy the cell contents of cells A2 to F( until last row) from the sheet "Transfer" to the columns D to I in the sheet "Outbound". In the "Outbound" sheet they are supposed to be copied after the last entry already existing in this sheet.
2. In column G of "Transfer" there are quantities (numbers) which I want to to be copied to "Outbound" as well, but into column J. Here comes the tricky thing: The amount is supposed to be negative. The values in "Transfer" are always positive, and I want them to change to negative when they are copied to "Outbound".

My knowledge of VBA is quite limited so I would appreciate your help very much :)

Cheers
Jan
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try

Code:
Sub test()
Dim LR As Long, LR2 As Long, i As Long
LR2 = Sheets("Outbound").Range("A" & Rows.Count).End(xlUp).Row
With Sheets("Transfer")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A2:G" & LR).Copy Destination:=Sheets("Outbound").Range("D" & Rows.Count).End(xlUp).Offset(1)
End With
With Sheets("Outbound")
    For i = LR2 + 1 To LR2 + LR - 1
        .Range("J" & i).Value = .Range("J" & i).Value * -1
    Next i
End With
End Sub
 
Upvote 0
Hey VoG,
the first half of your code works great and as expected. Thanks a lot for that.
Unfortunately the second part does not change the values to negative. I dont know why because from the code part it looks logical and should be working.

Any Idea on that?

Cheers
Jan
 
Upvote 0
Try this small change

Rich (BB code):
Sub test()
Dim LR As Long, LR2 As Long, i As Long
LR2 = Sheets("Outbound").Range("J" & Rows.Count).End(xlUp).Row
With Sheets("Transfer")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A2:G" & LR).Copy Destination:=Sheets("Outbound").Range("D" & Rows.Count).End(xlUp).Offset(1)
End With
With Sheets("Outbound")
    For i = LR2 + 1 To LR2 + LR - 1
        .Range("J" & i).Value = .Range("J" & i).Value * -1
    Next i
End With
End Sub
 
Upvote 0
Code:
Sub copyPaste()
Dim rngCopy     As Range
Dim rngCopyG    As Range
Dim rngPaste    As Range
Dim rngPasteJ   As Range
Dim lngLRow     As Long
    With ThisWorkbook.Worksheets("Transfer")
        lngLRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngCopy = .Range("A2:G" & lngLRow)
        Set rngCopyG = .Range("G2:G" & lngLRow)
    End With
    
    With ThisWorkbook.Worksheets("Outbound")
        lngLRow = .Range("D" & Rows.Count).End(xlUp).Row
        Set rngPaste = .Range("D" & lngLRow + 1)
        rngCopy.Copy rngPaste
        Set rngPasteJ = .Range("J" & lngLRow + 1 & ":J" & .Range("J" & Rows.Count).End(xlUp).Row)
        With rngPasteJ
            .Value = Evaluate(rngPasteJ.Address & " * -1")
        End With
        
    End With
    
    
End Sub
 
Upvote 0
Perfect!
It works just the way it is supposed to :)
Thank you so much VoG.

Just one more short question: Which command would I have to add if the entries are only to be copied with borders but no colors, not bold and stuff like that?

Cheers
Jan

*edit*
thanks to you as well, littleiitin, but I used VoG's version already :)
 
Upvote 0
This won't do borders but try

Code:
Sub test()
Dim LR As Long, LR2 As Long, i As Long
LR2 = Sheets("Outbound").Range("J" & Rows.Count).End(xlUp).Row
With Sheets("Transfer")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A2:G" & LR).Copy
    Sheets("Outbound").Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End With
With Sheets("Outbound")
    For i = LR2 + 1 To LR2 + LR - 1
        .Range("J" & i).Value = .Range("J" & i).Value * -1
    Next i
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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