Hi I'm new at VBA I have been writing up a code to output car manufacturers names and prices, through a user form. However when I try to execute my code it says "Subscript out of range". It refers to this line:
selectedMfsPrices(numMfsSelected) = allMfsPrices(i)
Any help would be much welcomed. Heres the rest of my code. Thanks
Private Sub cmd_ShowSelected_Click()
Dim i As Integer
'numListedItems holds total no. of manufacturers.
'numMfsSelected is used to count how many times Mfs are selected
'allCarMfs() holds the name of Cars.
'SelecetedCarMfs() holds the selected Cars.
Dim numlistedItems As Integer, numMfsSelected As Integer
Dim allCarMfs() As String, selectedCarMfs() As String
'First part holds prices of Mfs
'Second part holds selected prices of Mfs
Dim allMfsPrices() As Double, selectedMfsPrices() As Double
'list_Car...ListCount. This counts no.of items in the listbox
numlistedItems = list_CarManufacturers.ListCount
numMfsSelected = 0
ReDim allCarMfs(numlistedItems), selectedCarMfs(numlistedItems)
ReDim allMfsPrices(numlistedItems), selectedMfsPrices(numlisedItems)
'Fills allMfsPrices array from sheet 1
For i = 1 To numlistedItems
allMfsPrices(i) = Cells(i + 2, 1)
Next i
'Fills allCarMfs array from sheet 1
For i = 1 To numlistedItems
allCarMfs(i) = Cells(i + 1, 1)
Next i
'This fills in the users selected cars.
'Listboxs count from 0, so we use i-1
'If the item is selected increase the counter by 1. numMfsSelected +1
'If its selected add that Mfs to the array selectedCarMfs (line 3 of code in for loop 2)
'If selected add Mfs price to array called selectedMfsPrices
For i = 1 To numlistedItems
If list_CarManufacturers.Selected(i - 1) = True Then
numMfsSelected = numMfsSelected + 1
selectedCarMfs(numMfsSelected) = allCarMfs(i)
selectedMfsPrices(numMfsSelected) = allMfsPrices(i)
End If
Next i
'Redim preserve rediminsion array so no extra empty cells in listbox
'last lin first part name of our list box to be populated. Second part the array containing selected Mfs
ReDim Preserve selectedCarMfs(numMfsSelected)
ReDim Preserve selectedMfsPrices(numMfsSelected)
Sheet2.Activate
list_SelectedMfs.List = selectedCarMfs
End Sub
selectedMfsPrices(numMfsSelected) = allMfsPrices(i)
Any help would be much welcomed. Heres the rest of my code. Thanks
Private Sub cmd_ShowSelected_Click()
Dim i As Integer
'numListedItems holds total no. of manufacturers.
'numMfsSelected is used to count how many times Mfs are selected
'allCarMfs() holds the name of Cars.
'SelecetedCarMfs() holds the selected Cars.
Dim numlistedItems As Integer, numMfsSelected As Integer
Dim allCarMfs() As String, selectedCarMfs() As String
'First part holds prices of Mfs
'Second part holds selected prices of Mfs
Dim allMfsPrices() As Double, selectedMfsPrices() As Double
'list_Car...ListCount. This counts no.of items in the listbox
numlistedItems = list_CarManufacturers.ListCount
numMfsSelected = 0
ReDim allCarMfs(numlistedItems), selectedCarMfs(numlistedItems)
ReDim allMfsPrices(numlistedItems), selectedMfsPrices(numlisedItems)
'Fills allMfsPrices array from sheet 1
For i = 1 To numlistedItems
allMfsPrices(i) = Cells(i + 2, 1)
Next i
'Fills allCarMfs array from sheet 1
For i = 1 To numlistedItems
allCarMfs(i) = Cells(i + 1, 1)
Next i
'This fills in the users selected cars.
'Listboxs count from 0, so we use i-1
'If the item is selected increase the counter by 1. numMfsSelected +1
'If its selected add that Mfs to the array selectedCarMfs (line 3 of code in for loop 2)
'If selected add Mfs price to array called selectedMfsPrices
For i = 1 To numlistedItems
If list_CarManufacturers.Selected(i - 1) = True Then
numMfsSelected = numMfsSelected + 1
selectedCarMfs(numMfsSelected) = allCarMfs(i)
selectedMfsPrices(numMfsSelected) = allMfsPrices(i)
End If
Next i
'Redim preserve rediminsion array so no extra empty cells in listbox
'last lin first part name of our list box to be populated. Second part the array containing selected Mfs
ReDim Preserve selectedCarMfs(numMfsSelected)
ReDim Preserve selectedMfsPrices(numMfsSelected)
Sheet2.Activate
list_SelectedMfs.List = selectedCarMfs
End Sub