format macro

G

Guest

Last week, Art was trying to help me with this, but the solution doesn't work
for me. I'm looking for a macro that would be contained within my
personal.xls, that I could use as needed.

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march. [On a different spreadsheet, it may be different columns, or
a different number of columns. The point is that I will select the range
before I run the macro.]

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does an
autofit on the column widths, checks the column width of all the columns
within the selection, and then makes all the columns the same width as the
largest column.

Is it possible to do this all within personal.xls and not have to add macros
to each spreadsheet that I use it on?
 
D

Dave Peterson

You could write your macro against the active worksheet. In fact, I think I'd
use the current selection on that activesheet, too. Then just select the
columns you want first and run the macro:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

Jonathan said:
Last week, Art was trying to help me with this, but the solution doesn't work
for me. I'm looking for a macro that would be contained within my
personal.xls, that I could use as needed.

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march. [On a different spreadsheet, it may be different columns, or
a different number of columns. The point is that I will select the range
before I run the macro.]

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does an
autofit on the column widths, checks the column width of all the columns
within the selection, and then makes all the columns the same width as the
largest column.

Is it possible to do this all within personal.xls and not have to add macros
to each spreadsheet that I use it on?

Jonathan Cooper said:
your not the first person to say that to me! If I could just find time to
learn VBA, I could drive myself crazy instead!

My intent was to insert this macro into my personal workbook, and assign it
to a button on my shortcut bar, so that I can use it as a utility for various
other spreadsheets.

Will this work for that purpose?
 
G

Guest

I had to make one change. The macro was making all the columns the same
width as the largest column, but it wasn't doing the autofit first.

This seems to work now.
' by Dave Peterson'
' On the selected range, this macro does an autofit, then looks to find the
largest column
' width in the selection, and then makes all the columns within that
selection the same
' width as the largest column.


Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

Selection.Columns.AutoFit
LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub




Dave Peterson said:
You could write your macro against the active worksheet. In fact, I think I'd
use the current selection on that activesheet, too. Then just select the
columns you want first and run the macro:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

Jonathan said:
Last week, Art was trying to help me with this, but the solution doesn't work
for me. I'm looking for a macro that would be contained within my
personal.xls, that I could use as needed.

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march. [On a different spreadsheet, it may be different columns, or
a different number of columns. The point is that I will select the range
before I run the macro.]

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does an
autofit on the column widths, checks the column width of all the columns
within the selection, and then makes all the columns the same width as the
largest column.

Is it possible to do this all within personal.xls and not have to add macros
to each spreadsheet that I use it on?

Jonathan Cooper said:
your not the first person to say that to me! If I could just find time to
learn VBA, I could drive myself crazy instead!

My intent was to insert this macro into my personal workbook, and assign it
to a button on my shortcut bar, so that I can use it as a utility for various
other spreadsheets.

Will this work for that purpose?

:

Jonathan, you're making this harder!

Okay, how about this:

Put the following macro in the sheet you're working on:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
x = Target.Columns.Address
End Sub

Now, put this in a separate module:

Public x As String

Sub DoColumns()
Dim w As Integer
Dim i As Integer
Dim r As Range
w = 0
Columns(x).AutoFit
For Each r In Range(x)
If r.ColumnWidth > w Then w = r.ColumnWidth
Next r
Columns(x).ColumnWidth = w
End Sub

This will only work on adjacent columns in that sheet. Select your columns.
Then, using Tools/Macro/Macros, run the DoColumns macro.

With any luck this will work.

Art


:

Can you change it so that it works, regardless of of the specific columns or
number of columns i have in my selection?

:

Jonathan,

Try this:

Sub temp()
Dim w As Integer
Dim i As Integer
w = 0
Columns("A:C").AutoFit
For i = 1 To 3
If Columns(i).ColumnWidth > w Then w = Columns(i).ColumnWidth
Next i
Columns("A:C").ColumnWidth = w
End Sub

Art

:

Here is what I would like to be able to do. This is really a
formatting/presentation issue:

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march.

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does and
autofit on the column widths and then makes all three columns the same width
as the largest column.

Ideas?
 
D

Dave Peterson

I would have just done it manually <vvbg>.

Glad you made it work.


Jonathan said:
I had to make one change. The macro was making all the columns the same
width as the largest column, but it wasn't doing the autofit first.

This seems to work now.
' by Dave Peterson'
' On the selected range, this macro does an autofit, then looks to find the
largest column
' width in the selection, and then makes all the columns within that
selection the same
' width as the largest column.

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

Selection.Columns.AutoFit
LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

