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.
Related
I classify myself as a beginner in programing. I have a userform that first looks up a number presented by the user. Example 12345, 12346,12347. The number entered into the textbox is searched for and then added to the listbox as a valid number. After the user enters all the numbers needed, they should be able to click change and update the records accordingly.
Private Sub Change_Click()
Dim i As Long
For i = LBound(RunArray) To UBound(RunArray)
' Code to update each record, still working on it.
Next
End Sub
Private Sub RunNumber_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim RunArray() As String
Dim RunCount As Integer
RunCount = 0
If KeyCode = 13 Then
With Sheets("Sheet1").Range("A:A")
Set RunFind = .Find(What:=RunNumber, _
After:=.Cells(.Cells.count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not RunFind Is Nothing Then
ReDim Preserve RunArray(RunCount)
RunArray(RunCount) = RunNumber.Value
RunNumberList.AddItem RunNumber.Value
RunNumber.Value = ""
RunCount = RunCount + 1
Else
MsgBox "The Run Number you entered was not found, Please the number and try again."
RunNumber.Value = ""
End If
End With
End If
End Sub
Private Sub CreateArrayFromListbox()
Dim nIndex As Integer
Dim vArray() As Variant
' dimension the array first instead of using 'preserve' in the loop
ReDim vArray(ListBox1.ListCount - 1)
For nIndex = 0 To ListBox1.ListCount - 1
vArray(nIndex) = ListBox1.List(nIndex)
Next
End Sub
i have an example how to do it with a combobox, (it's the same with a listbox, just change the name accordingly.
Option Explicit
Private Sub UserForm_Initialize()
Dim i&
Dim Arr()
With Me.ComboBox1
For i = 1 To 1000
.AddItem "Item " & i
Next i
Arr = .List
.Clear
End With
For i = 0 To 999
Debug.Print Arr(i, 0)
Next i
Erase Arr
End Sub
this is just a sample code, in real coding, you won't clear the combobox this early, or erase the array.
The results are shown in the immediate window (alt-F11 , and Ctrl-g).
Note : the array is 2 dimendionned, and 0 based (Option base 1, after Option Explicitcan make the whole module 1-based (arrays start at 1) ).
I'm learning vb.net and have a problem. Short version of code.
I have structure like this:
public class form234
public array1() as string
public sub button1
Dim i As Integer
Dim array1(Len(text)) As String
For i = 1 To 3
array1(i) = "x"
TextBox2.Text = TextBox2.Text & array1(i) & " "
Next
When I put a msgbox here I get the content of my array
......
public sub button2
When I put a msgbox here I get error and array1 is "nothing"
...........
Why? How can I use my array value of sub button1 in sub button 2 as well?
I'd write that as:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ReDim array1(Text.Length)
For i As Integer = 0 To array1.Length - 1
array1(i) = "x"
TextBox2.Text = String.Join(" ", array1)
Next
End Sub
your problem is that you declared a second time your array1 in public sub button1.
if you want really to use the one that you declared public, you have to give him a size, the thing that you can do in his first declaration.
Else an alternative solution can be :
Dim i As Integer
Array.Resize(array1, Len(Text))
For i = 1 To 3
array1(i) = "x"
TextBox2.Text = TextBox2.Text & array1(i) & " "
Next
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?
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.
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.