I am trying to write VBA code that will allow me to select an excel file. It will then give me a combo box that is populated with a list of rates (5Y treasury, 10Y, etc), and based on my selection import the historical rates into my active workbook. I think its safe to say at this point that I have no idea what I'm doing.
I get an error that says
Method 'Range' of object'_Worksheet' failed.
Any help would be greatly appreciated.
Sub Button1_Click()
Dim fd As FileDialog
Dim strFile As String
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Integer
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please select a file"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show = True Then
strFile = .SelectedItems(1)
Set wb = Workbooks.Open(strFile)
Set ws = wb.Sheets("output-M")
'Populate combo box with Rates from column E
For i = 3 To ws.Range("E" & Rows.Count).End(xlUp).Row
UserForm1.ComboBox1.AddItem ws.Range("E" & i).Value
Next i
UserForm1.Show
'Import selected rate range to A1:A10 of active sheet
For i = 1 To 10
ActiveSheet.Range("A" & i).Value = Application.WorksheetFunction.VLookup(UserForm1.ComboBox1.Value, ws.Range("E:BQ100"), i + 1, False)
ActiveSheet.Range("A" & i).Value = UserForm1.ComboBox1.Value & " - " & ws.Range("F" & UserForm1.ComboBox1.ListIndex + 2 + i).Value
Next i
wb.Close False
End If
End With
End Sub