Page 1 of 1

vba copy

Posted: Sat Jul 03, 2021 9:40 pm
by sna
I am really struggling to solve thi, I have a sheet named CopyFromHere in which I have some data to be copied and pasted to another sheet named PasteHere
Now I want to write a macro for this , actually I am not able to figure out code for loop,I want to do as below
I want to copying from cell A2 to A19 from worksheet named CopyFromHere to cell D4 to D21 of worksheet named PasteHere , then I want to do same thing for column B,C,D of worksheet named CopyFromHere but this time with loop, because there may be 100 such columns,
I am also attaching this workbook
Thanks a lot in advance

Here's code

Code: Select all

Sub CopyTo()
Dim Sht1 As Worksheet, Sht2 As Worksheet
Dim x As Long
Set Sht1=Sheets("CopyFromHere")
Set Sht2=Sheets("PasteHere")
x=Sht1.Cells(1,Columns.Count).End(xlToLeft).Column
For i=1 To x
Sht1.Cells(2,i).Resize(18).Copy Sht2.Cells(4,i)
Next i
End Sub
But it's not working fine

Re: vba copy

Posted: Sat Jul 03, 2021 11:19 pm
by snasui
:D The example code is below:

Code: Select all

Sub CopyTo()
    Dim Sht1 As Worksheet, Sht2 As Worksheet
    Dim x As Long
    Set Sht1 = Sheets("CopyFromHere")
    Set Sht2 = Sheets("PasteHere")
    x = Sht1.Cells(1, Columns.Count).End(xlToLeft).Column
'    For i = 1 To x
    Sht1.Cells(2, 1).Resize(18, x).Copy Sht2.Cells(4, 4)
'    Next i
End Sub

Re: vba copy

Posted: Sun Jul 04, 2021 4:28 am
by sna
Thank you