'Set Worksheet in excel VBA return error of compilation
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:
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!
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

