I've created a combobox in an Excel sheet and named it "GraphChoice". It's located in a sheet named "Choose Graph"
In VBA I'm trying to fill it with data using this code:
Sub Choose_graph_and_date()
Dim Graph As Worksheet
Dim FormInfo As Worksheet
Set Graph = Worksheets("Choose Graph")
Set FormInfo = Worksheets("Forminfo")
Graph.Activate
Graph.GraphChoice.List = FormInfo.Range("A1:A3").Value
End Sub
I get an error saying Can't find method. I've also tried.
GraphChoice.List = FormInfo.Range("A1:A3").Value
Then I get error 424. Object Required.
The code is in the ThisWorkbook module since I want it to load every time you open the workbook.
Anyone knows whats wrong?
For a form control, you can use the DropDowns collection...
Graph.DropDowns("GraphChoice").List = FormInfo.Range("A1:A3").Value
Or, you can use the Shapes collection...
Graph.Shapes("GraphChoice").ControlFormat.List = FormInfo.Range("A1:A3").Value
For an ActiveX control, you can refer to it using the OleObjects collection...
Graph.OLEObjects("GraphChoice").Object.List = FormInfo.Range("A1:A3").Value
Or, you can use the Shapes collection...
Graph.Shapes("GraphChoice").OLEFormat.Object.Object.List = FormInfo.Range("A1:A3").Value
Actually, you can also refer to it using the code name for your sheet. So, for example, let's say that the code name for your sheet is Sheet2, you could do the following...
Sheet2.GraphChoice.List = FormInfo.Range("A1:A3").Value
My understanding is that this issue exists, because ActiveX Controls have a different kind of relation (hence referencing) than other common objects (such as cells) towards the sheets. You can solve your problem by either using:
Sheets("Choose Graph").GraphChoice.List = FormInfo.Range("A1:A3").Value
or by changing the object properties of the sheet with your graph to e.g. myGraphSheet and then using:
myGraphSheet.GraphChoice.List = FormInfo.Range("A1:A3").Value
Also if you want the ComboBox to be filled everytime you open it, you need to place your code in ThisWorkbook-Module (as you did) and in a sub with the following name Workbook_Open() :
Sub Workbook_Open()
'code executed when opened
End Sub
Related
I would like to create a script in excel vba to change the Reference of a bunch of defined names. I have a revised workbook with updated data and a new name; I would want to change each defined name reference to the updated workbook. For example:
change the reference of the named range
from '[c:\files\[factorsrev1.xls]'!:$K$15:$N$76
to '[c:\files\[newfactorsrev2.xlsm]!:$K$15:$N$76
I tried using the (change-multiple-named-cells-and-ranges) post, as a guide, but so far it hasn't worked.
What I have so far:
Sub RangeRename()
Dim N As Name
For Each N In ActiveWorkbook.Names
N.RefersTo = WorksheetFunction.Substitute(N.RefersTo, "factorsrev1.xls", "newfactorsrev2.xlsm")
Next N
End Sub
Sure would be nice not having to edit each one manually...help is most appreciated.
I'm trying to create a series of macros to audit some financial models.
The first macro I’m trying to create is one that names the current cell. Why? I want to name the cell, after that I’m going to record a macro to click the “Trace Precedents” and go to the cell that has the relationship.
After that I need to go back to the original cell, thats the named one. That's easy on the go function, but I need to the naming macro working
My recorded code for the naming macro is as follows:
Sub Namer ()
ActiveWorkbook.Names.Add Name:="Name1", RefersToR1C1:="=Workings!R42C6"
ActiveWorkbook.Names("Name1").Comment = ""
End Sub
I have the following problems:
I need to name the current cell on a workbook with a lot of sheets. I’m gonna be moving between sheets but my recorded code has a “fixed” sheet.
How can I fix that? Name the current cell on the current sheet
Something like this should help you ...
Public Sub CreatesNames()
Dim objSheet As Worksheet
For Each objSheet In ThisWorkbook.Worksheets
objSheet.Names.Add Name:="Name1", RefersTo:=objSheet.Range("A1")
Next
End Sub
... something to note, names can be created at the worksheet level or at the workbook level, so, if you're going to be creating the same name across worksheets then you need to use a Worksheet object, not the Workbook object.
So to use the active cell ...
Sheet Level
ActiveSheet.Names.Add Name:="Name1", RefersTo:=ActiveCell
Workbook Level
ActiveWorkbook.Names.Add Name:="Name1", RefersTo:=ActiveCell
I hope that helps.
I'm very new to DBA so probably it's a banal mistake but I looked around and I did not found anything that could help me.
I'm trying to populate a combobox dynamically using the content of a column (column "A" in this specific case) using a macro linked to a button. If the analyzed cells are empty everything goes smoothly and the message "done!" appears, but if there is any data in the cells I get the error "424 object required access".
I don't know if it would help: I took the code from this youtube video https://www.youtube.com/watch?v=x8O59GtatH8 and adapted it (just removed the listox) the complete code is at 5.35
I'm probably misunderstanding something very basic. I am guessing the declaration of the combobox.
Sub prova_stessa_scheda()
row_review = 1
Dim TheSheet As Worksheet
Set TheSheet = Sheets("Listino_prezzi")
Do
DoEvents
row_review = row_revieew + 1
item_in_review = TheSheet.Range("A" & row_review)
If Len(item_in_review) > 0 Then ComboProva_Change.AddItem (item_in_review) 'this is the command that gives the error
Loop Until item_in_review = ""
MsgBox "Done!"
End Sub
I expected that when the macro gets triggered the combobox gets filled with the value written in the cells of column "A" instead I got the error 424.
If you put your code into the Worksheet module of the sheet where the combobox is placed, VBA is assuming that you want to access the element CombProva of that sheet (this is what is done in the video, only with different names).
However, if you put your code into a regular module, VBA does not know what CombProva is. You have to tell VBA that you want to access it from a specific sheet. There are several ways to do so:
(1) Use
With ThisWorkbook.Sheets("Listino_prezzi")
.ComboProva.AddItem (item_in_review)
End With
Note that the following code will throw an compiler error. This is because TheSheet is of type Worksheet, so it could be any worksheet, and a worksheet does not have anything named ComboProva.
Dim TheSheet As Worksheet
Set TheSheet = Sheets("Listino_prezzi")
With TheSheet
.ComboProva.AddItem (item_in_review) ' <-- Compiler error
End With
(2) You can access the sheet also by its CodeName. If you look to the video: The sheet itself was renamed to Admin Site, but the CodeName is still Sheet1 (the CodeName can only be changed in the VBA editor in the Property-window). So you can write
With Sheet1
.ComboProva.AddItem (item_in_review)
End With
(3) You can access the combobox by name from the Shapes-collection of the sheet (basically everything that is put on a sheet but not within an cell is a Shape). However, as you are dealing with ActiveX-controls, this is a little bit ugly.
Dim sh as Shape
Set sh = TheSheet.Shapes("ComboProva")
With sh.DrawingObject.Object
Call .AddItem("Variante x")
End With
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 have inserted a combobox in sheet 7 ( named "Mainwindow").
I can't find information on how to access the combobox through VBA?
I though something like:
Sheets("Mainwindow").combbox_Instruments.value but it does not work.
Any ideas where one can read more about this or find a fix?
Try Sheets("Mainwindow").OLEObjects("combbox_instruments").Value
Also, OLE objects are member of the shapes collection, so you can iterate through all the shapes in a worksheet, however the above approach should suffice.
An ActiveX combobox can be accessed as follows:
Sub Test()
Dim obj As OLEObject
Set obj = Sheets("Mainwindow").OLEObjects("combbox_instruments")
End Sub