Center Across Celection

Dixie77

Board Regular
Joined
Nov 13, 2011
Messages
53
Hi All,

I'm hoping someone can point me in the right direction, I have the below code that looks for a number in column A if it finds it then the adjacent cells CentreAcrossSelection. I've found this is much easier than merging the cells.

But I'm getting a run time '91' error message when running?

Public Sub CenterValues()
Dim rng As Range, _
rng1 As Range, _
LR As Long

With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
LR = Range("A" & Rows.Count).End(xlUp).Row
With Range("A1:A" & LR)
Set rng = .Find("1", LookIn:=xlValues, lookAt:=xlPart)
If Not rng Is Nothing Then
rng1 = rng.Address
Do
rng.Resize(1, 7).HorizontalAlignment = xlCenterAcrossSelection
Set rng = .FindNext(rng)
Loop While Not rng Is Nothing And rng.Address <> rng1
End If
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub

Any ideas why this is happening? and any tips on how to correct?

Thanks in advance for looking

Rob
 

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.
rng and rng1 are Ranges
rng.Address returns a String
You cannot set a Range equal to a String
Try:
Code:
Set rng1 = rng
 
Last edited:
Upvote 0
Hi Joe4,

Thanks for the assist,

I'm now getting an error when it gets to the below line,

Loop While Not rng Is Nothing And rng.Address <> rng1

When I change rng.Address to rng, the code runs but nothing happens?

Any ideas?

Thanks again for the help

Rob
 
Upvote 0
You can try:
Code:
[COLOR=#FF0000]rng.Address <> rng1.Address
[/COLOR]
If it is not giving you the expected results, please lay out an actual data example for us to test.
 
Upvote 0
Hi Joe4,

That has worked a treat, I can't believe it was something as simple as that.

:eek:

Thanks a lot for the pointers

Regards

Rob
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

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