I have a piece of script that’s in module1 that checks if an option button is clicked.
The option button is placed in Sheet1, name “Info”, so I thought the script below would work
Sub checkClicked()
dim Page as worksheet
set Page as worksheets(“Info”)
Debug.print Page.optClicked
End sub
But when I did it like this it says method or data member not found. It would only work if I replace it with
…
Debug.print Sheet1.optClicked
…
Can anyone give me an insight why this happens?
Think of Sheet1 as a "subclass" of "worksheet" - when you add controls to the sheet you're adding new members. A generic worksheet object doesn't have a member which represents your option button, whereas Sheet1 does.
Sub Test()
Dim sht As Worksheet
Dim sht1 As Sheet1
Set sht = ThisWorkbook.Sheets("Sheet1")
Set sht1 = Sheet1
Debug.Print sht.optClicked 'error
Debug.Print sht1.optClicked 'OK
End Sub
Set Page = ActiveWorkbook.Worksheets("Info") should work. I think worksheets is no real property in VBA...
Also, your debug print code looks weird, use debug.print("bla")..
Do you have Option explicit activated?
Try Set Page = Worksheets("Info") and do NOT use these curly “” quotes - just in case (for Excel formulas this DOES matter).
The argument within Worksheets is the name of the worksheet you are interested in, i.e. "Sheet1".
Other approach: the ActiveX controls on sheet are accessible from two collections: Shapes and OLEObjects. You could use the OLEObjects collection to get access to your checkbox.
Sub checkClicked()
Dim Page As Worksheet
Set Page = Worksheets("Info")
' 1/ ActiveX check box in Shapes collection
Dim myShape As Shape
Set myShape = Page.Shapes("optClicked")
' --------------------------------------
' 2/ ActiveX check box in OLEObjects collection
Dim myOLEObject As OLEObject
Set myOLEObject = Page.OLEObjects("optClicked")
' Use Object property to get access to your check box
Dim myCheckBox As Variant
Set myCheckBox = myOLEObject.Object
If (TypeOf myCheckBox Is MSForms.CheckBox) Then
Debug.Print myCheckBox.value
End If
End Sub
Related
I am trying to copy data from one worksheet to another. I have a workbook that has about 62 worksheet tabs.
The part that is especially tricky for me is that the worksheet the data needs to be copied to will not always be the same.
I have a dropdown menu that lists 62 different pieces of equipment. This is shown in G1 in the worksheet named "HOME". I want the text to copy over to the correct tab based on the selection.
I figured out how to copy over specific text, when I do this I see the word "TEXT" show up on the specified worksheet.
Sheets(Range("g1").Value).Activate
Range("a1").Value = "TEXT"
I cannot figure out how to copy over G4:G24 from my "HOME" worksheet to another worksheet based on the same drop-down menu.
This is what I tried.
Private Sub CommandButton1_Click()
Worksheets("HOME").Range("g4:g24").Copy
Sheets(Range("g1").Value).Activate
Range("a1").Value = "TEXT"
Sheets(Range("g1").Value).Activate
Range("f4").PasteSpecial
End Sub
Be explicit about workbook and worksheets - never use Range/Cells without qualifying a worksheet (though you can get away with it in a worksheet code module if you're referring to the associated worksheet).
Private Sub CommandButton1_Click()
Dim wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook 'or ActiveWorkbook?
With wb.Worksheets("HOME")
Set ws = wb.Worksheets(.Range("G1").Value) 'create a worksheet reference
ws.Range("A1").Value = "TEXT" '...and use it
.Range("g4:g24").Copy ws.Range("f4")
End With
End Sub
See for example: What is the default scope of worksheets and cells and range?
Generally speaking you have a good start there, but it can be accomplished in much fewer lines with much more speed like this:
Sub ExampleSub()
Dim SheetName As String
SheetName = Worksheets("HOME").Range("A1").Value
Worksheets("HOME").Range("G4:G24").Value = Worksheets(SheetName).Range("G4:G24").Value
End Sub
It's not even necessary to use the variable SheetName, but it can help keep things simple, it can also now be reused later in the subroutine.
An alternative to reference sheets is to make the variable a worksheet:
Sub ExampleSub()
Dim SheetName As Worksheet
Dim HomeSheet As Worksheet
Set HomeSheet = Worksheets("HOME")
Set SheetName = Worksheets(HomeSheet.Range("A1").Value)
HomeSheet.Range("G4:G24").Value = SheetName.Range("G4:G24").Value
End Sub
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!
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.
I have opened an Excel file via VBA Access and can read and write in the Excel Cells. How can I tick an Excel Ckeck Box?
My code:
Dim Excel_App As Object
Dim strExcel As String
Set Excel_App = CreateObject("Excel.Application")
Excel_App.Visible = True
Excel_App.Workbooks.Open fready
With Excel_App
.CheckBox3.Value = True 'This line is used in VBA Excel and I need in Access
End With
Checkboxes belong to a specific collection on a specific Worksheet. Which collection this is, depends on the type of control you're looking for.
Your code is written as if the checkbox belonged to the Excel.Application object - that can't work.
First you need to keep a reference to that Workbook object you're opening, so instead of this:
Excel_App.Workbooks.Open fready
You need this:
Dim book As Object ' early-bound: As Excel.Workbook
Set book = Excel_App.Workbooks.Open(fready)
If you don't know which worksheet to find the checkbox on, you'll have to iterate the workbook's Worksheets collection:
Dim sheet As Object ' early-bound: As Excel.Worksheet
For Each sheet In book.Worksheets
'todo
Next
Form Controls
So we're looking for a CheckBox form control. We'll find that in the sheet's Shapes collection, and we'll know we're looking at a form control when its Type is msoFormControl; we'll know that it's a checkbox control when its FormControlType property returns xlCheckBox:
Dim sheet As Object ' early-bound: As Excel.Worksheet
For Each sheet In book.Worksheets
Dim shp As Object ' early-bound: As Excel.Shape
For Each shp In sheet.Shapes
If shp.Type = 8 ' early-bound: msoFormControl
If shp.FormControlType = 1 ' early-bound: xlCheckBox
'todo
End If
End If
Next
Next
So now we know shp is a checkbox form control. The Value is accessible through the ControlFormat object/property, so you can set the value of a checkbox named Check Box 1 (that's the default name) like this:
If shp.Name = "Check Box 1" Then
shp.ControlFormat.Value = 1 'checked
End If
Of course if you already know which specific sheet you're looking for, there's no need to iterate them all.
ActiveX Controls
If the control is an ActiveX control, it's a whole different story; you'll find it in the OLEObjects collection instead, which contains OLEObject instances, which have an Object property that returns the MSForms.CheckBox object; you can get the name of the checkbox from the OLEObject.ShapeRange.Name:
Dim ctrl As Object ' early-bound: As Excel.OLEObject
For Each ctrl In sheet.OLEObjects
If TypeName(ctrl.Object) = "CheckBox" Then ' early-bound: If TypeOf ctrl.Object Is MSForms.CheckBox Then
If ctrl.ShapeRange.Name = "CheckBox1" Then
ctrl.Object.Value = True ' checked
End If
End If
Next
Note that the early-bound TypeOf ctrl.Object Is MSForms.CheckBox check is MUCH more robust than the late-bound TypeName check. You need to reference the MSForms type library through Tools > References to use it (it's already referenced if your VBA project has any UserForm component, in which case early-bound code is a no-brainer).
I have placed a TextBox control on a sheet in Excel, in a VBA module I want to acces the TextBox and populate with content.
How do I reference the TextBox control?
I've named the TextBox 'tbSQL', in the module I can see Application and can refernece the sheet from the module, the sheet is called 'Database Info.'
I want to write data into the TextBox from the VBA module, but so far I haven't been able to reference it.
I've tried:
Public Const DATABASE_INFO As String = "Database Info."
Dim objDBsheet As Worksheet, objSQL As Range
Dim tbSQL As TextBox
Set objDBsheet = Application.Sheets(DATABASE_INFO)
Set tbSQL = objDBsheet.Shapes("tbSQL")
But this is as far as I get it errors on the Set tbSQL line. The reported error is "Type Mismatch"
I know the control is a TextBox, it was created from the Controlbox toolbar. When looking in the range bar in Excel it displays:
"=EMBED("Forms.TextBox.1","")"
In the Properties box for the TextBox control I have set the (Name) property to tbSQL, however it remains unchanged in the fx text box. It does show as tbSQL in the range box.
Most likely its a bug in Excel. It has nothing to do with Excel-2003.
If you use a Worksheet type variable then Excel fails to discover the control on that sheet in VBA. So if you declare your sheet holding variable as Object / Variant , the code will work fine.
Other alternative is to directly use the Worksheet's CodeName, so if you set the Worksheet's name as wksDBSheet in the VBA IDE's property grid and use that in your code, it will discover the TextBox
Sub test()
Dim objDBsheet As Object 'As Worksheet // Making the 0bjDBSheet type as Object or Variant
'// Allows the discovery of the TextBox on the sheet.
'// Most Likely its a bug.
Dim objSQL As Range
Dim tbSQL As MSForms.TextBox
Set objDBsheet = Application.Worksheets("Database Info.")
Set tbSQL = objDBsheet.tbSQL
tbSQL.Text = "Bug"
'/ Other Alternative is to directly use the CddeName of the sheet.
Set tbSQL = wksDBsheet.tbSQL
tbSQL.Text = "Code Name used"
End Sub
Text Box as shape, or Label as Form Control, or ActiveX control:
Sub f()
Dim tb As Shape, lblControl As Object, lblActiveX As Object
Set tb = Sheet1.Shapes("TextBox 1")
Set lblControl = Sheet1.Shapes("Label 2").OLEFormat.Object
Set lblActiveX = Sheet1.Shapes("Label1").OLEFormat.Object
lblControl.Text = "Form Control"
lblActiveX.Object.Caption = "ActiveX Control"
tb.TextFrame.Characters.Text = "Text Box"
End Sub
You need to use:
Sheets("Sheetname").Shapes("tbSQL").TextFrame.Characters.Text = "Anything you want"
In the end it looks like it is a bug in Excel 2003, I was able to reference the control by using Sheet1.tbSQL instead of going through Application.Sheets.
If you put an ActiveX texbox on a worksheet named "MyTextBox" then you can set it's text in vba like this:
ActiveSheet.MyTextBox.Text = "hi"
I want to programatically add items to an ActiveX ComboBox.
How do I reference the ComboBox to perform a .additem to it.
I tried:
Dim lstSubsB As ComboBox
Set lstSubsB = ws.ListObjects("lstSubs") ' - << Error occurs here
but it gives a 'Subscript out of Range' error. The combobox, is named 'lstSubs'.
In addition to the solutions provided, I found that the below also works.
Set lstSubsB = Me.lstSubs
With lstSubsB
For x = arrLower To arrUpper
lstSubs.AddItem arrSubs(x)
Next x
End With
Dim objX As Object
Set objX = ws.OLEObjects("lstSubs")
I know this is an old topic, but I just figured it out and thought I'd save some time for the next person. The property you want is "Object", which in this case is a ComboBox type. I'm sure Object type works as well, but strong typing helps with code complete.
' This snippet adds sheet names to a combobox
Dim lstSubsB as ComboBox
Dim Sht As Variant
' The Object property in this case returns a typed ComboBox
Set lstSubsB = ws.OLEObjects("lstSubs").Object
lstSubsB.Clear
For Each Sht In Worksheets
lstSubsB.AddItem Sht.Name
Next