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:

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
What problem(s) are you having? What's the purpose of the 2-D array O()?
 

rjwebgraphix

Well-known Member
Joined
May 25, 2010
Messages
588
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:

rjwebgraphix

Well-known Member
Joined
May 25, 2010
Messages
588
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
 

Forum statistics

Threads
1,081,860
Messages
5,361,734
Members
400,652
Latest member
cortexnotion

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top