using string value instade of array name in vba - arrays

i have some array and a combobox in excel vba, when from is active program set hozelistname value to combobox1. arrays is like below:
hozelistname=("zahedan","zabol")
hozezahedan=(1,2,3,4)
hozezaol=(5,6,7)
now when combobox1 change, i want it show the list of selected array. for example when i select zahedan from list i want it alert the value of zahedan array named hozezahedan and also for zabol.
i wrote the below code but not working! any body to help me?
Private Sub ComboBox1_Change()
dim arrayname,str as string
dim i as integer
arrayname = "hoze" & ComboBox1.text
for i= lbound(arrayname) to ubound(arrayname)
msgbox(arryname(i))
next
End Sub

Option Explicit
Private dicArrays As Scripting.Dictionary
Sub SetUP()
Dim arrTestOne(5) As String
Dim arrTestTwo(10) As String
Set dicArrays = New Scripting.Dictionary
dicArrays.Add "TestOne", arrTestOne
dicArrays.Add "TestTwo", arrTestTwo
End Sub
Sub Reference_Example()
Dim a() As String
a = dicArrays("TestTwo")
End Sub
I hope this explains a little better.

Its unclear what you are after but does the below help you through it?
'This block of code goes right at the top of the form
Option Explicit
Dim hozelistname(1) As String
Dim hozezahedan(3) As String
Dim hozezabol(2) As String
Private Sub ComboBox1_Change()
Dim i As Integer
Dim AryLocal() As String
'set the local array based on the combobox
Select Case Me.ComboBox1
Case "zahedan"
AryLocal = hozezahedan
Case "zabol"
AryLocal = hozezabol
Case Else
ReDim AryLocal(0)
End Select
'show the local array
For i = 0 To UBound(AryLocal, 1)
MsgBox (AryLocal(i))
Next
End Sub
Private Sub UserForm_Activate()
Dim LngCounter As Long
'Set up the lists
hozelistname(0) = "zahedan"
hozelistname(1) = "zabol"
'Set up each result
hozezahedan(0) = "1"
hozezahedan(1) = "2"
hozezahedan(2) = "3"
hozezahedan(3) = "4"
hozezabol(0) = "5"
hozezabol(1) = "6"
hozezabol(2) = "7"
'set the combobox
For LngCounter = 0 To UBound(hozelistname, 1)
Me.ComboBox1.AddItem hozelistname(LngCounter)
Next
End Sub
The next steps would be populating the ComboBox and arrays based on list written in Excel and not hardcoded ones.

Related

Populating ListBox in Userform with each element of an array

I have the following problem: In one module I created an array "arArray" with values from a specific column. Each value of said array represents a weeknumber based on another dataset. The array is defined in another module as Public under Option Explicit. This works perfectly. Below the code for the array.
Dim arrayData As Range
Dim sh As Worksheet
Dim Row_Count As Integer
Dim i As Integer
Dim lastRow2 As Long
Set sh = ThisWorkbook.Sheets("Import")
lastRow2 = sh.Columns(45).Find("*", , , , xlByRows, xlPrevious).Row
Set arrayData = sh.Range("AS2:AS" & lastRow2)
arArray = sh.Range("AS2:AS" & lastRow2)
Row_Count = arrayData.Rows.Count
For i = 1 To Row_Count
arArray(i, 45) = Cells(i, 45).Value
Next i
Now I'm trying to fill all values of "arArray" into the Listbox "Lst_Tabellen" of the UserForm "Auswertung". Below the code for Initialization:
Public Sub UserForm_Initialize()
With Lst_Tabellen
Lst_Tabellen.Clear
Lst_Tabellen = arArray
End With
End Sub
I'm calling this userform in another sub:
Public Sub Call_Userform()
Auswertung.Show
End Sub
My problem is, that the Userform opens, but without any values. I never programmed a userform before, so I'm really stuck here. Can you guys see my error?

VBA add array to a list

