Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

copy specific ranges from sheet to another

0

Hi 

I  try  copyIing   ranges  B2:B, E2:E   from sheet PURCHASE to  sheet BUYING & SELLING into  columns B,C . 

but  seem  to  my  way  is  wrong. I'm looking  forward  to right way  from anybody  have  idea .

here  is  the  code 

Sub n()
Dim lr As Long
lr = Sheets("BUYING & SELLING").Range("B" & Rows.Count).End(xlUp).Row
With Sheets("PURCHASE")
.Range("B2:B" & "E2:E" & .Cells(.Rows.Count, "B").End(xlUp).Row).Copy
Sheets("BUYING & SELLING").Range("B" & lr + 1).PasteSpecial Paste:=xlPasteValues
End With
End Sub
Answer
Discuss

Answers

0
Selected Answer

Ali

Try this version (commented to help your understanding):

Sub n()
Dim lr As Long, lr2 As Long

' copy source data
With Sheets("PURCHASE")
    lr2 = .Range("B" & .Rows.Count).End(xlUp).Row
    .Range("B2:B" & lr2 & ", E2:E" & lr2).Copy
End With

' paste values after values on target sheet
With Sheets("BUYING & SELLING")
    lr = .Range("B" & .Rows.Count).End(xlUp).Row
    .Range("B" & lr+1).PasteSpecial Paste:=xlPasteValues
    ' remove copy dotted lines
    Application.CutCopyMode = False
End With

End Sub

Note that the first bold section above uses a separate With block to do the copying (before the main With block putting that data at the end of your target sheet).

Hope this helps.

Discuss

Discussion

Hi  john 
thanks .  seem you  misundrstood  what  I  want  it ,  just  copy  specified  columns as willie's suggestion  even  that  gives  error  for willie's code . 
Ali M (rep: 28) Dec 1, '22 at 2:51 am
Agreed Ali. I woke to see Willie's answer and realised my mistake. Hopefully my comment on his Answer fixes things for you. 
John_Ru (rep: 6142) Dec 1, '22 at 3:14 am
Please see my revised Answer.
John_Ru (rep: 6142) Dec 1, '22 at 7:02 am
good work  john !
thanks  very  much
Ali M (rep: 28) Dec 1, '22 at 3:05 pm
Thanks for selecting my answer Ali.
John_Ru (rep: 6142) Dec 1, '22 at 4:30 pm
Add to Discussion
0

The following is a slight modification of John's suggestion. The difference is that my suggestion only deals with columns B & C on the BUYING & SELLING sheet and only copies columns B & E on the PURCHASE sheet.

>> updated 12/1 as recommended  by John

>> updated 12/1 - added line of code so first paste of data row2 and each subsequent run of the macro leaves a blank row between each data set; revised file attached.

Sub n()

Dim lr As Long

Application.ScreenUpdating = False

' uncomment the following 7 lines to delete existing data

' With Sheets("BUYING & SELLING")
'    ' clear target columns (B & C)
'    lr = .Range("B" & .Rows.Count).End(xlUp).Row
'          .Range("B2:B" & lr).Clear
'    lr = .Range("C" & .Rows.Count).End(xlUp).Row
'          .Range("C2:C" & lr).Clear
' End With
    
    ' copy source data
        lr = Sheets("PURCHASE").Range("B" & Rows.Count).End(xlUp).Row
        Sheets("PURCHASE").Range("B2:B" & lr).Copy
        lr = Sheets("BUYING & SELLING").Range("B" & Rows.Count).End(xlUp).Row
        If lr <> 1 Then lr = lr + 1 ' this line will cause there to be an empty row between each data set
        Sheets("BUYING & SELLING").Range("B" & lr + 1).PasteSpecial Paste:=xlPasteValues
        
         lr = Sheets("PURCHASE").Range("E" & Rows.Count).End(xlUp).Row
         Sheets("PURCHASE").Range("E2:E" & lr).Copy
         lr = Sheets("BUYING & SELLING").Range("C" & Rows.Count).End(xlUp).Row
         If lr <> 1 Then lr = lr + 1 ' this line will cause there to be an empty row between each data set
         Sheets("BUYING & SELLING").Range("C" & lr + 1).PasteSpecial Paste:=xlPasteValues
        
        Sheets("BUYING & SELLING").Activate
            Range("A" & lr).Select

