Forum Discussion

Mike_42's avatar
Mike_42
Copper Contributor
Jun 14, 2024

Looping a macro

I have a macro that copies/pastes then deletes columns of data, it then copies/pastes the new data at the end of the previous set.

How can I make this macro loop until all the data is processed, ie column G in my case?

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Mike_42 

    To create a macro that loops through all columns until column G and processes each set of data, you can use a loop structure within your VBA code. Below is an example of how you can achieve this. The macro will continue to copy, paste, and delete columns of data until all data in column G is processed.

    Example Macro Code

    Vba Code is untested, backup your file.

    Sub ProcessDataUntilColumnG()
        Dim ws As Worksheet
        Dim lastCol As Long
        Dim col As Long
    
        ' Define the worksheet to work on
        Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet's name
    
        ' Find the last column with data
        lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    
        ' Loop through columns starting from the first data column (e.g., column A)
        For col = 1 To lastCol
            ' If the current column is G (7), exit the loop
            If ws.Cells(1, col).Column = 7 Then
                Exit For
            End If
    
            ' Perform your copy, paste, and delete operations here
            ' Example: Copy column A, paste to another location, then delete column A
            ws.Columns(col).Copy
            ws.Columns("Z").PasteSpecial xlPasteValues ' Change "Z" to your target column
    
            ' Optionally, you can delete the column after processing
            ' ws.Columns(col).Delete
    
            ' Update lastCol since deleting a column reduces the column count
            lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        Next col
    
        ' Clean up
        Application.CutCopyMode = False
        MsgBox "Data processing completed!"
    
    End Sub

    Explanation of the Code

    1. Define the Worksheet:

    • Set ws = ThisWorkbook.Sheets("Sheet1"): Sets the worksheet where the macro will operate. Change "Sheet1" to your actual sheet name.

    2. Find the Last Column with Data:

    • lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column: Finds the last column with data in the first row.

    3. Loop Through Columns:

    • For col = 1 To lastCol: Loops through each column from the first column to the last detected column.
    • If ws.Cells(1, col).Column = 7 Then Exit For: Exits the loop if the current column is column G (7).

    4. Perform Copy, Paste, and Delete Operations:

    • ws.Columns(col).Copy: Copies the current column.
    • ws.Columns("Z").PasteSpecial xlPasteValues: Pastes the copied data to column Z. Change "Z" to your desired target column.
    • Optionally, you can delete the column after processing with ws.Columns(col).Delete.
    • Updates lastCol since deleting a column reduces the column count.

    5. Clean Up and Notify:

    • Application.CutCopyMode = False: Clears the clipboard.
    • MsgBox "Data processing completed!": Displays a message box indicating that the processing is complete.

    How to Use:

    1. Open the VBA Editor:

    • Press Alt + F11 to open the VBA editor.

    2. Insert the VBA Code:

    • In the VBA editor, insert a new module by right-clicking on any existing module or the workbook name, then choose Insert > Module.
    • Copy and paste the provided VBA code into the new module.

    3. Run the Macro:

    • Close the VBA editor and return to your Excel sheet.
    • Press Alt + F8, select ProcessDataUntilColumnG, and click Run.

    This macro will loop through all columns and process the data until it reaches column G, performing the specified copy, paste, and delete operations. Adjust the target column and sheet names as necessary for your specific use case. The text, steps and code were created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources