Search code examples
excelvbavariablesinputbox

Link variables representing rows, with several inputbox


I have a macro with two input boxes.

I would like to do the following:
Using the first input box, enter a number.
Using the second input box, enter a second number.
Copy the rows related to the line numbers.

For example, the user enters the number 1 into the first input box and enters the number 2 into the second input box. I would like to copy the rows 1 and 2 simultaneously.
With my current macro row 12 is copied.

I think the problem comes from line: Rows(Chosennumber & Chosennumber2).Copy

Sub selectlinefiletemplat()
    Dim Chosennumber As Integer
    Dim Chosennumber2 As Integer

    On Error GoTo NothingChosen

    Chosennumber = Application.InputBox( _
      prompt:="Type in a number", _
      Default:="Type your number here", _
      Type:=1)

    Chosennumber2 = Application.InputBox( _
      prompt:="Type in a number", _
      Default:="Type your number here", _
      Type:=1)

    Rows(Chosennumber & Chosennumber2).Copy

    Exit Sub

    NothingChosen:
        MsgBox "You didn't choose anything!"
End Sub

Solution

  • Try with Application.Union:

    Application.Union(Rows(Chosennumber), Rows(Chosennumber2)).Copy
    

    You doing it wrong because & concatenates string, so when you enter 1 and 2, concatenation is 12 :)