I'm a beginner programmer and am making VBA macro for my senior capstone project. I'm trying to populate a listbox with the data in "A column". It must be dynamic, because the user will edit the data. I know its simple for a seasoned coder, but I'm having problems with the syntax. Any help is greatly appreciated!
Private Sub UserForm_Initialize()
Dim LastRowControllers, LastRowBrakes As Integer
Dim Brakes, Controllers As Range
With Worksheets("ControllersInventory")
LastRowControllers = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With Worksheets("BrakesInventory")
LastRowBrakes = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Set Controllers = Range(Cells(1, 1), Cells(LastRowControllers, 1))
Set Brakes = Range(Cells(1, 1), Cells(LastRowBrakes, 1))
'Populate Controller_List
Worksheets("ControllerInventory").Select
With Controller_List
.RowSource "= Controllers"
End With
'Populate Brake_List
Worksheets("BrakeInventory").Select
With Brake_List
.RowSource "= Brakes"
End With
End Sub
I found an alternative way for populating listboxes, however my syntax is wrong and I would like to use proper coding techniques by using declared ranges.
Brake_List.RowSource = Worksheets("BrakeInventory").Range(Cells(1, 1), Cells(LastRowControllers, 1)).Address
For example:
Dim Rws As Long, Rng As Range
Rws = Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Range(Cells(1, 1), Cells(Rws, 1))
ListBox1.List = Rng.Value
Dispense with the .Select
it doesn't buy you anything
.RowSource
is expecting a range. You're trying to assign it a string value (denoted by the ""), but you're not getting that far because the =
sign is in the quotes.
With Controller_List
.RowSource = Controllers
End With
'Populate Brake_List
With Brake_List
.RowSource = Brakes
End With
I see you often forget to reference the worksheet when using Range
or Cells
.
Option Explicit
Private Sub UserForm_Initialize()
'first error when declaring variables, you declared some as variant
Dim LastRowControllers as Long, LastRowBrakes As Long 'rows can go more than 65535 (integer limit) , so i use Long
Dim Brakes as Range, Controllers As Range
With Worksheets("ControllersInventory")
LastRowControllers = .Cells(.Rows.Count, "A").End(xlUp).Row
Set Controllers = .Range(.Cells(1, 1), .Cells(LastRowControllers, 1))
End With
With Worksheets("BrakesInventory")
LastRowBrakes = .Cells(.Rows.Count, "A").End(xlUp).Row
Set Brakes = .Range(.Cells(1, 1), .Cells(LastRowBrakes, 1))
End With
'Populate Controller_List
With Controller_List
'.List= Controllers.value 'works but not dynamic
.Rowsource = Controllers.Parent.Name & "!" & Controllers.Address
End With
'Populate Brake_List
With Brake_List
'.List = Brakes.value 'works but not dynamic
.Rowsource = Brakes.Parent.Name & "!" & Brakes.Address ' example = "Sheet1!$A$1:$A$10"
End With
Set Brakes = Nothing
Set Controllers = Nothing
End Sub
Also note, if the data needs to be dynamic, i think the userform must be called this way:
Userform1.Show False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.