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
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