I'm using Excel VBA. I need to create a macro button that launches a user form. The user form will ask for 3 arguments. "Worksheet Name", "Number of Countries", and "Order" (the first 2 inputs will be given in text boxes, but "Order" will be from a combo box). The macro should create a new sheet in the workbook, named whatever the user inputs for "Worksheet Name." There is an existing sheet in this workbook called "Countries", which lists some countries beginning at cell A2, and continuing down column A. Depending on the input for "Number of Countries", this macro should copy that number of countries from the existing list, and paste them onto the newly created worksheet. Finally, if the user selects "Reverse" as their input for "Order", the list should be flipped.
For example... you open the macro, enter "New Stuff", "5", and select "Reverse". After clicking "OK", Excel should create a New Stuff sheet where it pastes:
Chile Canada Britain Brazil Australia Argentina
This should all be done treating these lists as arrays.
Right now, I have a user form titled CreateList. It has text boxes titled SheetText and NumRows, and a combobox titled OrderList (which I want "Normal" and "Reverse" in as options).
The userform connects to the following code
Private Sub CreateList_Initialize()
OrderList.AddItem "Normal"
OrderList.AddItem "Reverse"
OrderList.ListIndex = 0
End Sub
Private Sub OKButton_Click()
Call CountrycPasting(SheetText.Value, NumRows.Value, OrderList.Value)
Unload Me
End Sub
Which connects to the following code:
Option Explicit
Sub CountryPasting(SheetText As String, NumRows As Integer, OrderList As String)
Dim Countries(NumRows) As Integer 'here's what my array should be
Dim Row As Integer
Worksheets.Add Before:=Worksheets(1)
ActiveSheet.Name = SheetText
Worksheets("Countries").Range(A2).Select
For Row = 1 To NumRows
Countries(Row) = Selection.Value
Selection.Offset(1, 0).Select
Next Row
Worksheet(SheetText).Range(A3).Select
For Row = 1 To NumRows
Selection.Value = Countries(Row)
Selection.Offset(1, 0).Select
Next Row
End Sub
Sub Load_Form()
CreateList.Show
End Sub
A bunch of problems here. First of all, "Normal" and "Reverse" don't even show up as options in the combo box on the user form. Also, I have no idea what to do about the reversing of the list. Something like, if OrderList.Value = Reverse then .... . And when I try to run this with just the first couple inputs, I get the error message "constant expression required" in regards to the "Dim Countries(NumRows) As Integer" line (I've tried dimming as a string, as well, to no avail).