Dave Peterson said:
You could write your macro against the active worksheet. In fact, I think I'd
use the current selection on that activesheet, too. Then just select the
columns you want first and run the macro:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

Jonathan said:
Last week, Art was trying to help me with this, but the solution doesn't work
for me. I'm looking for a macro that would be contained within my
personal.xls, that I could use as needed.

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march. [On a different spreadsheet, it may be different columns, or
a different number of columns. The point is that I will select the range
before I run the macro.]

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does an
autofit on the column widths, checks the column width of all the columns
within the selection, and then makes all the columns the same width as the
largest column.

Is it possible to do this all within personal.xls and not have to add macros
to each spreadsheet that I use it on?

:

your not the first person to say that to me! If I could just find time to
learn VBA, I could drive myself crazy instead!

My intent was to insert this macro into my personal workbook, and assign it
to a button on my shortcut bar, so that I can use it as a utility for various
other spreadsheets.

Will this work for that purpose?

:

Jonathan, you're making this harder!

Okay, how about this:

Put the following macro in the sheet you're working on:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
x = Target.Columns.Address
End Sub

Now, put this in a separate module:

Public x As String

Sub DoColumns()
Dim w As Integer
Dim i As Integer
Dim r As Range
w = 0
Columns(x).AutoFit
For Each r In Range(x)
If r.ColumnWidth > w Then w = r.ColumnWidth
Next r
Columns(x).ColumnWidth = w
End Sub

This will only work on adjacent columns in that sheet. Select your columns.
Then, using Tools/Macro/Macros, run the DoColumns macro.

With any luck this will work.

Art


:

Can you change it so that it works, regardless of of the specific columns or
number of columns i have in my selection?

:

Jonathan,

Try this:

Sub temp()
Dim w As Integer
Dim i As Integer
w = 0
Columns("A:C").AutoFit
For i = 1 To 3
If Columns(i).ColumnWidth > w Then w = Columns(i).ColumnWidth
Next i
Columns("A:C").ColumnWidth = w
End Sub

Art

:

Here is what I would like to be able to do. This is really a
formatting/presentation issue:

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march.

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does and
autofit on the column widths and then makes all three columns the same width
as the largest column.

Ideas?
 
G

Guest

One more question. I have another macro that does custom column headings.
I'd like that macro to run this new macro you created for me. What is the
code to get that macro to run this macro?

Dave Peterson said:
I would have just done it manually <vvbg>.

Glad you made it work.


Jonathan said:
I had to make one change. The macro was making all the columns the same
width as the largest column, but it wasn't doing the autofit first.

This seems to work now.
' by Dave Peterson'
' On the selected range, this macro does an autofit, then looks to find the
largest column
' width in the selection, and then makes all the columns within that
selection the same
' width as the largest column.

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

Selection.Columns.AutoFit
LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

Dave Peterson said:
You could write your macro against the active worksheet. In fact, I think I'd
use the current selection on that activesheet, too. Then just select the
columns you want first and run the macro:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

Jonathan Cooper wrote:

Last week, Art was trying to help me with this, but the solution doesn't work
for me. I'm looking for a macro that would be contained within my
personal.xls, that I could use as needed.

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march. [On a different spreadsheet, it may be different columns, or
a different number of columns. The point is that I will select the range
before I run the macro.]

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does an
autofit on the column widths, checks the column width of all the columns
within the selection, and then makes all the columns the same width as the
largest column.

Is it possible to do this all within personal.xls and not have to add macros
to each spreadsheet that I use it on?

:

your not the first person to say that to me! If I could just find time to
learn VBA, I could drive myself crazy instead!

My intent was to insert this macro into my personal workbook, and assign it
to a button on my shortcut bar, so that I can use it as a utility for various
other spreadsheets.

Will this work for that purpose?

:

Jonathan, you're making this harder!

Okay, how about this:

Put the following macro in the sheet you're working on:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
x = Target.Columns.Address
End Sub

Now, put this in a separate module:

Public x As String

Sub DoColumns()
Dim w As Integer
Dim i As Integer
Dim r As Range
w = 0
Columns(x).AutoFit
For Each r In Range(x)
If r.ColumnWidth > w Then w = r.ColumnWidth
Next r
Columns(x).ColumnWidth = w
End Sub

This will only work on adjacent columns in that sheet. Select your columns.
Then, using Tools/Macro/Macros, run the DoColumns macro.

With any luck this will work.

Art


:

Can you change it so that it works, regardless of of the specific columns or
number of columns i have in my selection?

:

Jonathan,

