TierStructureTierStructureI am trying to populate two comb boxes on a userform w/ VBA and set a default value for the same boxes based on a ws function. I think I can figure out the ws function, but I'm having a little trouble with arrays and the default values.
Private Sub UserForm_Initialize()
Dim TierStructure() As Variant
TierSturucture = Array("Composite", "2-Tier", "3-Tier", "4-Tier", "5-Tier", "6-Tier")
StopLossCombo.List = TierSturucture
AdminCombo.List = TierSturucture
StopLossCombo.Value = TierStructure(1)
AdminCombo.Value = TierStructure(1)
End Sub
The error I receive is "run time error 9 - subscript out of range".
You have a typo - "TierSturucture" in two places.
To avoid this in the future, make sure each module has "Option Explicit" at the top. You can automate this via Tools>Options>Editor>Require Variable Declaration. This will make sure any variable has been declared, and thereby catch misspellings.
Also, note that you don't need to declare TierStructure as an array. Variant variables can hold arrays by themselves. So, just use:
Dim TierStructure As Variant
Related
I'm new to Excel applications with VBA.
I've been told to declare all the variables throughout my code, by using the "Option Explicit" statement.
In the code below (which is only a small part of the actual program), I'm trying to read the contents of the range C4:L6 and assigns those to the variable "Bar":
Option Base 1
Option Explicit
Sub My_PROGRAM()
Dim Bar() As Long
Bar = Worksheets("Sheet1").Range("C4:L6").Value 'Reads the contents of the range
End Sub
I keep getting a Run-Time error '13' - type mismatch.
My question is, how do I declare a 2D array? I tried Dim Bar(1 to 3, 1 to 10) but that didn't work either.
If I don't use Option explicit, the program works just fine
Option Base 1
Sub My_PROGRAM()
Bar = Worksheets("Sheet1").Range("C4:L6").Value 'Reads the contents of the range
End Sub
Thank you
I am working on a file that creates up to 120 charts based on data, variables, and format selections from the user. To do this I create a variant array to hold the charts which allows me to easily reference them for adding data, formatting, etc. This method has worked well for me so far.
Now I would like to let users make small tweaks to formatting (adjust the min and max on the axis, add or remove legend entries, etc.). To do this I would like to continue referencing the charts from an array, but I can't seem to add the existing charts to the variant array.
When I initially create the charts I use this line to add the chart to the array when it is created. I fill in appropriate parameters to place and size the chart and this seems to work fine.
Set charts(graphIndex) = activeSheet.ChartObjects.Add(...)
After creating all the charts, I think the non Global variables used are cleared from the cache (at least that is my current understanding). This means that in order to make these tweaks I need to reinitialize and redefine the variant array that I use to reference the charts. This is what I am struggling with. This is my current attempt to add the existing chart to the variant array.
charts(graphIndex) = Worksheets(activeSheetName).ChartObjects("chart name").Chart
When I run the code I am getting a "Run-time error '438': Object doesn't support property or method."
Hopefully I provided enough context, but any help on this would be greatly appreciated. This feels like it should be fairly easy, but I couldn't find any information online.
I am just guessing that in your code if you had the Set word it would have worked (However, I am not seeing the whole code, thus not sure).
This works, if you make sure to have 3 charts named "Diagramm 1", "Diagramm 2" and "Diagramm 3" on the first worksheet:
Option Explicit
Sub TestMe()
Dim cht2 As Chart
Dim varArray As Variant
With Worksheets(1)
Set cht2 = .ChartObjects("Diagramm 2").Chart
varArray = Array(.ChartObjects("Diagramm 1").Chart, cht2)
ReDim Preserve varArray(2)
Set varArray(2) = .ChartObjects("Diagramm 3").Chart
Dim cnt As Long
For cnt = LBound(varArray) To UBound(varArray)
Debug.Print varArray(cnt).Name
Next cnt
End With
End Sub
The Reedim Preserve increases the array units with one additional, while it keeps what it already has. Thus, at the end this is what we have in the locals:
I have been working on this code in which I have a userform that has a mashup of listboxes and comboboxes. So far I have populated the listboxes but for some reason I am having trouble with the comboboxes (combobox1 and combobox2).
I have managed to populate the drop-down list for combobox1, and from that list I want to 'filter' through a named range that is already called out through the 'name manager'. The named range is called Range_Books.
Range_Books references two columns and a variable number of rows in table48 on sheet BOOKS or in VBA code Sheet7. The code below is my latest iteration of attempting to accomplish what I have explained but it still has failed.
I originally was attempting to call out the range directly without the Worksheets("Sheet7"). since the named range is not on a specific sheet, but I am still not sure which is the best way to call out the range and if that is the root of my problem. I have called out the range directly without the worksheets(" ") before which is why I am so perplexed by this.
It may be important to note that when the userform is initialized, it opens a secondary workbook in order to populate the listboxes. After initialization, various actions may be done before a value is chosen for combobox1, and thus activating the function I am trying to create. This secondary workbook stays open until the userform is closed. I mention this because I am unsure if the secondary workbook is causing issues with the range object. I have been receiving trouble from VBA since adding the opening of a secondary workbook functionality to the userform.
Private Sub ComboBox1_Change()
Dim count As Integer
Dim i As Integer
count = Worksheets("Sheet7").Range("Range_Books").Rows.count
For i = 0 To count
If Worksheets("Sheet7").Range("Range_Books").Cells(i, 1) = ComboBox1.Value Then
ComboBox2.AddItem (Worksheets("Sheet7").Range("Range_Books").Cells(i, 2))
End If
Next i
End Sub
You need to either start with For i = 1 to count, or change the ranges to .Cells(i+1,1)...
Also, make sure you're referring to the correct sheet. I think this is where the crux of your issue is.
If your named range is in a worksheet with the tab name "Books", then you need to instead use count = Worksheets("Books").Range("Range_Books").Rows.count
If you want to use the "Sheet7" reference instead, you could use count = Sheet7.Range("Range_Books").Rows.count
For i = 0 To count
...
Cells(i, 1)
at this point, i = 0. Row 0 Doesn't exist.
Change i = 0 to i = 1
Use this
Private Sub ComboBox1_Change()
Dim count As Integer
Dim i As Integer
Dim ws As WorkSheet
Set ws = Sheets("Sheet7")
count = ws.Range("Range_Books").Rows.count
For i = 1 To count
If Worksheets("Sheet7").Range("Range_Books").Cells(i, 1) = ComboBox1.Value Then
ComboBox2.AddItem (Worksheets("Sheet7").Range("Range_Books").Cells(i, 2))
End If
Next i
End Sub
Thank you everyone for the help, it is very much appreciated! My final working code is below. I changed all instances of Worksheets("Sheet7") to just Sheet7. I attached a picture of the Excel Objects folder tree, as you can see I have Sheet7 which I named "Books". My confusion was that Worksheets(" ") calls out the name I assign rather than the VBA assigned name for the sheet. I also added ComboBox2.Clear that way whenever ComboBox1 changes it resets the values rather then stacking them. I hope this helps somebody in the future and thanks again to the commentors who helped me!
enter image description here
Private Sub ComboBox1_Change()
ComboBox2.Clear
Dim count As Integer
Dim i As Integer
count = Sheet7.Range("Range_Books").Rows.count
For i = 1 To count
If Sheet7.Range("Range_Books").Cells(i, 1) = ComboBox1.Value Then
ComboBox2.AddItem (Sheet7.Range("Range_Books").Cells(i, 2))
End If
Next i
End Sub
I have a Form, and I wnat to rename the sheet. Actually, program does it correctly, but when I want to use .Activate function I get an error : Type mismatch
Worksheets.Add().name = UserForm1.txtNameSur
Worksheets(UserForm1.txtNameSur).Activate
I've also tried
Worksheets("&UserForm1.txtNameSur&").Activate
Still the same.
Thanks!
GD Stefan,
The .Activate method of Worksheets is expecting either an index (integer or long) or a string, identifying the page to activate. You are passing an object in the form of an TextBox to the .Activate method. Try passing the value, rather than the whole object, as VBA gets confused as to what to do with that TextBox where it is expecting only a String or an Integer or Long variable.
i.e.:
Worksheets.Add().Name = UserForm1.txtNameSur.Value
Worksheets(UserForm1.txtNameSur.Value).Activate
That should work !
I want to point to a cell as a range in VBA. I've tried using:
Dim range
range = Sheet("sheet").Range("A1")
But this just returns the value in the range. What I actually want is the range object so I can manipulate it, e.g. by setting range.Value = "Hello"
Any ideas?
First, I strongly recommend you to make explicit declaration of variables in your code mandatory. Go to Tools - Options, in the Editor tab check "Require variable Declaration", or put Option Explicit in the first line of all your scripts.
Second, I think there is a small typo in your code, it should be Sheets.("sheet").
To answer your question, with range = Sheets("sheet").Range("A1") you are assigning a value variable, not an object. Therefore the default variable of the range object is implicitly assigned, which is value. In order to assign an object, use the Set keyword. My full example code looks like this:
Option Explicit
Public Sub Test()
Dim RangeObject As range
Set RangeObject = Sheets("Sheet1").range("A1")
RangeObject.Value = "MyTestString"
End Sub
This should put the text "MyTestString" in cell A1.
Edit: If you are using named ranges, try RangeObject.Value2 instead of RangeObject.Value. Named ranges do not have a Value property.