Statement to apply code to certain worksheets with certain names. Trouble with if statement

colewillm

New Member
Joined
Oct 8, 2014
Messages
6
Hi everyone,

I'm trying to write a macro to look at all of the worksheets in a workbook and apply a certain but similar code to the worksheets depending on the worksheet name. One thing to note is the worksheets name has a possibility of being different, but still will contain a unique identifier somewhere in the name, such as the name of the work sheet could be GL, xxxx GL xxxx, GL xxxx, or xxxx GL with the xxxx representing some other word. Right now my code does not perform anything. The names of the worksheets at the moment are GL PL, AL, and ER

Here is my example code. The macro beneath the if statement works properly. I just need help using them to the right worksheets. Thanks

Application.Workbooks.Open (Analysis.Path & "\" & datarec & ".xlsm")
Set datarec2 = ThisWorkbook
Dim ws As Worksheet
For Each ws In datarec2.Worksheets


If ws.Name Like "* GL *" Or ws.Name Like "GL" Then

Range("N115").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Range(ActiveCell.Offset(0, -13), ActiveCell.Offset(50, -13)).Copy
Analysis.Sheets("Large Losses - GL").Range("c9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -12), ActiveCell.Offset(50, -12)).Copy
Analysis.Sheets("Large Losses - GL").Range("d9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -11), ActiveCell.Offset(50, -11)).Copy
Analysis.Sheets("Large Losses - GL").Range("o9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -10), ActiveCell.Offset(50, -10)).Copy
Analysis.Sheets("Large Losses - GL").Range("a9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -9), ActiveCell.Offset(50, -9)).Copy
Analysis.Sheets("Large Losses - GL").Range("e9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -7), ActiveCell.Offset(50, -7)).Copy
Analysis.Sheets("Large Losses - GL").Range("p9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 2), ActiveCell.Offset(50, 2)).Copy
Analysis.Sheets("Large Losses - GL").Range("f9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 3), ActiveCell.Offset(50, 3)).Copy
Analysis.Sheets("Large Losses - GL").Range("g9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 6), ActiveCell.Offset(50, 6)).Copy
Analysis.Sheets("Large Losses - GL").Range("h9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 5), ActiveCell.Offset(50, 5)).Copy
Analysis.Sheets("Large Losses - GL").Range("j9").PasteSpecial xlPasteValues
End If


If ws.Name Like ("* AL *") Or ws.Name Like "AL" Then


Range("N115").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Range(ActiveCell.Offset(0, -13), ActiveCell.Offset(50, -13)).Copy
Analysis.Sheets("Large Losses - AL").Range("c9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -12), ActiveCell.Offset(50, -12)).Copy
Analysis.Sheets("Large Losses - AL").Range("d9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -11), ActiveCell.Offset(50, -11)).Copy
Analysis.Sheets("Large Losses - AL").Range("o9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -10), ActiveCell.Offset(50, -10)).Copy
Analysis.Sheets("Large Losses - AL").Range("a9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -9), ActiveCell.Offset(50, -9)).Copy
Analysis.Sheets("Large Losses - AL").Range("e9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -7), ActiveCell.Offset(50, -7)).Copy
Analysis.Sheets("Large Losses - AL").Range("p9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 2), ActiveCell.Offset(50, 2)).Copy
Analysis.Sheets("Large Losses - AL").Range("f9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 3), ActiveCell.Offset(50, 3)).Copy
Analysis.Sheets("Large Losses - AL").Range("g9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 6), ActiveCell.Offset(50, 6)).Copy
Analysis.Sheets("Large Losses - AL").Range("h9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 5), ActiveCell.Offset(50, 5)).Copy
Analysis.Sheets("Large Losses - AL").Range("j9").PasteSpecial xlPasteValues
End If


If ws.Name Like "* EL *" Or ws.Name Like "EL" Or ws.Name Like "* WC *" Or ws.Name Like "WC" Or ws.Name Like "* ER *" Or ws.Name = "ER" Then


Range("N115").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Range(ActiveCell.Offset(0, -13), ActiveCell.Offset(50, -13)).Copy
Analysis.Sheets("Large Losses - WC").Range("c9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -12), ActiveCell.Offset(50, -12)).Copy
Analysis.Sheets("Large Losses - WC").Range("d9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -11), ActiveCell.Offset(50, -11)).Copy
Analysis.Sheets("Large Losses - WC").Range("o9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -10), ActiveCell.Offset(50, -10)).Copy
Analysis.Sheets("Large Losses - WC").Range("a9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -9), ActiveCell.Offset(50, -9)).Copy
Analysis.Sheets("Large Losses - WC").Range("e9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, -7), ActiveCell.Offset(50, -7)).Copy
Analysis.Sheets("Large Losses - WC").Range("p9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 2), ActiveCell.Offset(50, 2)).Copy
Analysis.Sheets("Large Losses - WC").Range("f9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 3), ActiveCell.Offset(50, 3)).Copy
Analysis.Sheets("Large Losses - WC").Range("g9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 6), ActiveCell.Offset(50, 6)).Copy
Analysis.Sheets("Large Losses - WC").Range("h9").PasteSpecial xlPasteValues
Range(ActiveCell.Offset(0, 5), ActiveCell.Offset(50, 5)).Copy
Analysis.Sheets("Large Losses - WC").Range("j9").PasteSpecial xlPasteValues
End If
Next ws

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
.
.

Change the beginning code to:

Set datarec2 = Application.Workbooks.Open(Analysis.Path & "\" & datarec & ".xlsm")

Otherwise you're going to perform the routine on the workbook containing your code rather than the workbook you're opening...
 
Upvote 0
Try changing this:


Set datarec2 = ThisWorkbook

to this:

Set datarec2 = ActiveWorkbook
 
Upvote 0
Maybe it would make more sense if I'd have put the very beginning part. Sorry.

Sub Populate_Large_Losses()
Dim datarec As String
Dim Analysis As Workbook
Set Analysis = ThisWorkbook

datarec = InputBox("Please enter the data rec file name")


Dim datarec2 As Workbook
Application.Workbooks.Open (Analysis.Path & "\" & datarec & ".xlsm")
Set datarec2 = ThisWorkbook
Dim ws As Worksheet
For Each ws In datarec2.Worksheets
If ws.Name Like "* GL *" Or ws.Name Like "GL" Then

I'm really just having trouble with the if statements. I've never used Like and don't know how to properly use if I'm even using it correctly. Don't know if the macro is looking up what I intend it to lookup.
 
Upvote 0
It was actually only cross posted once and that was by accident. This is my first time using this forum and I didn't realize that this forum is connected to other forums through other websites. The other thread was already closed by an admin, so please let my question be answered. It won't happen again. I'm just very new to this site.
 
Upvote 0
It was actually only cross posted once and that was by accident. This is my first time using this forum and I didn't realize that this forum is connected to other forums through other websites. The other thread was already closed by an admin, so please let my question be answered. It won't happen again. I'm just very new to this site.
Your problem remains the same - you run the macro from a specific workbook which is 'Thisworkbook', it opens another workbook which you want to set to the variable 'datarec2', but instead your code sets 'datarec2' to 'Thisworkbook'. Most likely that is why nothing is happening when you run the code. Make either of the changes suggested in posts #2 or 3, and then post back if you have additional problems.
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,100
Members
452,301
Latest member
QualityAssurance

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