VBA code question

jaysus22

New Member
Joined
Oct 13, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Good day all - thanks in advance for any assistance you can provide.

I'm working on a rather large workbook that has around 20 different sheets, with one master data sheet. What I'm trying to accomplish is to have a VB code that based upon 2 input cells will filter the data. I have already gotten the filtering code to work without any trouble. My input cells are M3 & M4.

With Worksheets("Data")
If .AutoFilterMode = True Then .AutoFilterMode = False

.Range("A8").AutoFilter Field:=8, Criteria1:=Array("=" & .Range("M3").Value)

.Range("A8").AutoFilter Field:=6, Criteria1:=Array("<" & .Range("M4").Value)

Once data is filtered it will then copy/paste to one of the 20 sheets that exist in this workbook. The part I'm struggling with is where to paste the data (columns A:H on the sheet called "Data"). On the "Data" sheet I've created a dropdown box (cell M5) that has all the existing SheetNames for the entire workbook listed by utilizing the following formula on a (=IFERROR(INDEX(SheetNames,R2),""), =IFERROR(INDEX(SheetNames,R3),""), etc). on a look-up reference sheet. This works great and all the sheets are listed w/ the exact name.

What I'd like to do is set a "TARGET" in the code for the location where the copied data can be pasted to based upon the value that exists in cell "M5".

I've done a lot of research over the last few days, and I'm struggling to come up with something.

Dim SOURCE As Worksheet
Dim TARGET As Worksheet


Set SOURCE = ThisWorkbook.Worksheets("Data")
Set TARGET =

SOURCE.Range("A:H").Copy Destination:=TARGET.Range("A1")



I know what I have here probably isn't close to what is needed, any insights you can provide would be greatly appreciated.

-Jay
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
So M5 contains the name of the sheet where you wish to copy to? In that case, try
VBA Code:
Set TARGET = ThisWorkbook.Sheets(Source.Range("M5"))

Bye
 
Upvote 0
Thanks Anthony, unfortunately I get a debug pop-up and it shows this line of the code is in error.
 
Upvote 0
Good day all - thanks in advance for any assistance you can provide.

I'm working on a rather large workbook that has around 20 different sheets, with one master data sheet. What I'm trying to accomplish is to have a VB code that based upon 2 input cells will filter the data. I have already gotten the filtering code to work without any trouble. My input cells are M3 & M4.

With Worksheets("Data")
If .AutoFilterMode = True Then .AutoFilterMode = False

.Range("A8").AutoFilter Field:=8, Criteria1:=Array("=" & .Range("M3").Value)

.Range("A8").AutoFilter Field:=6, Criteria1:=Array("<" & .Range("M4").Value)

Once data is filtered it will then copy/paste to one of the 20 sheets that exist in this workbook. The part I'm struggling with is where to paste the data (columns A:H on the sheet called "Data"). On the "Data" sheet I've created a dropdown box (cell M5) that has all the existing SheetNames for the entire workbook listed by utilizing the following formula on a (=IFERROR(INDEX(SheetNames,R2),""), =IFERROR(INDEX(SheetNames,R3),""), etc). on a look-up reference sheet. This works great and all the sheets are listed w/ the exact name.

What I'd like to do is set a "TARGET" in the code for the location where the copied data can be pasted to based upon the value that exists in cell "M5".

I've done a lot of research over the last few days, and I'm struggling to come up with something.

Dim SOURCE As Worksheet
Dim TARGET As Worksheet


Set SOURCE = ThisWorkbook.Worksheets("Data")
Set TARGET =


SOURCE.Range("A:H").Copy Destination:=TARGET.Range("A1")



I know what I have here probably isn't close to what is needed, any insights you can provide would be greatly appreciated.

-Jay
Got the code figured out, did some additional digging and figured out a way to reference the value that existed in the "SheetName" Cell M5

ThisWorkbook.Sheets(Worksheets("Data").Range("M5").Value).Activate

then continued on w/ the code to paste the data

Range("A1").Select
Active.Sheet.Paste
 
Upvote 0
Solution

Forum statistics

Threads
1,213,537
Messages
6,114,216
Members
448,554
Latest member
Gleisner2

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