VBA & Macro Problems

How to Loop Through Excel Columns with VBA Until Column G

Kushani NimanthikaKushani Nimanthika Nov 11, 2026 869 views

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.

How to Loop Through Excel Columns with VBA Until 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 you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press 'Alt + F11' on your keyboard to launch the Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

In the top menu, click on 'Insert' and select 'Module' to create a blank workspace for your script.

3
Paste the VBA Code

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

4
Customize the Operations

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.

5
Run the Macro

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 a For Loop and Exit Statement to Process Until Column G
Column Index Reference: In VBA, column 'A' corresponds to index 1. Counting alphabetically, column 'G' is index 7. This is why 'If col = 7' is used as the stopping condition.
Efficient Spreadsheet Automation

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. 1. Open Your Workbook: Launch WPS Spreadsheet and open your .xlsm or .xls file.
  2. 2. Navigate to Developer Tools: Click on the 'Developer' tab on the top ribbon menu.
  3. 3. Access the VBA Editor: Click 'Visual Basic' to open the code editor.
  4. 4. Run the Loop Script: Paste your column-looping VBA code into a module and execute it effortlessly.
Fully compatible with Microsoft Excel macro formats (.xlsm, .xls)Seamlessly run and edit VBA macros without rewriting your codeLightweight, fast-loading, and easy on system resources
microsoft office alternative - wps office

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".