+ Reply to Thread
Results 1 to 5 of 5

Macro Help - Loops

  1. #1
    Alan P
    Guest

    Macro Help - Loops

    I'm trying to set up a macro to help search through a column of numbers to
    find out which two (or more) add to an input number, and then show the
    numbers in a box. I'm starting with a basic loop that I've used before and
    I can't get it past the Do While point.

    Can anyone tell me what I'm missing?

    Sub Find_Combos_That_Add_to_Input_Value()

    'Start at the currently selected cell
    Dim x As Integer

    x = ActiveCell.Row
    'Loop

    Do While Cells(x, 0).Value <> ""

    'If the values of the first row plus the next row equal the
    input number
    'show the message, otherwise go to the next row until the end

    If (Cells(0, 0).Value + Cells(x, 0).Value = 5) Then

    MsgBox ("Values are" & Cells(0, 0).Value & "and" & Cells(x, 0).Value)

    Else
    'increase the value of rownum by 1 to move the loop starting
    point to the next row

    End If

    x = x + 1

    Loop

    End Sub


  2. #2
    Tom Ogilvy
    Guest

    Re: Macro Help - Loops

    Cells(1,1) is A1, so you can't use zero as an index

    Sub Find_Combos_That_Add_to_Input_Value()

    'Start at the currently selected cell
    Dim x As Long, c as Long, i as Long

    x = ActiveCell.Row
    c= ActiveCell.Column
    'Loop

    Do While Cells(x, c).Value <> ""
    i = 1
    do while cells(x + i, c) <> ""

    If (Cells(x, c).Value + Cells(x+i, c).Value = 5) Then

    MsgBox "Values are" & Cells(x, c).Value & _
    " and " & Cells(x+i, c).Value
    exit sub
    End If
    i = i + 1
    Loop

    x = x + 1

    Loop

    End Sub

    --
    Regards,
    Tom Ogilvy



    "Alan P" <[email protected]> wrote in message
    news:[email protected]...
    > I'm trying to set up a macro to help search through a column of numbers to
    > find out which two (or more) add to an input number, and then show the
    > numbers in a box. I'm starting with a basic loop that I've used before

    and
    > I can't get it past the Do While point.
    >
    > Can anyone tell me what I'm missing?
    >
    > Sub Find_Combos_That_Add_to_Input_Value()
    >
    > 'Start at the currently selected cell
    > Dim x As Integer
    >
    > x = ActiveCell.Row
    > 'Loop
    >
    > Do While Cells(x, 0).Value <> ""
    >
    > 'If the values of the first row plus the next row equal the
    > input number
    > 'show the message, otherwise go to the next row until the end
    >
    > If (Cells(0, 0).Value + Cells(x, 0).Value = 5) Then
    >
    > MsgBox ("Values are" & Cells(0, 0).Value & "and" & Cells(x, 0).Value)
    >
    > Else
    > 'increase the value of rownum by 1 to move the loop starting
    > point to the next row
    >
    > End If
    >
    > x = x + 1
    >
    > Loop
    >
    > End Sub
    >




  3. #3
    Bob Phillips
    Guest

    Re: Macro Help - Loops

    The Cells property refers to a row number and a column number/letter, so 0
    is an invalid value (no 0 row/column).

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Alan P" <[email protected]> wrote in message
    news:[email protected]...
    > I'm trying to set up a macro to help search through a column of numbers to
    > find out which two (or more) add to an input number, and then show the
    > numbers in a box. I'm starting with a basic loop that I've used before

    and
    > I can't get it past the Do While point.
    >
    > Can anyone tell me what I'm missing?
    >
    > Sub Find_Combos_That_Add_to_Input_Value()
    >
    > 'Start at the currently selected cell
    > Dim x As Integer
    >
    > x = ActiveCell.Row
    > 'Loop
    >
    > Do While Cells(x, 0).Value <> ""
    >
    > 'If the values of the first row plus the next row equal the
    > input number
    > 'show the message, otherwise go to the next row until the end
    >
    > If (Cells(0, 0).Value + Cells(x, 0).Value = 5) Then
    >
    > MsgBox ("Values are" & Cells(0, 0).Value & "and" & Cells(x, 0).Value)
    >
    > Else
    > 'increase the value of rownum by 1 to move the loop starting
    > point to the next row
    >
    > End If
    >
    > x = x + 1
    >
    > Loop
    >
    > End Sub
    >




  4. #4
    Gary''s Student
    Guest

    RE: Macro Help - Loops

    Reconsider Cells(x,0)...perhaps Cells(x,1)?
    ________________________
    Gary's Student


    "Alan P" wrote:

    > I'm trying to set up a macro to help search through a column of numbers to
    > find out which two (or more) add to an input number, and then show the
    > numbers in a box. I'm starting with a basic loop that I've used before and
    > I can't get it past the Do While point.
    >
    > Can anyone tell me what I'm missing?
    >
    > Sub Find_Combos_That_Add_to_Input_Value()
    >
    > 'Start at the currently selected cell
    > Dim x As Integer
    >
    > x = ActiveCell.Row
    > 'Loop
    >
    > Do While Cells(x, 0).Value <> ""
    >
    > 'If the values of the first row plus the next row equal the
    > input number
    > 'show the message, otherwise go to the next row until the end
    >
    > If (Cells(0, 0).Value + Cells(x, 0).Value = 5) Then
    >
    > MsgBox ("Values are" & Cells(0, 0).Value & "and" & Cells(x, 0).Value)
    >
    > Else
    > 'increase the value of rownum by 1 to move the loop starting
    > point to the next row
    >
    > End If
    >
    > x = x + 1
    >
    > Loop
    >
    > End Sub
    >


  5. #5
    Alan P
    Guest

    RE: Macro Help - Loops

    Thanks, I figured it out. You were on the right track that it couldn't be 0
    - I was thinking 0 relative to initial position but it was a non-starter - I
    designated other variables and it worked.

    Thanks for your help.

    "Gary''s Student" wrote:

    > Reconsider Cells(x,0)...perhaps Cells(x,1)?
    > ________________________
    > Gary's Student
    >
    >
    > "Alan P" wrote:
    >
    > > I'm trying to set up a macro to help search through a column of numbers to
    > > find out which two (or more) add to an input number, and then show the
    > > numbers in a box. I'm starting with a basic loop that I've used before and
    > > I can't get it past the Do While point.
    > >
    > > Can anyone tell me what I'm missing?
    > >
    > > Sub Find_Combos_That_Add_to_Input_Value()
    > >
    > > 'Start at the currently selected cell
    > > Dim x As Integer
    > >
    > > x = ActiveCell.Row
    > > 'Loop
    > >
    > > Do While Cells(x, 0).Value <> ""
    > >
    > > 'If the values of the first row plus the next row equal the
    > > input number
    > > 'show the message, otherwise go to the next row until the end
    > >
    > > If (Cells(0, 0).Value + Cells(x, 0).Value = 5) Then
    > >
    > > MsgBox ("Values are" & Cells(0, 0).Value & "and" & Cells(x, 0).Value)
    > >
    > > Else
    > > 'increase the value of rownum by 1 to move the loop starting
    > > point to the next row
    > >
    > > End If
    > >
    > > x = x + 1
    > >
    > > Loop
    > >
    > > End Sub
    > >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1