How to Loop Through Excel Columns with VBA Until Column G
Question details
The user needs to create a VBA macro in Excel that loops through columns to perform actions like copying, pasting, or processing data, and automatically stops execution when it reaches Column G.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating repetitive column operations using a VBA macro with a specific stopping point to prevent processing unwanted data.
- Observed behavior
- The user wants to successfully process columns sequentially and automatically terminate the loop exactly at Column G.
Before running any VBA script, ensure you have enabled the Developer tab in your spreadsheet software and determine exactly which data operations (like copying or deleting) need to happen inside the loop.
Use a For Loop and Exit Statement to Process Until Column G
Implement a VBA For Loop that increments the column index and uses an 'Exit For' statement when the column index reaches 7 (Column G).
This method iterates through each column starting from the first one. By checking if the current column number equals 7 (which corresponds to Column G), the code knows exactly when to stop processing and exit the loop safely.
Press 'Alt + F11' on your keyboard to launch the Visual Basic for Applications (VBA) Editor.
In the top menu, click on 'Insert' and select 'Module' to create a blank workspace for your script.
Paste the following script into the module window: Sub ProcessDataUntilColumnG() Dim ws As Worksheet, lastCol As Long, col As Long Set ws = ThisWorkbook.Sheets("Sheet1") lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column For col = 1 To lastCol If col = 7 Then Exit For ws.Columns(col).Copy ws.Columns("Z").PasteSpecial xlPasteValues Next col Application.CutCopyMode = False MsgBox "Data processing completed!" End Sub
Modify the 'ws.Columns(col).Copy' and 'ws.Columns("Z").PasteSpecial' lines to match the specific copy, paste, or delete actions you need for your workbook.
Press 'F5' or click the green 'Run' button in the toolbar to execute the macro. The script will process columns A through F and stop at G.

Use WPS Spreadsheet to Run Macros and Automate Tasks
WPS Office provides excellent support for VBA macros, allowing you to automate repetitive tasks like looping through columns with ease. Enjoy a highly compatible and lightweight spreadsheet software.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open your .xlsm or .xls file.
- 2. Navigate to Developer Tools: Click on the 'Developer' tab on the top ribbon menu.
- 3. Access the VBA Editor: Click 'Visual Basic' to open the code editor.
- 4. Run the Loop Script: Paste your column-looping VBA code into a module and execute it effortlessly.

Frequently Asked Questions
How do I loop through rows instead of columns in VBA?
To loop through rows, change your variable to iterate over row indices (e.g., 'For i = 1 To 10') and use 'ws.Rows(i)' or 'ws.Cells(i, 1)' instead of 'ws.Columns(col)'.
Why isn't my VBA macro stopping at Column G?
Ensure your loop includes the conditional statement 'If col = 7 Then Exit For' BEFORE any processing actions. If the processing action occurs before the condition is checked, Column G will still be processed.
How can I reference Column G by its letter instead of the number 7?
While using the numeric index (7) is faster in a standard For loop, you can reference a column letter by comparing the column address string, such as extracting the letter using 'Split(Cells(1, col).Address(True, False), "$")(0)' and checking if it equals "G".




