excel macro to copy data ind paste into selected column depending on cell number

goeman

New Member
Joined
Jan 18, 2018
Messages
13
Hi there

Would really appreciate someones help please.

I have the following in a spreadsheet

Sheet 1
Col A: contains various numbers in the row e.g 23, 97, 254 etc (then Rows B to H contain various data)

A B C D E F G
1
2 23
3 97
4 254
5 588


I the need to look at the numbers in col A (e.g 23) and then copy the data cells B2:G2 and paste into row 23 in Sheet2 and then the same for the next number in col A in Sheet1 (e.g 97) copy data in cells B3:G3 and paste into row 97 in Sheet2

Thanks for your help
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:
Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        Range("B" & i & ":" & "G" & i).Copy Sheets(2).Cells(Sheets(1).Cells(i, 1).Value, 1)
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for the fast reply

but seems macro falls down on :

Range("B" & i & ":" & "G" & i).Copy Sheets(2).Cells(Sheets(1).Cells(i, 1).Value, 1)
 
Upvote 0
Do you have row numbers in all rows in column "A" starting in Row(1)

so columns A looks like this for example:

12
14
45
34
23


Do you have a Sheet 2 ?

Or do you have text in range ("A1")
 
Last edited:
Upvote 0
Sorry my fault

the issue was that I had a text header in first row of Sheet1

removed that and it works perfectly

Thanks again for your help...
 
Upvote 0
Glad I was able to help you.
If you want to keep the header change this line of code:
Code:
For i = 1 To Lastrow

To:

Code:
For i = 2 To Lastrow

The 1 means start on row (1)
The 2 means start on row (2)
 
Upvote 0
Glad I was able to help you.
If you want to keep the header change this line of code:
Code:
For i = 1 To Lastrow

To:

Code:
For i = 2 To Lastrow

The 1 means start on row (1)
The 2 means start on row (2)



excellent thank you

final last question please.... if I wanted to rename Sheet1 to 'Input' and Sheet2 to 'Data'?
 
Upvote 0
If your going to run the script from the Sheet named "Input" no change is needed for that part.

Just change this line of code:

Range("B" & i & ":" & "G" & i).Copy Sheets("Data").Cells(Sheets(1).Cells(i, 1).Value, 1)


If your not going to be running the script from sheet("Input") then there would have to be several more changes.

I do not have access to Excel at this time and will have to get back to you.

This is one reason it's always best to point out all these details in your original post.
 
Upvote 0
Try this:

Note sheet names and starts on row(2)
Code:
Sub Test()
'Modified 1-18-18 10:32 AM EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Sheets("Input").Activate
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To Lastrow
        Range("B" & i & ":" & "G" & i).Copy Sheets("Data").Cells(Sheets("Input").Cells(i, 1).Value, 1)
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,941
Members
448,534
Latest member
benefuexx

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