Using variables to reference columns in VBA

  • Thread starter Thread starter aktiv
  • Start date Start date
A

aktiv

I need to be able to change the width of a contiguous selection o
columns. I know how to do this with pre-determined columns with th
user-specified width "txtColumnWidth":> Columns("A:E").ColumnWidth = txtColumnWidthHowever, in this case the columns are specifed via the user in a for
using the variables "txtColumnStart" and "txtColumnEnd". I would thin
that> Columns(txtColumnStart, txtColumEnd).ColumnWidth = txtColumnWidthwould be the solution, but I get a "Run-time error '1004'
Application-defined or object-defined error" message.

How does one specify column sets from variables for this kind o
activity?

Thanks!

Keith
 
aktiv

Try

txtColumnStart = "A"
txtColumEnd = "C"
Columns(txtColumnStart & ":" & txtColumEnd).ColumnWidth =
txtColumnWidth
 
I knew that it would be something simple!

That did the trick - thanks a lot.

Keith
 
You can also use numbers to identify your columns
x =
y = 1
ActiveSheet.Range(Columns(x), Columns(y)).ColumnWidth = txtColumnWidt

----- aktiv > wrote: ----

I need to be able to change the width of a contiguous selection o
columns. I know how to do this with pre-determined columns with th
user-specified width "txtColumnWidth":> Columns("A:E").ColumnWidth = txtColumnWidthHowever, in this case the columns are specifed via the user in a for
using the variables "txtColumnStart" and "txtColumnEnd". I would thin
that> Columns(txtColumnStart, txtColumEnd).ColumnWidth = txtColumnWidthwould be the solution, but I get a "Run-time error '1004'
Application-defined or object-defined error" message

How does one specify column sets from variables for this kind o
activity

Thanks

Keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top