VBA & Macro Problems

Copy a Reference Value Across Variable Excel Cells with VBA

Jason MillerJason Miller Dec 2, 2026 869 views

Question details

Use VBA to read a reference value and copy it across a variable number of adjacent cells.

Copy a Reference Value Across Variable Excel Cells with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet contains reference values in column S, and each value determines how many cells to fill horizontally in the same row.
Observed behavior
Each reference value should be copied into a horizontal range whose width is determined by the rounded value, subject to a minimum and maximum.
Before you start

Check that S104:S108 contains numeric values and that T104:Y108 can be overwritten. Open the workbook in a desktop version of Excel that supports VBA and save it as an Excel Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Use a VBA loop and Resize to fill each row

Use this macro to copy each value in S104:S108 into one to six cells, starting in column T.

The macro rounds each reference value away from zero to a whole number, then limits the fill count to one through six. It copies the original value, not the rounded count.

1
Open the VBA editor

In desktop Excel, open the workbook, then open the Visual Basic editor from Developer > Visual Basic. In the editor, select Insert > Module.

2
Add the macro

Paste this code into the module: Sub Copiar() Dim ws As Worksheet Dim r As Long, n As Long Dim previousScreenUpdating As Boolean Set ws = ActiveSheet previousScreenUpdating = Application.ScreenUpdating On Error GoTo CleanUp Application.ScreenUpdating = False For r = 104 To 108 n = Application.RoundUp(ws.Range("S" & r).Value, 0) If n <= 0 Then n = 1 ElseIf n > 6 Then n = 6 End If ws.Range("T" & r).Resize(1, n).Value = ws.Range("S" & r).Value Next r CleanUp: Application.ScreenUpdating = previousScreenUpdating If Err.Number <> 0 Then MsgBox "The macro stopped: " & Err.Description End Sub

3
Run the macro

Return to Excel and select the worksheet you want to update. Choose Developer > Macros, select Copiar, and click Run.

4
Check the filled ranges

Inspect columns T through Y on rows 104 through 108. For example, a value of 1.2 in S104 fills T104:U104 with 1.2; a value of 0 fills T104.

Use a VBA loop and Resize to fill each row
Existing cells are overwritten: The macro writes to the selected range without clearing cells beyond it. If a row was previously filled farther to the right, those older values remain.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files and continue working with your data as you move between office suites. The VBA macro in this article is an Excel solution; macro support in WPS can vary by edition and platform.

Edit spreadsheet data in a familiar interfaceOpen and save Microsoft Excel .xlsx filesWork with reference values and adjacent cells in WPS SpreadsheetsUse a free, lightweight office suite
QA img-9

Frequently Asked Questions

How do I change the rows or starting column?

In the VBA editor, change 104 and 108 in the For loop to your first and last row. Change "S" to the reference column and "T" to the first destination column.

How do I allow more than six destination cells?

In the macro, change both instances of 6 in the ElseIf block to your desired maximum. Make sure the corresponding destination cells are available to overwrite.

Why does the macro copy the original value instead of the rounded number?

Application.RoundUp calculates the number of cells to fill. The assignment to the resized range uses the original value from column S. To copy the rounded count instead, replace the value on the right side of the assignment with n.

Can I use a chained comparison such as 1 < n <= 6 in VBA?

Use separate comparisons joined with And instead: If n > 1 And n <= 6 Then. Chained comparisons do not test the range as intended in VBA.

Why are old values still visible to the right of the new fill?

The macro overwrites only the cells in the current resized range. If the new fill is shorter than a previous one, clear the unwanted cells in columns T through Y before running it again.