using macro recorder to change a whole column's format

still learning

Well-known Member
Joined
Jan 15, 2010
Messages
838
Office Version
  1. 365
Platform
  1. Windows
Hi
Just wondering……..
When I open a worksheet, the advance filter looks for criteria from another sheet in the same workbook.
One of the columns (B:B) seems to put the extracted data in with the format to the “left”.
The data is always a text
What I did was ran the macro recorder and have the whole column formatted to “center”.
(the cursor is at A1 when this part of the macro starts and the cursor goes back to A1 to continue the macro)

My question is :
Why does the macro recorder make the column as A:A instead of B:B
VBA Code:
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    With Selection
        .HorizontalAlignment = xlCenter
    ActiveCell.Offset(0, 1).Select
    End With


mike
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I believe it is really saying the first column in your selection (that "A:A" really means the first column in your selection).

Your selection is:
VBA Code:
ActiveCell.Offset(0, 1)
If you start in cell A1, and offset one column, you are now in B1.
So, the first column in that selected range is column B.
 
Upvote 0
Solution
Hi Joe4,
I see.
is there a way to write that part of the macro where if I'm in A1, I don't have to move to the heading of B:B and then move back.
Just wondering

mike
 
Upvote 0
I am not 100% clear on what it is you are trying to accomplish.
Are you starting in cell A1 and wanting to select column A, or column B?
 
Upvote 0
When I open the sheet.
the cursor moves to A1, in case it's not there already.
Then the advance query ( Filter>>Advance )finds the data that i want from a different sheet in the same workbook and puts it in the proper column.
for reasons unknown,( to me, anyway) the text that goes into B:B: is formatted to "left", even though its formatted "center" on the other worksheet.
I then go to B:B and format it to "center"
Then the cursor goes back to A1
Then the cursor drops down to A4 and freezes that line (for the header)
then it drops down to the first empty column in A
Using the macro recorder, it called B:B, A:A
I was wondering why.
You explained it very well.
Then I was wondering if I could have the macro format B:B with out going to it first.
Here is the full macro.
The criteria is always the same>>>becky.
VBA Code:
Sub becky()
Sheets("Becky 2025").Select
Application.ScreenUpdating = False
Application.Goto Reference:="R1C1"
         Range("beckydata").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
        "beckycriteria"), CopyToRange:=Range("beckyextract"), Unique:=False
 ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    With Selection
        .HorizontalAlignment = xlCenter  
    End With
 Range("A1").Select
 Range("A4").Select
 ActiveWindow.FreezePanes = True
Selection.End(xlDown).Select
             '  I'll use this when I have more than 8-10 lines
             ' it'll  make it easier for me to see
     'ActiveWindow.ScrollRow = ActiveCell.Row - 8
ActiveCell.Offset(1, 0).Range("A1").Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
for reasons unknown,( to me, anyway) the text that goes into B:B: is formatted to "left", even though its formatted "center" on the other worksheet.
It doesn't matter what it was formatted to align to on the other sheet, the column on the sheet will be be formatted to General and if you are putting text in the cells it will align to the left.

ActiveCell.Offset(0, 1).Columns("A:A")
This is telling the cell to offset by one cell to the right i.e. from A1 to B1 then use the first column of that cell (.Columns(1)) i.e. column B

Then I was wondering if I could have the macro format B:B with out going to it first.
Replace
VBA Code:
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    With Selection
        .HorizontalAlignment = xlCenter  
    End With
with
VBA Code:
Range("B:B").HorizontalAlignment = xlCenter
 
Upvote 0
Thanks for the assist, Mark. I was pulled away for a little while.
 
Upvote 0
Hi Mark858
Hi Joe4
Thank you both for the very informative response.
I know my way works, but , as my screen name shows, I'm always learning.
I have to say that because of this forum, I've been able to write code.
everything I've learned is from here.
I'm also glad that I've never experienced a reason to feel ashamed because of my questions.

mike
As a PS, is there a way to show more than one solution?
 
Upvote 0

Forum statistics

Threads
1,225,606
Messages
6,185,956
Members
453,333
Latest member
BioCoder84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top