Trouble with Simple Macro- do until, row counter, and inputbox

hgsouthall

New Member
Joined
Apr 25, 2013
Messages
1
Hello! I'm having trouble with what should be a relatively simple macro. I'm trying to input a specific number of coordinates. The number of coordinates is determined by the user through an input box. When the row number equals the inputbox number, I would like the inputbox prompt to stop asking for more coordinates. This is what I have so far.

Code:
Option Explicit
Sub Cholera()

DIM i as integer:                              'Row counter
DIM M(100,2) as integer:                   'Array for coordinates, 100 = limit

i = 1 M = InputBox("Maximum number of cases", , 0)




    Do Until i = M
    
        Cells(i, 1) = InputBox("x coordinates for cases", , "")
            O(i, 1) = Cells(i, 1)
        Cells(i, 2) = InputBox("y coordinates for cases", , "")
            O(i, 2) = Cells(i, 2)
        i = i + 1
    Loop

I would appreciate any help you could give me.
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
What problem(s) are you having? What's the purpose of the 2-D array O()?
 
Upvote 0
Couple things I noticed off the bat....

You have M declared like a multidimensional array. If you want to limit the number it'll have to be done differently.

Example in code below:

i and M are on the same line. (split them)

Besides that. I commented out 2 lines that don't make sense. What do you expect them to do? The way you have it, it thinks that O should be a function or a sub that you're calling. But it's written in the format like you would writing directly to cells(i, 1).... so I'm not sure what you're expecting it to do.

Code:
Sub Cholera()

Dim i As Integer                              'Row counter
Dim M As Variant                  'Array for coordinates, 100 = limit

i = 1

retry:
M = InputBox("Maximum number of cases")
If M > 100 Or M <= 0 Then
    ans = MsgBox("Number exceeds 100 or is equal to or less than 0", vbRetryCancel + vbCritical)
        If ans = vbRetry Then GoTo retry
        If ans = vbCancel Then Exit Sub
ElseIf M = "" Then Exit Sub
End If

Do Until i = M
    Cells(i, 1) = InputBox("x coordinates for cases", , "")
'        O(i, 1) = Cells(i, 1)
    Cells(i, 2) = InputBox("y coordinates for cases", , "")
'        O(i, 2) = Cells(i, 2)
    i = i + 1
Loop

End Sub
 
Last edited:
Upvote 0
Was trying to fix an error in the above code, but ran out of time. Sorry.... This also covers the error you would get if someone clicked Cancel on the first input box. You'd want to address other input boxes in a similar fashion.

Code:
Sub Cholera()

Dim i As Integer                              'Row counter
Dim M As Variant                  'Array for coordinates, 100 = limit

i = 1

retry:
M = InputBox("Maximum number of cases")
If M = "" Then Exit Sub
If M > 100 Or M <= 0 Then
    ans = MsgBox("Input was not a number between 1 and 100", vbRetryCancel + vbCritical)
        If ans = vbRetry Then GoTo retry
        If ans = vbCancel Then Exit Sub
End If

Do Until i = M
    Cells(i, 1) = InputBox("x coordinates for cases", , "")
'        O(i, 1) = Cells(i, 1)
    Cells(i, 2) = InputBox("y coordinates for cases", , "")
'        O(i, 2) = Cells(i, 2)
    i = i + 1
Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top