How should i write this loop?

myespooir

Board Regular
Joined
Jul 24, 2015
Messages
52
Hi there,

I would like to write the following loop, could you give me some idea how to write it?

I need the macro to copy columns with the same region in the mater workbook to another workbook.

For example, the macro will copy the column A to C to a new workbook named EAST.
then the macro will come back to the master workbook and copy column A and column D to G to a new workbook named SOUTH... until all the region row =""
Column ABCDEFGHIJ
Name
RegionEASTEASTSOUTHSOUTHSOUTHSOUTHNORTHNORTHWEST
...
...

<tbody>
</tbody>

I am thinking about calculating columns and using x and y to loop through it, but not sure how to do it. Could you help me with this?

Code:
Range(Cells(1, x), Cells(800, y)).Copy


Thanks a million!!

Christina
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Christina,

Assuming the Region names are in the first row, you can use the following code:

Code:
Sub copyColumns()


    Dim iCol As Integer 'counter for the column number
    Dim curReg As String 'current Region
    Dim Sht As Worksheet 'the main worksheet
    Dim iDest As Integer 'the column number to append to in new sheet
    
    Set Sht = ActiveSheet
    
    'Start at column B, loop until Region is empty
    iCol = 2
    While Not Sht.Cells(1, iCol) = ""
    
        curReg = Sht.Cells(1, iCol)
    
        'if the worksheet doesnt exist, create and copy column A with current Region
        If Not WorksheetExists(curReg) Then
        
            Sheets.Add
            ActiveSheet.Name = curReg
            Sht.Range("A:A").Copy Destination:=ActiveSheet.Range("A:A")
            Sht.Cells(1, iCol).EntireColumn.Copy Destination:=ActiveSheet.Range("B:B")
        
        'else, copy current Region to the existing sheet
        Else
        
            iDest = Worksheets(curReg).UsedRange.Columns.Count + 1
            Sht.Cells(1, iCol).EntireColumn.Copy Destination:=ActiveSheet.Cells(1, iDest)
        
        End If
    
        iCol = iCol + 1
    
    Wend


End Sub


Function WorksheetExists(ByVal WorksheetName As String) As Boolean


    Dim Sht As Worksheet
    
    For Each Sht In ThisWorkbook.Worksheets
        If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then
            WorksheetExists = True
            Exit Function
        End If
    Next Sht
    
    WorksheetExists = False
    
End Function

Regards,
Zach
 
Upvote 0
Hi Zach,

Thank you so much for your quick reply! There are some column changes (see table below) and new info. Could you please help with the following changes?

- Column A to Column AE are basic product info.
- Column AF to ... are the purchase quantity by store.
- Region is on the 9th row.

Regardless the region, Column A to AE need to be copied to the new sheet. From Column AF on, the macro needs to copy the columns with the same region over to the new workbook.

Column ABAEAFAGAHAIAJAK
Store Name123456
Region
EASTEASTEASTSOUTHNORTHNORTH
ProductsPrice
Min. Quan
001
002
70
60
5
8
10
16
25
60
...

<tbody>
</tbody>

There are 2 more sheets (named Summary and Final Format) in the master workbook be to copied over to the new workbook along with the first region sheet. Can the Macro copy these 2 sheets as well?

Once the new workbook is created, can the Macro also save it as well (on desktop), and name the workbook with the region?

Thanks a million!!
Christina
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,825
Members
449,470
Latest member
Subhash Chand

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