Brett
Brett

Reputation: 19

In VBA, when I change the format of one column, the adjacent one changes too

I am trying to format two columns - one as "General" and one as "Number 0.000," but when the second one formats, it changes the format of the first column to "Number 0.000" as well. If I manually select the columns and change the format it works fine. Why is my macro changing the format of both columns?

Here is a code snippet:

Range("C:C").Select
Selection.NumberFormat = "General"
Range("D:D").Select
Selection.NumberFormat = "0.000"

Upvotes: 1

Views: 298

Answers (1)

BruceWayne
BruceWayne

Reputation: 23285

Try to avoid using .Select, it might cause issues. Try:

Range("C:C").NumberFormat = "General"
Range("D:D").NumberFormat = "0.000"

That works for me.

The reason it works if you manually select the range is because when you select a range, the .select part in your macro will use what is selected. Avoiding .select helps to ensure that no matter what cell/range you have selected, it will use the explicit range you want (in this case, Range("C:C") and Range("D:D")).

Upvotes: 2

Related Questions