Copy worksheet columns from a second workbook based on the worksheet name in the first workbook - how?

kntrigirl1908

New Member
Joined
Apr 20, 2020
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
Hi! After searching the internet I happened to find myself here. I'm very new to vb and know that it can be used to automate repetitive tasks.

So here is my dilemma. I have several worksheets in a workbook where I have identified a category variance in cell P2. The category variance could be one of six and each are stored as a separate worksheet in a second workbook. I'm trying to work out how to use cell P1 in the first workbook as a reference to go to the same worksheet name in the second workbook and copy a range of columns. The worksheets never change in the second workbook but can for the first which I've built a reference table to handle that.

My thinking was to use a vlookup function to use the worksheet name in the first workbook to find the category variance worksheet name in the 2nd workbook and that works. However, I'm have trouble referencing the value in P2
as part of the formula when copying and pasting between workbooks. CategoryVariance is the 2nd workbook and Generate is the 1st workbook.

My error is "Run-time error '9': Subscript out of range". What am I doing wrong?

Sub Generate ()
Dim Lastws As Integer

Lastws = Application.Worksheets.Count

For i = 3 To Lastws
Worksheets(i).Activate
ActiveSheet.Cells(1, 16).Value = ActiveSheet.Name
ActiveSheet.Cells(2, 16).Value = Application.WorksheetFunction.VLookup(Range("P1").Value, Range("FluxWs"), 3, 0)
Workbooks("CategoryVariance.xlsm").Worksheets(Cells(2, 16)).Range("P:AF").Copy Workbooks("Generate.xlxm").Worksheets(3).Range("P1")
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi! After searching the internet I happened to find myself here. I'm very new to vb and know that it can be used to automate repetitive tasks.

So here is my dilemma. I have several worksheets in a workbook where I have identified a category variance in cell P2. The category variance could be one of six and each are stored as a separate worksheet in a second workbook. I'm trying to work out how to use cell P1 in the first workbook as a reference to go to the same worksheet name in the second workbook and copy a range of columns. The worksheets never change in the second workbook but can for the first which I've built a reference table to handle that.

My thinking was to use a vlookup function to use the worksheet name in the first workbook to find the category variance worksheet name in the 2nd workbook and that works. However, I'm have trouble referencing the value in P2
as part of the formula when copying and pasting between workbooks. CategoryVariance is the 2nd workbook and Generate is the 1st workbook.

My error is "Run-time error '9': Subscript out of range". What am I doing wrong?

Sub Generate ()
Dim Lastws As Integer

Lastws = Application.Worksheets.Count

For i = 3 To Lastws
Worksheets(i).Activate
ActiveSheet.Cells(1, 16).Value = ActiveSheet.Name
ActiveSheet.Cells(2, 16).Value = Application.WorksheetFunction.VLookup(Range("P1").Value, Range("FluxWs"), 3, 0)
Workbooks("CategoryVariance.xlsm").Worksheets(Cells(2, 16)).Range("P:AF").Copy Workbooks("Generate.xlxm").Worksheets(3).Range("P1")

I found the answer that works for me, thanks!
 
Upvote 0

Forum statistics

Threads
1,215,470
Messages
6,124,992
Members
449,201
Latest member
Lunzwe73

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