Getting Column width using macro..

K

KM

I need some help creating a macro in MS Excel.

Say I have a worksheet with 20+ columns of various column
widths. All I need is to get the size of each column and
put it in a cell of the respective column.

If the cursor was placed on a row where the column
headings are available, I want the macro,

to get width of respective column and save it in the next
row
go to next column and do step 1
stop when no more columns are available (say it reaches
the empty cell)


After executing the macro, the Worksheet should look
something like this:

A B C D E
Name ID DOB Comment
15.14 8.43 11.14 25

Thanks in Advance for the help.
KM
 
S

Steve Slechta

I ran this code as a quick test on Column A


Columns("A:A").ColumnWidth = 15
Cells(1, 1).Select
ActiveCell = Selection.ColumnWidth


Hope that helps.

Steve
 
B

Bob Phillips

KM,

If Don's reply does not suit, this code will work out the last column and
add the column widths

Sub Widths()
Dim cLastCol As Long
Dim i As Long

cLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To cLastCol
Cells(2, i) = Columns(i).Width
Next

End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

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

Top