Trying to find an easy way to have a user prompted ONCE to select a cell in Column A, have a 1 placed in that cell, then for each cell the user "clicks" on
after that without being prompted, the next number is placed in the cell. Need to limit the total selections to 40 but be able to stop at any time. Ultimately I want to take the results of this and copy the rows selected and paste them on the same worksheet starting in A5 sorted by the numeric values in column A. The code below gives a user prompt for EACH selection and does not allow for stopping until 40 selections are made.
VBA Code:
Sub SelectAndFill()
Dim i As Long
Dim rng As Range 'declare a variable to store the selected cell
Dim firstCell As Range 'declare a variable to store the first cell
i = 1 'initialize the counter
Set firstCell = Application.InputBox("Select the first cell in column A", Type:=8) 'assign the first cell to the variable
If firstCell.Column = 1 Then 'check if the first cell is in column A
firstCell.value = i 'fill the first cell with the counter value
i = i + 1 'increment the counter
Do While i <= 40 'loop until the limit is reached
Set rng = Application.InputBox("Select a cell in column A", "Select a cell", firstCell.Offset(1, 0).Address, Type:=8) 'assign the selected cell to the variable
If rng.Column = 1 Then 'check if the selected cell is in column A
rng.value = i 'fill the cell with the counter value
i = i + 1 'increment the counter
Else
MsgBox "Please select a cell in column A only" 'display an error message
End If
Loop
Else
MsgBox "Please select a cell in column A only" 'display an error message
End If
End Sub
VBA Code: