Activate sheet and centeralign a cell in that sheet

sylvest

New Member
Joined
Sep 15, 2006
Messages
46
How do I use VBA code to centre the text in the range Hello on Sheet2 of a workbook if the active sheet is unknown?

So first I need a code to select the Sheet2 then a code to centre align.

The above needs to be accomplished in one single line of VBA.Is it possible?

Tks..
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

venkat1926

Well-known Member
Joined
Aug 21, 2005
Messages
4,824
I doubt whether one line code is possible but you can put the macro name under a command button

the macro is
Code:
Sub test()
Dim sh As Worksheet
Dim cfind As Range
For Each sh In ThisWorkbook.Worksheets
sh.Activate
 Set cfind = Cells.Find(what:="hello", lookat:=xlWhole)
   If cfind Is Nothing Then GoTo line1
   
   With cfind
        .HorizontalAlignment = xlCenter
End With
line1:
Next sh
End Sub
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi sylvest

the range Hello

If you mean a cell that contains "Hello", you already have a solution. If you mean the range named "hello"

Worksheets("Sheet2").Range("Hello").HorizontalAlignment = xlHAlignCenter


Remark: "active sheet is unknown": I don't understand what you mean by this.

Hope this helps
PGC
 

Forum statistics

Threads
1,137,339
Messages
5,680,910
Members
419,940
Latest member
WilliamPorter

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
Top