+ Reply to Thread
Results 1 to 9 of 9

macro that would increment the new value I am assigning in the first column by 1 each time

Hybrid View

feeko macro that would increment... 05-09-2013, 01:23 PM
Harribone Re: macro that would... 05-09-2013, 01:37 PM
JBeaucaire Re: macro that would... 05-09-2013, 01:37 PM
feeko Re: macro that would... 05-09-2013, 02:02 PM
Harribone Re: macro that would... 05-09-2013, 01:41 PM
JOHN H. DAVIS Re: macro that would... 05-09-2013, 01:43 PM
feeko Re: macro that would... 05-09-2013, 01:47 PM
JBeaucaire Re: macro that would... 05-09-2013, 03:08 PM
feeko Re: macro that would... 05-09-2013, 03:12 PM
  1. #1
    Registered User
    Join Date
    05-09-2013
    Location
    learnynge memyde
    MS-Off Ver
    Excel 2003
    Posts
    4

    macro that would increment the new value I am assigning in the first column by 1 each time

    Hi All,

    I'm new on this forum and have been reading members of this site helping others with excel problems. You all are great and want to thank you for your time and help.

    Here's my problem, need a macro that would increment the new value I am assigning in the first column by 1 each time the value in the second column changes.

    for example i have three rows in column B with the number 10025 and for each row in column A there is a 1. When the number in column B changes to let's say, 10034, the value in column A then changes to 2. When the number changes again in column B to 10043, the value you in column A will change to 3, and so on.

    Any ideas or suggestions? My boss wants me to write a macro and I have no idea how to do it as I'm learning excel recently and taking classes. thanks for your help!

  2. #2
    Valued Forum Contributor Harribone's Avatar
    Join Date
    02-24-2013
    Location
    Midlands, UK
    MS-Off Ver
    Excel 2019/365
    Posts
    570

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    A quick search on the net gave me this:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim KeyCells As Range
    
        ' The variable KeyCells contains the cells that will
        ' cause an alert when they are changed.
        Set KeyCells = Range("B1")
        
        If Not Application.Intersect(KeyCells, Range(Target.Address)) _
               Is Nothing Then
               
            
            Range("A1").Value = Range("A1").Value + 1
            
           
        End If
    End Sub
    This assumes that A1 wil increase by 1 when B1 changes. You need to open the VBA editor and then select the worksheet that this will be used on and insert the code there.
    This should then run in the background and increment A1 each time B1 changes.
    Say thanks, click *

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    In A1, put 1 to start the list.

    In A2, put this formula and copy down:

    =IF(B2=B1, A1, A1+1)

    or:

    =A1+(B2<>B1)
    Last edited by JBeaucaire; 05-09-2013 at 01:38 PM. Reason: corrected first formula
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    05-09-2013
    Location
    learnynge memyde
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    IT WORKED, thank you so so much.

  5. #5
    Valued Forum Contributor Harribone's Avatar
    Join Date
    02-24-2013
    Location
    Midlands, UK
    MS-Off Ver
    Excel 2019/365
    Posts
    570

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    Ahhh I've re read your post after seeing JBeau's post and my solution isn't what you need so please ignore this.
    JBeau's solution is exactly what you want.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    Maybe:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B1:B3")) Is Nothing Then
        Target.Offset(, -1).Value = Target.Offset(, -1).Value + 1
    End If
    End Sub

  7. #7
    Registered User
    Join Date
    05-09-2013
    Location
    learnynge memyde
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    Guys, I want to thank you so much for your help and detailed responses.

    I understand JBeau's solution and I think it does work, the only thing I don't get is the "copy down" part. Do I have to put that formula in every cell (A1, A2, A3, and so on)? There is about 1000 rows. I know I can click and drag but is that what must be done? Thank you so much!
    Last edited by feeko; 05-09-2013 at 01:53 PM.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    I've marked this thread as SOLVED for you.
    Next time, select Thread Tools from the links above and mark this thread as SOLVED. Thanks.

  9. #9
    Registered User
    Join Date
    05-09-2013
    Location
    learnynge memyde
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: macro that would increment the new value I am assigning in the first column by 1 each

    okay, will do. thanks again for you and the other's help.

+ 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