0

Here is a minimal example of my Excel sheet:

enter image description here

GOAL: Column 1 and 2 will always be respectively "Number" and "Stand". I'm trying to rename the header in column C to the column left to the "TOTAL-column" (which can be in different positions). The names in the aforementioned range should be a counter, Such that the resulting header in the above example would be: Number, Stand, 1, 2, 3, 4, 5, 6, TOTAL.

ATTEMPT: This is what I got so far, however it isn't very efficient, to manually copy the replacement-line for each column, and it doesn't add a header in the empty columns

Sub Rename()
Dim WS As Worksheet
Dim MatchCase As Boolean
For Each WS In Worksheets
WS.Cells.Replace What:="1,", Replacement:="1", LookAt:=xlPart, MatchCase:=True
WS.Cells.Replace What:="2,", Replacement:="2", LookAt:=xlPart, MatchCase:=True
Next
End Sub

1 Answer 1

0

I'm curios why you are trying to do this with VBA. You could make the cell a formula that says =column()-2 or =column()-2&",18"

If you really wanted a vba solution, you could use vba to search for the "total" cell, and then make all cells to the left of it the above formula.

Sign up to request clarification or add additional context in comments.

1 Comment

It's a small part of a larger VBA. The position of the TOTAL column is unknown and the values in column C to H in the above example can have varying number of decimals but is formatted as a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.