Fill right VBA is skipping over a column

gaudrco

Board Regular
Joined
Aug 16, 2019
Messages
203
I have a command button that adds a column to a table. The table has formulas that need to be filled to the right with each new column added. Here is the vba that I have been using to achieve this:

Code:
Dim ws As Worksheet, oLo As ListObject


Set ws = Sheets("Competitor Comparison")
With ws
    Set oLo = .ListObjects("CompComparisonTable")
    With oLo
        .ListColumns.Add
        oLo.HeaderRowRange.Cells(1, .ListColumns.Count).Value = Range("C57")
        ws.Range(.ListColumns(.ListColumns.Count - 1).DataBodyRange, .ListColumns(.ListColumns.Count).DataBodyRange).FillRight
    End With
End With


The fill command is not behaving correctly. When I click the button to add a new column, the new column does not fill with the correct column starting point.


Here are the reference column formulas:
=IFERROR(INDEX('Competitor Comparison Data'!T:T,MATCH($B$3,'Competitor Comparison Data'!$D:$D,0)+ROWS($A$8:$A8)-1),"No Match")
=IFERROR(INDEX('Competitor Comparison Data'!U:U,MATCH($B$3,'Competitor Comparison Data'!$D:$D,0)+ROWS($A$8:$A8)-1),"No Match")


Here is the new column formula that is filled into the new column when the button is clicked:
=IFERROR(INDEX('Competitor Comparison Data'!W:W,MATCH($B$3,'Competitor Comparison Data'!$D:$D,0)+ROWS($A$8:$A8)-1),"No Match")


So it skipped over V:V and went to W:W. Additionally, when I add a second column, the first added column changes from W:W to X:X.


Once I add a column, formulas in existing columns should not change.


So two issues.
1. The first column added is not starting with the correct reference column; its skipping over V:V
2. Every new column added changes the reference column of all the other added columns. If I added 3 columns, the order of my last 4 columns would be as follows:


=IFERROR(INDEX('Competitor Comparison Data'!U:U,MATCH($B$3,'Competitor Comparison Data'!$D:$D,0)+ROWS($A$8:$A8)-1),"No Match") Correct Column
=IFERROR(INDEX('Competitor Comparison Data'!Y:Y,MATCH($B$3,'Competitor Comparison Data'!$D:$D,0)+ROWS($A$8:$A8)-1),"No Match")
=IFERROR(INDEX('Competitor Comparison Data'!Z:Z,MATCH($B$3,'Competitor Comparison Data'!$D:$D,0)+ROWS($A$8:$A8)-1),"No Match")
=IFERROR(INDEX('Competitor Comparison Data'!AA:AA,MATCH($B$3,'Competitor Comparison Data'!$D:$D,0)+ROWS($A$8:$A8)-1),"No Match")


This problem arose from this thread: https://www.mrexcel.com/forum/excel...mulas-over-right-into-new-column-added-4.html
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I don't think this is a vba issue but perhaps if the vba code was altered to accomplish the same thing, then maybe the fill would work properly.
 
Upvote 0
I think I figured it out and I will share what I did in case anyone runs into a similar problem.

My actual code is much larger than the one I shared above. The way I solved this issue was by moving the line of code (that fills the column to the right) farther down in my code. By doing this, I believe it gave Excel more time to process the adjustment of adding a column.
 
Upvote 0
By doing this, I believe it gave Excel more time to process the adjustment of adding a column.

I can't see how that would be the reason, much more likely there is something in the code that you have moved it below that affected it.
 
Upvote 0
It could also be that I separated the code into two pieces. At first, I had this:
Code:
[COLOR=#333333]Dim ws As Worksheet, oLo As ListObject[/COLOR]

Set ws = Sheets("Competitor Comparison")
With ws
    Set oLo = .ListObjects("CompComparisonTable")
    With oLo
        .ListColumns.Add
        oLo.HeaderRowRange.Cells(1, .ListColumns.Count).Value = Range("C57")
        ws.Range(.ListColumns(.ListColumns.Count - 1).DataBodyRange, .ListColumns(.ListColumns.Count).DataBodyRange).FillRight
    End With [COLOR=#333333]End With[/COLOR]

Now I have this:
Code:
Dim ws As Worksheet, oLo As ListObject

Set ws = Sheets("Competitor Comparison")
With ws
    Set oLo = .ListObjects("CompComparisonTable")
    With oLo
        .ListColumns.Add
        oLo.HeaderRowRange.Cells(1, .ListColumns.Count).Value = Range("C57")
    End With
End With

'Other code in between here, including a msgbox

Set ws = Sheets("Competitor Comparison")
With ws
    Set oLo = .ListObjects("CompComparisonTable")
    With oLo
ws.Range(.ListColumns(.ListColumns.Count - 1).DataBodyRange, .ListColumns(.ListColumns.Count).DataBodyRange).FillRight
    End With
End With
 
Upvote 0
By doing this, I believe it gave Excel more time to process the adjustment of adding a column.
To see if was a timing thing with your original "Competitor Comparison" sheet, I added a DoEvents, and a 5 second wait to the macro before doing the auto fill and it made no difference.

I even wrote the actual r1c1 formulas to the new column instead of auto filling and Excel still changed the formulas that should have been W:W to X:X.

"Other code in between here, including a msgbox"
Why going to a different sheet for a couple of manipulations, then returning to "Competitor Comparison" to auto fill the formulas is working is beyond me.

Anyway, glad you're now happy.
Good luck with your project.
 
Upvote 0

Forum statistics

Threads
1,223,942
Messages
6,175,544
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top