+ Reply to Thread
Results 1 to 5 of 5

Increment numbers in column A by 1 when selected

  1. #1
    Forum Contributor
    Join Date
    06-23-2005
    Posts
    253

    Increment numbers in column A by 1 when selected

    Need code to increment a number from the cell above in column A when column A cell is selected. Have tried using the following code but the number does not increment by 1, it merely copies the number from the cell above.

    (1) Why does the following code not work?

    (2) What would be the modification to make the following code work or what would be the simple code to take the number from above in column A, add 1, & put it in the selected cell of column A if the selected cell is blank, empty, or -0-?

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    R = Target.Row
    If Target.Row > 3 And Target.Column = 1 And Cells(R, 1) = 0 _
    And Cells((R - 1), 1).Value > 0 Then
    Cells(R, 1).Value = Cells((R - 1), 1).Value + 1
    End If
    End Sub

    Thanks a million. mikeburg

  2. #2
    Ardus Petus
    Guest

    Re: Increment numbers in column A by 1 when selected

    Your code generates an error if you select A1

    '----------
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
    If .Row > 3 And .Column = 1 Then
    If .Value = 0 And .Offset(-1, 0).Value > 0 Then
    .Value = .Offset(-1, 0).Value + 1
    End If
    End If
    End With
    End Sub
    '-----------

    "mikeburg" <[email protected]> a écrit
    dans le message de
    news:[email protected]...
    >
    > Need code to increment a number from the cell above in column A when
    > column A cell is selected. Have tried using the following code but the
    > number does not increment by 1, it merely copies the number from the
    > cell above.
    >
    > (1) Why does the following code not work?
    >
    > (2) What would be the modification to make the following code work or
    > what would be the simple code to take the number from above in column
    > A, add 1, & put it in the selected cell of column A if the selected
    > cell is blank, empty, or -0-?
    >
    > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    > R = Target.Row
    > If Target.Row > 3 And Target.Column = 1 And Cells(R, 1) = 0 _
    > And Cells((R - 1), 1).Value > 0 Then
    > Cells(R, 1).Value = Cells((R - 1), 1).Value + 1
    > End If
    > End Sub
    >
    > Thanks a million. mikeburg
    >
    >
    > --
    > mikeburg
    > ------------------------------------------------------------------------
    > mikeburg's Profile:

    http://www.excelforum.com/member.php...o&userid=24581
    > View this thread: http://www.excelforum.com/showthread...hreadid=536399
    >




  3. #3
    Tom Ogilvy
    Guest

    RE: Increment numbers in column A by 1 when selected

    The code worked fine for me.

    --
    Regards,
    Tom Ogilvy


    "mikeburg" wrote:

    >
    > Need code to increment a number from the cell above in column A when
    > column A cell is selected. Have tried using the following code but the
    > number does not increment by 1, it merely copies the number from the
    > cell above.
    >
    > (1) Why does the following code not work?
    >
    > (2) What would be the modification to make the following code work or
    > what would be the simple code to take the number from above in column
    > A, add 1, & put it in the selected cell of column A if the selected
    > cell is blank, empty, or -0-?
    >
    > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    > R = Target.Row
    > If Target.Row > 3 And Target.Column = 1 And Cells(R, 1) = 0 _
    > And Cells((R - 1), 1).Value > 0 Then
    > Cells(R, 1).Value = Cells((R - 1), 1).Value + 1
    > End If
    > End Sub
    >
    > Thanks a million. mikeburg
    >
    >
    > --
    > mikeburg
    > ------------------------------------------------------------------------
    > mikeburg's Profile: http://www.excelforum.com/member.php...o&userid=24581
    > View this thread: http://www.excelforum.com/showthread...hreadid=536399
    >
    >


  4. #4
    Ardus Petus
    Guest

    Re: Increment numbers in column A by 1 when selected

    If Target.Row > 3 And Target.Column = 1 And Cells(R, 1) = 0 _
    And Cells((R - 1), 1).Value > 0 Then

    Cells((R-1),1) errs if R = 1

    --
    AP

    "Tom Ogilvy" <[email protected]> a écrit dans le message
    de news:[email protected]...
    > The code worked fine for me.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "mikeburg" wrote:
    >
    > >
    > > Need code to increment a number from the cell above in column A when
    > > column A cell is selected. Have tried using the following code but the
    > > number does not increment by 1, it merely copies the number from the
    > > cell above.
    > >
    > > (1) Why does the following code not work?
    > >
    > > (2) What would be the modification to make the following code work or
    > > what would be the simple code to take the number from above in column
    > > A, add 1, & put it in the selected cell of column A if the selected
    > > cell is blank, empty, or -0-?
    > >
    > > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    > > R = Target.Row
    > > If Target.Row > 3 And Target.Column = 1 And Cells(R, 1) = 0 _
    > > And Cells((R - 1), 1).Value > 0 Then
    > > Cells(R, 1).Value = Cells((R - 1), 1).Value + 1
    > > End If
    > > End Sub
    > >
    > > Thanks a million. mikeburg
    > >
    > >
    > > --
    > > mikeburg
    > > ------------------------------------------------------------------------
    > > mikeburg's Profile:

    http://www.excelforum.com/member.php...o&userid=24581
    > > View this thread:

    http://www.excelforum.com/showthread...hreadid=536399
    > >
    > >




  5. #5
    Forum Contributor
    Join Date
    06-23-2005
    Posts
    253
    Thanks a million. mikeburg

+ 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