Home > Blockchain >  Set Worksheet in excel VBA return error of compilation
Set Worksheet in excel VBA return error of compilation

Time:04-20

I have two tabs in Excel, in one I have a button that performs a certain operation, and in the other I have two OptionButtons (OB).

The first OB is named obPD.

The second OB is named obAD.

When I click the button, the following routine is called:

Sub PDAN()
Dim wb1 As Workbook: Set wb1 = MyWB
Dim wsE As Worksheet: Set wsE = wb1.Worksheets("Sheet1")

If wsE.obPD.Value = True Then
    'do something
ElseIf wsE.obAD.Value = True Then 
    'do something
Else
    'do something
End If

The problem is that the following error is showing:

enter image description here

VBA indicates that the problem is in part

wsE.obPD.Value

If, instead, I change wsE to Worksheets("Sheet1"), then the error disappears.

Can anyone help? Thanks in advance!

CodePudding user response:

You'd need to use

Dim wsE As Object

instead of

Dim wsE As Worksheet

An "out of the box" WorkSheet object does not have a member obPD or obAD (that's what is triggering the compile-time error) so you cannot use WorkSheet as the variable type.

Better to use the sheet's codename as suggested by BigBen though.

  • Related