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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,217,757
Messages
6,138,429
Members
450,136
Latest member
Tabako1960

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