ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using variables to reference columns in VBA (https://www.excelbanter.com/excel-programming/299225-using-variables-reference-columns-vba.html)

aktiv

Using variables to reference columns in VBA
 
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

--
Message posted from http://www.ExcelForum.com


mudraker[_233_]

Using variables to reference columns in VBA
 
aktiv

Try

txtColumnStart = "A"
txtColumEnd = "C"
Columns(txtColumnStart & ":" & txtColumEnd).ColumnWidth =
txtColumnWidth


---
Message posted from http://www.ExcelForum.com/


aktiv[_2_]

Using variables to reference columns in VBA
 
I knew that it would be something simple!

That did the trick - thanks a lot.

Keith

--
Message posted from http://www.ExcelForum.com


Chris

Using variables to reference columns in VBA
 
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


--
Message posted from http://www.ExcelForum.com




All times are GMT +1. The time now is 09:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com