Sub RangeBulkAmend()
Set list = list.CreateInstance
Dim c As Range
Dim i As Long
Dim myarr() As Variant
For Each c In Selection
list.Add c.value
Next c
ReDim myarr(list.Count - 1)
For i = 1 To list.Count - 1
myarr(i) = list.Items(i)
msg = msg & vbCrLf & myarr(i)
Next i
{{ListWindow.ListBox1.list = myarr}}
Load ListWindow
ListWindow.Show
end sub
i have an error on the compile as I try to pass my array to a list the code with double braces that where the compiler points too but if I highlight i get the message Object variable or With block variable not set any help will be gladly appreciated thank you
in advance please note the list refereed to in the code above is my own custom list the issue is sending the array to the list box at the double braces checked the code it produces something now to extract that to a list box
If it is only your intention to load a list box with the selected cells values then:
Sub RangeBulkAmend()
Dim myarr() As Variant
myarr = Selection.Value
Load ListWindow
ListWindow.ListBox1.List = myarr
ListWindow.Show
End Sub
Will do it
Or for that matter simply skipping the whole and just assigning the selection.Value to the listbox also works:
Sub RangeBulkAmend()
Load ListWindow
ListWindow.ListBox1.List = Selection.Value
ListWindow.Show
End Sub
To mass add to an existing list in a list box try this:
Sub RangeBulkAmend()
Load ListWindow
Dim myarr() As Variant
Dim oldarr() As Variant
Dim t&, i&
myarr = Selection.Value
t = ListWindow.ListBox1.ListCount
ReDim oldarr(0 To (ListWindow.ListBox1.ListCount + UBound(myarr, 1) - 1)) As Variant
For i = 0 To UBound(oldarr)
If i < ListWindow.ListBox1.ListCount Then
oldarr(i) = ListWindow.ListBox1.List(i)
Else
oldarr(i) = myarr(i - t + 1, 1)
End If
Next i
ListWindow.ListBox1.List = oldarr
ListWindow.Show modal
End Sub

Populating VBA array from a textbox

I am trying to get the value of a textbox and save it to a string array by clicking a button. I am getting a syntax error on the name(0) = TextBox1.Text line. I will be making it a dynamic array once I figure out how to do it (that's why I have a num variable as well because the button can be clicked multiple times).
Am I doing something wrong here?
Private Sub CommandButton1_Click()
Dim name(10) As String
Dim num As Integer
' Debug.Print TextBox1.Text
name(0) = TextBox1.Text
End Sub
'Name' is a built in name for excel.
Try:
Private Sub CommandButton1_Click()
Dim testArray(1 to 11) As String
Dim num As Integer
' Debug.Print TextBox1.Text
testArray(1) = TextBox1.Text
End Sub
EDIT: Changed the name of the array.

VBA Count Values in Array

I've read the post on this VBA problem, but my VBA script is still not working.
Public Sub Test()
Dim arrNames As Variant 'Declare array named "arrNames"
arrNames = Sheet1.Range("F2:F1000") 'Array filled with column F
intN = Application.CountIf(arrNames, "*") 'does not work intent: count cells w/info
'intN = Application.CountA(arrNames) 'works but MsgBox displays value of 999
MsgBox (intN)
End Sub
How do I get the number of cells in my array containing any value?
EDITED version after help
Public Sub Test()
Dim arrNames As Variant 'Declare array named "arrNames"
Dim i As Long
arrNames = Sheet1.Range("F2:F1000") 'Array filled with column F
For i = LBound(arrNames) To UBound(arrNames)
If (arrNames(i,1) = "") Then
EmptyCounter = EmptyCounter + 1
End If
Next i
End Sub
There is no direct way to do it, as far as I understand. But you could run a simple loop to check if the values are equal to "" assuming string data.
For e.g.
For i = LBound(ArrayName) to UBound(ArrayName)
If (ArrayName(i) = "") Then
EmptyCounter = EmptyCounter + 1
End If
Next i
If it's numeric or other type of data, you may try variations of the above loop using functions such as IsEmpty(VariableName) etc.
You can try this:
intN = Worksheets("Sheet1").Range("F2:F1000").Cells.SpecialCells(xlCellTypeConstants).Count
MsgBox intN
100% it works.

How to assign excel cell with string to array value in visual basic?

I'm pretty new to visual basic, but I'm having trouble assigning cell values to members of an array. Basically what I am doing is looping through a column and assigning each cell to a new part of the array. Test code is posted below:
Sub Test()
Dim List(5) As String
Dim celltext As String
For i = 1 To 5
celltxt = ActiveSheet.Range("K" & i).Text
List(i) = celltext
MsgBox List(i)
Next i
End Sub
Each time the message box pops up though, it is blank, meaning that the assignment didn't work. The cells at those coordinates DO have values. Any clues?
You are assigning to "celltxt" but reading from "celltext".
Add Option Explicit at the top of every module -- that will make these types of errors more obvious.
When you Dim List(5) As String. The lowest element in the array is 0 and not 1. You might want to change that to Dim List(1 to 5) As String else your first element will always be blank in the array.
You are using ActiveSheet. Are you sure it is the right sheet?
Try this code
Sub Test()
Dim List(1 To 5) As String
Dim ws As Worksheet
Dim i as Long
Set ws = ThisWorkbook.Sheets("Sheet1")
For i = 1 To 5
List(i) = ws.Range("K" & i).Value
MsgBox List(i)
Next i
End Sub
You might also with to try:
Dim List As Variant
Dim i As Long
List = ActiveSheet.Range("K1:K5")
For i = 1 To UBound(List)
MsgBox List(i, 1)
Next i
This will add performance by only reading from the worksheet once instead of each time the loop is looped.

Resources