1

The following is a code section that I have in my VBA program:

Columns("currentColumn:(currentColumn+1)").Select

For some reason I have a type mismatch when I run this. currentColumn is an Integer. This is my first time using VBA so I'm sure I'm missing something. I was wondering if someone could point me towards the right track.

Thanks

bugsyb
  • 5,662
  • 7
  • 31
  • 47

1 Answers1

1

To elaborate on Scott's suggestion:

The problem is not only that Columns doesn't accept inputs like "2:3" (Rows would work with that but for Columns you'd need "B:C"). The problem is also that "currentColumn:(currentColumn+1)" is a string consisting of exactly these characters. The variable currentColumn" isn't replaced and the addition +1 isn't executed either. If you want to build a string with the number currentColumn and not the string "currentColumn you need to use &:

currentColumn & ":" & currentColumn + 1 'if currentColumn = 2 then this is the same as "2:3"

note that you usually don't need to Select objects. Here are some examples on how to avoid it.

Community
  • 1
  • 1
arcadeprecinct
  • 3,767
  • 1
  • 12
  • 18