Lambrix

Board Regular
Joined
Jun 29, 2012
Messages
62
So my code is working great, but looking at my code I can't help but think that there must be an easier way to loop through the script so it isn't so repetitive. Any thoughts?

Code:
Sub Macro1()
Dim ActSheet As Worksheet
Dim JUNCTION As String
Dim BOC As String
Dim Amount As String

Sheets("Aggregate").Activate
 
Set ActSheet = ActiveSheet

Sheets("National").Activate
Range("O4").Activate
      
Sheets("JUNCTION 1").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 2").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 3").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 4").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 5").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 6").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 7").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 8").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 9").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 10").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 11").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 12").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 15").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 16").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 17").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 18").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 19").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 20").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 21").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 22").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION 23").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Sheets("JUNCTION - OBS").Activate
Range("O4").Activate
Amount = Range("O4").Value
BOC = ActiveCell.Offset(0, -12).Value
JUNCTION = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Lambrix

Board Regular
Joined
Jun 29, 2012
Messages
62
Figured out an easier solution :

<code>
Sub AllWorkSheets()

Dim ActSheet As Worksheet
Dim VISN As String
Dim BOC As String
Dim Amount As String
Sheets("Aggregate").Activate
Set ActSheet = ActiveSheet
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Select

Range("O5").Activate
Amount = Range("O5").Value
BOC = ActiveCell.Offset(0, -12).Value
VISN = ActiveCell.Offset(0, -14).Value
ActSheet.Select
Range("B1").Value = BOC
Range("A1").Value = "JUNCTION"
Cells(Rows.Count, 1).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Value = JUNCTION
ActiveCell.Offset(0, 1) = Amount

Next

End Sub

</code>
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,964
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Consider for a real simplication:

Code:
Sub AllWorkSheets()
    With Sheets("Aggregate")
        .Cells(1).Resize(, 2).Value = Array("JUNCTION", "BOC")
        For Each ws In Sheets
            .Cells(.Rows.Count, 1).End(xlUp)(2).Resize(, 2).Value = Array(ws.Range("C5").Value, ws.Range("O5").Value)
        Next
    End With
End Sub

I am quite sure my code does not grab the correct cells, but that's only because your code is rather unclear.
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,587
Messages
5,765,300
Members
425,271
Latest member
kristyfinn

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
Top