Try this:

Sub temp()
Dim w As Integer
Dim i As Integer
w = 0
Columns("A:C").AutoFit
For i = 1 To 3
If Columns(i).ColumnWidth > w Then w = Columns(i).ColumnWidth
Next i
Columns("A:C").ColumnWidth = w
End Sub

Art

:

Here is what I would like to be able to do. This is really a
formatting/presentation issue:

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march.

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does and
autofit on the column widths and then makes all three columns the same width
as the largest column.

Ideas?
 
D

Dave Peterson

Add a line like:
Call YourOtherMacroName
where you want to.

Jonathan said:
One more question. I have another macro that does custom column headings.
I'd like that macro to run this new macro you created for me. What is the
code to get that macro to run this macro?

Dave Peterson said:
I would have just done it manually <vvbg>.

Glad you made it work.


Jonathan said:
I had to make one change. The macro was making all the columns the same
width as the largest column, but it wasn't doing the autofit first.

This seems to work now.
' by Dave Peterson'
' On the selected range, this macro does an autofit, then looks to find the
largest column
' width in the selection, and then makes all the columns within that
selection the same
' width as the largest column.

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

Selection.Columns.AutoFit
LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

:

You could write your macro against the active worksheet. In fact, I think I'd
use the current selection on that activesheet, too. Then just select the
columns you want first and run the macro:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myRng As Range
Dim myCol As Range
Dim LargestWidth As Double

Set wks = ActiveSheet
Set myRng = Intersect(Selection.EntireColumn, wks.Rows(1)).EntireColumn

LargestWidth = 0
For Each myCol In myRng.Columns
If myCol.ColumnWidth > LargestWidth Then
LargestWidth = myCol.ColumnWidth
End If
Next myCol

myRng.ColumnWidth = LargestWidth

End Sub

Jonathan Cooper wrote:

Last week, Art was trying to help me with this, but the solution doesn't work
for me. I'm looking for a macro that would be contained within my
personal.xls, that I could use as needed.

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march. [On a different spreadsheet, it may be different columns, or
a different number of columns. The point is that I will select the range
before I run the macro.]

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does an
autofit on the column widths, checks the column width of all the columns
within the selection, and then makes all the columns the same width as the
largest column.

Is it possible to do this all within personal.xls and not have to add macros
to each spreadsheet that I use it on?

:

your not the first person to say that to me! If I could just find time to
learn VBA, I could drive myself crazy instead!

My intent was to insert this macro into my personal workbook, and assign it
to a button on my shortcut bar, so that I can use it as a utility for various
other spreadsheets.

Will this work for that purpose?

:

Jonathan, you're making this harder!

Okay, how about this:

Put the following macro in the sheet you're working on:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
x = Target.Columns.Address
End Sub

Now, put this in a separate module:

Public x As String

Sub DoColumns()
Dim w As Integer
Dim i As Integer
Dim r As Range
w = 0
Columns(x).AutoFit
For Each r In Range(x)
If r.ColumnWidth > w Then w = r.ColumnWidth
Next r
Columns(x).ColumnWidth = w
End Sub

This will only work on adjacent columns in that sheet. Select your columns.
Then, using Tools/Macro/Macros, run the DoColumns macro.

With any luck this will work.

Art


:

Can you change it so that it works, regardless of of the specific columns or
number of columns i have in my selection?

:

Jonathan,

Try this:

Sub temp()
Dim w As Integer
Dim i As Integer
w = 0
Columns("A:C").AutoFit
For i = 1 To 3
If Columns(i).ColumnWidth > w Then w = Columns(i).ColumnWidth
Next i
Columns("A:C").ColumnWidth = w
End Sub

Art

:

Here is what I would like to be able to do. This is really a
formatting/presentation issue:

Lets say I have A1:C5 selected, which represent headdings and data for jan,
feb and march.

I want to do an auto fit for each column, so that the column widths are big
enough to fit the numbers. No problem, format-->column-->autofit and I'm
done.

But once that is done (for example), the width of column A is 15, column B
is 7 and column C is 13. When you print the spreadsheet, it will look much
better if all the column widths are the same. So, since column A is the
largest, I would want all three columns sized to 15.

So, essentially I would like a macro that looks at the selection, does and
autofit on the column widths and then makes all three columns the same width
as the largest column.

Ideas?
 

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

Similar Threads

macro / autofit 5
Need macro - please help 1
Macro's 1
Column widths 2
Header Columns 2
column widths 3
Worksheet Column Widths Different For Different Users 2
How to split the spreadsheet, not the screen 3

Top