For/Next Loop Iteration Based on Specific Columns

zdodson

Board Regular
Joined
Feb 29, 2012
Messages
124
All,

Below is the code I am working with
VBA Code:
Sub ExtractBST()

Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim Current As Worksheet
Dim I As Long

Application.ScreenUpdating = False

'set variables for copy and destination worksheets
Set wsCopy = Workbooks("TRAINING.xlsm").Worksheets("Marines")
Set wsDest = Workbooks.Add.Worksheets(1)

'1. FIND LAST ROW IN THE COPY RANGE BASED ON DATA IN COLUMN B
    lCopyLastRow = wsCopy.Cells(Rows.Count, "B").End(xlUp).Row

'2. FIND LAST ROW IN THE DESTINATION RANGE BASED ON DATA IN COLUMN A
    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row

'3. COPY & PASTE DATA

    For I = 1 To 28

        wsCopy.Range("A1:F" & lCopyLastRow).Copy _
            Sheets(I).Range("A" & lDestLastRow)
       
       [B][COLOR=rgb(226, 80, 65)] wsCopy.Range("AJ1:AJ" & lCopyLastRow).Copy _[/COLOR][/B]
            Sheets(I).Range("G" & lDestLastRow)
       
        ActiveWorkbook.Sheets.Add AFTER:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
       
    Next I
   
'4. FOR/NEXT LOOP TO FORMAT THE REPORT

    For Each Current In Worksheets
        Current.Activate
        Call wsFormatBST

    Next

    Worksheets(1).Select
   
Application.ScreenUpdating = True

For this portion of the code, my origin column begins in column AJ. I want to iterate from column AJ through column BM, copying and pasting the data from the origin (wsCopy) to its destination (wsDest).

VBA Code:
wsCopy.Range("AJ1:AJ" & lCopyLastRow).Copy _[/B]
            Sheets(I).Range("G" & lDestLastRow)

Any help would be greatly appreciated. Thank you in advance!
 
Try
Rich (BB code):
wsCopy.Range(wsCopy.Cells(1, i + 35), wsCopy.Cells(lCopyLastRow, i + 35)).Copy _
        Sheets(i).Range("G2")
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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