I need to convert a String to an Object, something like this.
Dim ObjectName As String
Dim Object As Object
ObjectName = "Label1"
Set Object = ObjectName
Object.Caption = "Text"
This is something that would really help me a lot, but I don't know if that is possible.
example from ms docs site. add label as shape
Sub test()
Set myDocument = Worksheets(1)
myDocument.Shapes.AddLabel(msoTextOrientationVertical, _
100, 100, 60, 150) _
.TextFrame.Characters.Text = "Test Label"
End Sub
Add the labels as ActiveX objects on a sheet and set their .Caption property as follows
Public Sub SetLabelText()
Dim i As Long, n As Long, Obj as Object
n = Sheet1.OLEObjects().Count
For i = 1 To n
Set Obj = Sheet1.OLEObjects(i).Object
Obj.Caption = "Text #" & CStr(i)
Next i
End Sub
with the expected result
You can also access a specific label, by name OLEObjects("Label1").Object
Related
I have the following code and query to get the Sent/Received Bytes from Wlan connection. I want to get the values for each Properties' Item but I get Generic failure when trying like this:
a = WMIvalues.Item(1).Properties_.Item(1).Value
How would be the correct way to do it?
Sub Test()
Dim WMIvalues As Object
Dim sWQL As String
sWQL = "Select BytesReceivedPersec,BytesSentPersec,BytesTotalPersec from Win32_PerfRawData_Tcpip_NetworkInterface"
Set WMIvalues = GetObject("winmgmts:root/CIMV2").ExecQuery(sWQL)
a = WMIvalues.Item(1).Properties_.Item(1).Value
End Sub
This works for me:
Dim WMIvalues As Object
Dim sWQL As String
Dim o As Object, i As Long
sWQL = "Select BytesReceivedPersec,BytesSentPersec,BytesTotalPersec from " _
"Win32_PerfRawData_Tcpip_NetworkInterface"
Set WMIvalues = GetObject("winmgmts:root/CIMV2").ExecQuery(sWQL)
i = 0
For Each o In WMIvalues
i = i + 1 'increment item counter variable
Debug.Print o.BytesReceivedPersec, o.BytesSentPersec, o.BytesSentPersec, o.BytesTotalPersec
'logic here based on i and the o properties...
Next o
See: https://www.activexperts.com/admin/scripts/wmi/vbscript/0473/
As said above, in my following code I get an compiler error telling me there is an invalid identifier. I don't really see the problem, basically it is a very easy code. The problem should be in the lines using the backcolor-Function.
Sub addmaterial()
Dim AMU As UserForm
Set AMU = AddMaterialUserform1
Dim SCU As ComboBox
Set SCU = AMU.SelectComboBoxUserform
Dim APCU As ComboBox
Set APCU = AddMaterialUserform1.AddedPropertiesComboBoxUserform
Dim TextBoxObject As Combobox
Dim i As Integer
SCU.AddItem "Material"
SCU.AddItem "Material Group"
APCU.BorderColor.ColorIndex = 15
For i = 1 To 12
TextBoxObject = "Textbox" & i
AMU.TextBoxObject.BackColor.ColorIndex = 15
Next
AMU.Show
End Sub
You try to assign a String to an Object
TextBoxObject = "Textbox" & i
You can use the AMU.Controls- Collection
Set TextBoxObject = AMU.Controls("Textbox" & i)
If you don't have the reference just the Name.
Or if there is no Collection on other Objects have a look at
CallByName(Object As Object, ProcName As String, _
CallType As VbCallType, Args() As Variant)`
`.
Dim TextBoxObject As String
AMU.TextBoxObject.BackColor.ColorIndex = 15
At a guess it doesn't like you declaring a string variable as the same name as a text box
So my code looks like this:
Dim i As Integer
Dim labelnum As String
For i = 1 To 81
labelnum = "Label" & i
If "labelnum".Caption = Label1.Caption Then
"labelnum".BackColor = Label1.BackColor
End If
Next i
I want to loop through 81 labels to check to see if the caption in that one is the same as the one I have selected. Is there something else I can put where it says "labelnum"?
I'm practicing and trying to make sudoku through VBA. I want to highlight the box I have selected and highlight all other squares on the board that have the same number.
Thanks!
In a Worksheet, a Label is a Shape Object, so you can use the Shapes collection:
Dim i As Integer
Dim shpLabel As Shape
For i = 1 To 81
Set shpLabel = Sheet1.Shapes("labelnum" & i)
If shpLabel.Caption = Label1.Caption Then
shpLabel.BackColor = Label1.BackColor
End If
Set shpLabel = Nothing
Next i
In a UserForm, a Label is a Control Object, so you can use the Controls collection:
Dim i As Integer
Dim ctrlLabel As Control
For i = 1 To 81
Set ctrlLabel = Me.Controls("labelnum" & i)
If ctrlLabel.Caption = Label1.Caption Then
ctrlLabel.BackColor = Label1.BackColor
End If
Set ctrlLabel = Nothing
Next i
I have a class called autoCRUD in a class module in excel 2013. From another module (a regular one) I try to call a method from this class and I get the "Object required" exception.
Here's the method:
Public Function CreateCRUDView(TipoCRUD As String) 'TipoCRUD pode ser C (Create), R (Read), U (Update), D (Delete)
Dim myForm As Object
Dim NewFrame As MSForms.Frame
Dim NewButton As MSForms.CommandButton
Dim NewListBox As MSForms.ListBox
Dim NewLabel As MSForms.Label
Dim X As Integer
Dim Line As Integer
Dim t As Integer
Dim arrLeg() As Variant
arrLeg = legenda
'This is to stop screen flashing while creating form
Application.VBE.MainWindow.Visible = False
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
'Create the User Form
With myForm
.Properties("Caption") = "New Form"
.Properties("Width") = 300
.Properties("Height") = 270
End With
'Criar labels
t = 10
For Each lbl In arrLeg
Set NewLabel = myForm.Designer.Controls.Add("Forms.label.1")
With NewLabel
.name = "lbl_" + Replace(CStr(lbl.Value), " ", "")
.t = (10 + t)
.Left = 10
.Font.Size = 8
End With
t = t + 10
Next
'Create CommandButton Create
Set NewButton = myForm.Designer.Controls.Add("Forms.commandbutton.1")
With NewButton
.name = "cmd_1"
If UCase(TipoCRUD) = "C" Then
.Caption = "Salvar"
ElseIf UCase(TipoCRUD) = "U" Then
.Caption = "Alterar"
End If
.Accelerator = "M"
.Top = Top + 10
.Left = 200
.Width = 66
.Height = 20
.Font.Size = 8
.Font.name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
Top = Top + 10
End Function
The code from another module that calls the method is :
Public Sub Main()
Dim ac As autoCrud
Set ac = New autoCrud
ac.CreateCRUDView ("c")
End Sub
I don't get it, why am I getting this error?
Here is the code for "legenda":
Public Property Get sht() As Worksheet
Const shtName As String = "Teste1"
Set sht = ActiveWorkbook.Worksheets(shtName)
End Property
Public Property Get legenda() As Range
Const linha As Integer = 3
Const colunaI As Integer = 2
Dim colunaF As Integer
Dim i As Integer
i = colunaI
Do While sht.Cells(linha, i).Value <> ""
i = i + 1
Loop
colunaF = (i - 1)
Set legenda = sht.Range(Cells(linha, colunaI), Cells(linha, colunaF))
End Property
The lbl.Value is supposed to be a string value, the name of the label. And it comes from the spread sheet in the header of the table, teh legenda() only selects that header and the arrLeg takes the legenda as a range and transforms it in an array.
Edit:
Apparently the error occurs in the line that says: .name = "lbl_" + Replace(CStr(lbl.Value), " ", "")
As you can see, I've tried to take the spaces from the string and also ensure that it is a string, but none of it worked.
Edit 2:
I actually just use a class for organization and re-usability purposes. I take the properties and other methods and use them inside the 'createCRUDView' method, this method will then create a CRUD View, that is, create a form either to "Create", "Read" (not used since it's excel),"Update or "Delete" data entries. It basically creates forms dynamically to any table you make
VBA error 424 is object required error. So I'm now pretty sure that lbl in CStr(lbl.Value) is not an object. With your code legenda is a Range but after
Dim arrLeg() As Variant
arrLeg = legenda
arrLeg will be a variant array. This array does not contain objects. You can debug this with
For Each lbl In arrLeg
...
MsgBox TypeName(lbl)
...
Next
So you should use CStr(lbl).
And
Set legenda = sht.Range(Cells(linha, colunaI), Cells(linha, colunaF))
will only work while the "Teste1" sheet is the ActiveSheet because Cells(linha, colunaI) is not explicit assigned to a sheet so the ActiveSheet will be supposed.
Set legenda = sht.Range(sht.Cells(linha, colunaI), sht.Cells(linha, colunaF))
will be better.
I want to dynamically add a radio button on a form, using VBA.
I tried writing this code, but it crashes with 'Type Mismatch'
Dim optionBtn As OptionButton
Set optionBtn = UserForm1.Controls.Add("Forms.OptionButton.1", "name", True)
optionBtn.Left = 10
optionBtn.Top = 10
optionBtn.Width = 30
optionBtn.Group = "q1"
I also tried doing this:
Dim optionBtn As Control
Set optionBtn = UserForm1.Controls.Add("Forms.OptionButton.1", "name", True)
optionBtn.Left = 10
optionBtn.Top = 10
optionBtn.Width = 30
optionBtn.Group = "q1"
but I get a Control, not a OptionButton - how can I cast it to a OptionButton ? (sorry, I'm new to VB)
I was able to get it work with this (Excel 2003):
Dim lbl As Variant
Set lbl = UserForm1.Controls.Add("Forms.Label.1", "lblFoo", True)
lbl.Caption = "bar"
Update to reflect your change from a Label to an OptionButton
Again, the key is use a Variant type for the variable that you are assigning the returned control to:
Dim opt As Variant
Set opt = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo", True)
opt.Caption = "Bar"
Keep in mind that autocomplete won't work on the variables that are defined as Variants. However you can still refer to properties and methods of those variables by typing them manually.
Actually, I believe your problem lies in the fact that you are naming optionBtn as an object button. It needs to be named as a MSForms Option Button. Since a Variant can be an object, it will work when using a variant.
I used the following and it works fine.
Dim TempForm As Object
Dim newOptionButton as MSForms.OptionButton
Dim sUserformName as string
Dim i as integer
Dim x as integer
Dim y as integer
' other junk removed for example sake...
sUserformName = sheet1.cells(1,1)
' create form
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
With TempForm
.Properties("Caption") = sUserformName
.Properties("Width") = 450
.Properties("Height") = 300
End With
for i = 3 to sheet1.range("A65536").End(XlUp).Row
sDefault = sheet1.cells(i,5)
iGN = sheet1.cells(i,6)
' additional code removed... for sake of example... the code would add labels, text boxes, etc...
Set newOptionButton = TempForm.designer.Controls.Add("Forms.optionbutton.1")
With newOptionButton
.Caption = sDefault
.GroupName = "Group" & iGN
.Top = 20 + (20 * x)
.Left = y
.Height = 16
.Font.Size = 8
.Font.Name = "Ariel"
End With
' here the code changes x and y depending on where the user (excel template) directs the next control.
next i
Good luck....
mark's code should work, but I often prefer to create the item manually then show/hide it based on the need.
You need to define the object as an optionbutton from the msforms library.
Dim optionBtn As MSForms.OptionButton
Set optionBtn = UserForm1.Controls.Add("Forms.OptionButton.1", "name", True)