Passing the HorizontalAlignment property to a Subroutine

pmich

Active Member
Joined
Jun 25, 2013
Messages
294
The Font Size and the HorizontalAlignment of Text in various rows will have to be different.
So, when the programme moves from one Row to the other I am trying to pass to a Subroutine the Font Size whether 10 or 11 and the Alignment property whether xlCenter or xlLeft.

I select a range and I call the Subroutine.
Code:
Range("B1").Select
Call TxtTunerTwoOff(11, "xlleft")
This is the Subroutine that is called:
Code:
Private Sub TxtTunerTwoOff(FontSiz As Integer, Alignr As String)
With Selection
   .Offset(0, 4).Font.Bold = True
   '.Offset(0, 4).Font.Size = 11
   .Offset(0, 4).Font.Size = FontSiz
   '.Offset(0, 4).HorizontalAlignment = xlCenter
   .Offset(0, 4).HorizontalAlignment = Alignr
End With
End Sub
When the Subroutine is simply "Private Sub TxtTunerTwoOff" and when I use "Call TxtTunerTwoOff'" to run the Subroutine, then the code ".Offset(0, 4).HorizontalAlignment = xlCenter" works fine.
But when the Subroutine is "Private Sub TxtTunerTwoOff(FontSiz As Integer, Alignr As String)", then the code ".Offset(0, 4).HorizontalAlignment = Alignr" gets the following error message:
Run-time error '1004':
Unable to set the HorizontalAlignment property of the Range class

Any suggestion is appreciated.
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try
Code:
Private Sub TxtTunerTwoOff(FontSiz As Integer, Alignr As [COLOR=#ff0000]Long)
[/COLOR]
Code:
    Call TxtTunerTwoOff(11, -4131)
 
Upvote 0
You can also use the actual enumeration:

Code:
Private Sub TxtTunerTwoOff(FontSiz As Integer, Alignr As xlHAlign)
Code:
    Call TxtTunerTwoOff(11, xlHAlignLeft)
 
Upvote 0
Fluff
user-offline.png

Thanks for the suggestion.
Your code works fine for Left Alignment.
I saw a table on the net and found out that -4108 is the integer for Center Alignment.
Thanks for your code.
 
Upvote 0
RoryA
user-offline.png

Thanks for your valuable suggestion.
Actually, I was reluctant to ask this question, as I thought that the VB terms cannot be passed on to a subroutine.
I searched on the web and I could not find a solution. There may be one in some website. Anyway, I could not locate it.
But, now I know that it is good that I have raised this issue.
I am glad that I got the answer to my query.
I like this FORUM.
I tried your code and it works perfectly.
The highlight of your code is that even without knowing the integer for the center alignment, I can use xlHAlignCenter.
Thanks, once again.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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