Passing contorl ByRef gives Type Mismatch error

ajoy123rocks

New Member
Joined
Jul 13, 2022
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Private Sub Game_Num_Val_Change()


Dim cntl As Control


Me.Game_Num_Val = UCase(Me.Game_Num_Val)
Set cntl = Me.Game_Num_Val


Call VerticalAlignCenter(cntl)


End Sub

Public Sub VerticalAlignCenter(ByRef ctl As Control)
MsgBox 1
On Error GoTo ErrorCode
    Dim MinimumMargin As Integer
    Dim BorderWidth As Integer
    
    If Not ((TypeOf ctl Is TextBox) Or (TypeOf ctl Is Label)) Then Exit Sub
  
    MinimumMargin = 1 * TwipsPerPoint
    BorderWidth = (ctl.BorderWidth * TwipsPerPoint) / 2
    
    ctl.TopMargin = ((ctl.Height - (ctl.FontSize * TwipsPerPoint)) / 2) - MinimumMargin - BorderWidth
    
ErrorCode:
    Exit Sub
    
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Declare it as MSForms.Control in both places.
 
Upvote 0
Declare it as MSForms.Control in both places.
VBA Code:
Private Sub Game_Num_Val_Change()


Dim cntl As MSForms.Control


Me.Game_Num_Val = UCase(Me.Game_Num_Val)
Set cntl = Me.Game_Num_Val


Call VerticalAlignCenter(cntl)


End Sub
Public Sub VerticalAlignCenter(ByRef ctl As MSForms.Control)
MsgBox 1
On Error GoTo ErrorCode
    Dim MinimumMargin As Integer
    Dim BorderWidth As Integer
    
    If Not ((TypeOf ctl Is TextBox) Or (TypeOf ctl Is Label)) Then Exit Sub
  
    MinimumMargin = 1 * TwipsPerPoint
    BorderWidth = (ctl.BorderWidth * TwipsPerPoint) / 2
    
    ctl.TopMargin = ((ctl.Height - (ctl.FontSize * TwipsPerPoint)) / 2) - MinimumMargin - BorderWidth
    
ErrorCode:
    Exit Sub
    
End Sub

Still the same Type mismatch error... FYI the textbox is an ActiveX textbox
 
Upvote 0
Which line gives the error?
 
Upvote 0
I am no expert on this so hopefully Rory can help fill in the gaps but your code looks remarkable like what can be found at the link below and that is in an MS Access forum not Excel.

I can fix your mismatch error by declaring it in both places as:
VBA Code:
msforms.TextBox
(declaring both as Object seems to work as well)

Once past there I get additional errors ie
• TwipsPerPoint is not declared and not given a value
• BorderWidth & TopMargin are not a properties of the Active X Text box. It has Height, Width & Top
• This line did not work.
If Not ((TypeOf ctl Is TextBox) Or (TypeOf ctl Is Label)) Then Exit Sub
The Active X Equivalent seems to be:
If TypeName(ctl) <> "TextBox" And TypeName(ctl) <> "Label" Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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