Application.ScreenUpdating = True

    ' remove copy dotted lines
    Application.CutCopyMode = False

End Sub
'
'

Cheers   :-)

Discuss

Discussion

Willie. Yours is a better solution since I didn't realise Ali wanted to copy a non-contiguous range i.e. cells from B and E only but not C and D - my bad! That will teach me not to fire off a quick fix! 
John_Ru (rep: 6142) Dec 1, '22 at 1:46 am
Hi willie
thanks,  but  gives application defined  error in this  line 
.Range("B2:" & lr).Clear

I  hope you  can  fix  it 
Ali M (rep: 28) Dec 1, '22 at 2:53 am
@Ali

That's a minor mistake by Willie (who's probably asleep in the USA I think). You need to change that line to read:
.Range("B2:B" & lr).Clear
 
and do similar for the line clearing C2 and down.

@Willie

If you agree, I suggest you correct your Answer accordingly (for the benefit of later users).
John_Ru (rep: 6142) Dec 1, '22 at 3:08 am
thanks John again for your  guiding  me  to  correction !
seem  the  code  reproduce  to  fixing  all of  things .
based  on  my  orginal  code  when  copy  to  Sheets("BUYING & SELLING") as  in  this  line 
Sheets("BUYING & SELLING").Range("B" & lr + 1).PasteSpecial Paste:=xlPasteValues
this  means  copy  after  lastrow  is  filled , but  based  on  the willie's code  will  replace  data  when run  the  macro  every  time should  not  do  it.
Ali M (rep: 28) Dec 1, '22 at 3:34 am
Ali, I've now modified my Answer to do that (and Willie might do the same)
John_Ru (rep: 6142) Dec 1, '22 at 7:03 am
John,
Thanks for catching my error. I have update my post as you suggested with a couple of other little tweaks.
I have tested it and it seems to achieve what Ali M wants. I have also attached my testing file.
And yes, I was asleep, but in Canada, not USA.   :-)
WillieD24 (rep: 557) Dec 1, '22 at 12:47 pm
Oops! Sorry about guessing your Iocation incorrectly Willie. 
John_Ru (rep: 6142) Dec 1, '22 at 1:13 pm
No worries John. It's all good.     ;-)
WillieD24 (rep: 557) Dec 1, '22 at 1:39 pm
thanks  Willie  , but  I  no  know  why  start  from row3 , should  be from  row 2  
and  based  the  code  you  start  from row2 . this  is  strange !!
Ali M (rep: 28) Dec 1, '22 at 3:07 pm
Hi Ali M,
I didn't notice that the first run of the macro started at row3. I have added a line of code to fix this and still leave a blank row btween each data set. (having a blank row makes it easy to see where each data set starts ans ends)

Cheers
WillieD24 (rep: 557) Dec 1, '22 at 3:59 pm
Willie- thanks for revising your answer (and doing it correctly- putting me to shame!). I'm going to ask Don about email notifications since once again I've not been alerted to new comments on either Answer. In fact that been the case since 21 November.
John_Ru (rep: 6142) Dec 2, '22 at 3:33 am
thanks  willie ,
in  reality  I  don't  need empty row  amongst  ranges  , do  you  know  why  becuase  I  have  another  macro  to  merge  and  the  empty  row  could  be  problem when merge , so  I  disabled  your  condition  and  works  as i expected ,  but  it's  usefule  maybe  I  need  it  for  the  others  projects .
many  thanks   for  your  effort
Ali M (rep: 28) Dec 2, '22 at 12:24 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login