VBA & Macro Problems

Excel VBA: Copy a Range Using a Variable Column Number

Chanuka GeekiyanageChanuka Geekiyanage Nov 25, 2026 868 views

Question details

Copy rows 2 and 3 from column B through a variable ending column and paste the results as values.

Copy an Excel VBA Range Using a Variable Column Number
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
An Excel VBA procedure reads the ending column number from a calculated worksheet cell.
Observed behavior
The desired range should extend from B2 through row 3 of the column number in G26, without requiring a column-letter conversion.
Before you start

Make sure G26 calculates a whole column number of at least 2, and identify the worksheet and destination for the copied values. The example uses Sheet1 and H2; change these references to match your workbook.

Solution 1Recommended

Build the source range with Cells and paste values

Use a numeric column index directly in a worksheet-qualified range, then paste the copied results without formulas.

Cells(row, column) accepts a column number, so there is no need to convert the number in G26 to a letter. Both endpoints are qualified with the same worksheet.

1
Open the VBA editor

In Excel, open the Visual Basic editor from Developer > Visual Basic, then choose Insert > Module.

2
Add the macro

Paste the following code into the module: Sub CopyVariableRange() Dim ws As Worksheet Dim endCol As Long Dim v As Variant Dim src As Range Set ws = ThisWorkbook.Worksheets("Sheet1") v = ws.Range("G26").Value If IsError(v) Then MsgBox "G26 must contain a valid column number." Exit Sub End If If Not IsNumeric(v) Then MsgBox "G26 must contain a valid column number." Exit Sub End If If CDbl(v) < 2 Or CDbl(v) > ws.Columns.Count Then MsgBox "G26 must be between 2 and the last worksheet column." Exit Sub End If If CDbl(v) <> Fix(CDbl(v)) Then MsgBox "G26 must contain a whole number." Exit Sub End If endCol = CLng(v) Set src = ws.Range(ws.Cells(2, 2), ws.Cells(3, endCol)) src.Copy ws.Range("H2").PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False End Sub

3
Set the worksheet and destination

Replace "Sheet1" with the worksheet name and "H2" with the upper-left destination cell. Choose a destination with enough space for two rows and the full source width.

4
Run the macro

In Excel, choose Developer > Macros, select CopyVariableRange, and click Run. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you need to retain the macro.

Build the source range with Cells and paste values
Check for overlapping ranges: If the destination overlaps the source, pasting will overwrite cells in the source range. Choose a separate destination if those cells must remain unchanged.
Free Microsoft Office alternative

Work with spreadsheet ranges in WPS Office

WPS Office is a free, lightweight alternative for working with spreadsheets in a familiar interface. Open compatible Excel files and migrate everyday spreadsheet work easily; it does not fix VBA issues in Microsoft Excel.

Copy spreadsheet ranges and paste values through the interfaceCompatible with Microsoft Excel formats such as XLSX and XLSFamiliar spreadsheet tools for an easier transition from ExcelFree and lightweight for everyday office work
microsoft office alternative - wps office

Frequently Asked Questions

Do I need to convert the column number to a letter?

No. Use ws.Cells(3, endCol) as the ending cell in ws.Range(ws.Cells(2, 2), ws.Cells(3, endCol)). For example, column number 28 refers to column AB.

How can I assign the values without using Copy and PasteSpecial?

After setting src, use ws.Range("H2").Resize(src.Rows.Count, src.Columns.Count).Value = src.Value. This writes the source values to a destination of the same dimensions without using the clipboard.

What if G26 contains a column letter instead of a number?

For a valid column letter in G26, you can obtain its number with endCol = ws.Range(CStr(ws.Range("G26").Value) & "1").Column. Validate the input and ensure the resulting column is at least B before building the range.

Why should I qualify both Cells references with the worksheet?

Using ws.Range(ws.Cells(2, 2), ws.Cells(3, endCol)) ensures that both endpoints belong to the intended worksheet instead of depending on whichever sheet is active.