+ Reply to Thread
Results 1 to 4 of 4

Thread: Building Excel Macro for counting interations and cell value

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    New York, NY
    MS-Off Ver
    Excel 2003
    Posts
    8

    Building Excel Macro for counting interations and cell value

    Gentlemen, you were so quick and helpful with my project, that I have come back with an Excel question, for which I need your help.

    I would like to build a macro, with a button that I can click, that changes the value of a cell, starting with 1, add +1 each iteration, and stop at a number I choose. I’m doing this because I have a bunch of cases and self-referencing IF statements that will store the values.

    Please note that I have never built or programmed and macro and newbie-literate help would be greatly appreciated (if it doesn’t kill your brain cells too much to talk in laymen terms I am using Excel 2007.


    Step 1:

    Cell A1: 1 INPUT (input to choose the case is in this cell)
    Cell A2: 20 LAST CUSTOMER# (highest number to be counted to)

    So I want to be able to click a button, and have Cell A1 cycle from 1 to 20.


    Step 2:

    Then, I want to create another macro, that automates another layer of complexity.

    Cell A1: 1 INPUT (input to choose the case is in this cell)
    Cell A2: 20 LAST CUSTOMER# (highest number to be counted to)
    Cell A3: 30 LAST DIVISION# (highest number to be counted to)

    So I want to click a different another button, and have excel cycle through

    Customer 1, Division 1
    Customer 1, Division 2
    Customer 1, Division 3
    ...
    Customer 20, Division 30


    Thanks a ton guys,
    Josh



  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Building Excel Macro for counting interations and cell value

    Hello Josh,

    Where will these incremented values be placed?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-22-2010
    Location
    New York, NY
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Building Excel Macro for counting interations and cell value

    They will be placed back into the A1 cell

  4. #4
    Forum Guru
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,110

    Re: Building Excel Macro for counting interations and cell value

    Hi joshjpang;

    These macros will put into A1 "Customer 1, Division 1" .. "Customer 20, Division 30".
    Cell A2 must have 20 in it and Cell A3 must have 30 in it.

    They will create buttons for you.

    On the sheet
    1) Right Mouse Click sheet tab and select "View Code". This will open a window with a name at the top like this :"myWorkbook.xls - Sheet1 (Code)"
    2) Cut & Paste All this Code into that window.
    3) Click Alt+F11. This will take you back to Excel
    4) Put your cursor where you want the buttons. It will create 2 buttons that take up 5 cells down. Example: Cursor in C5, Button 1 will be in C5:C6 and Button 2 will be in C8:C9. Button 1 will say "Increment Customer", and Button 2 will say "Increment Division"

    5) Click Alt+F8. This will open the macro window.
    6) Select "Create_Buttons" and click "Run". If there is more than one macro in the list, select "This Workbook" in "Macros In:"

    To change text, Right Mouse click the button and select "Edit Text".
    To change size, Right Mouse click the button and grab one of the circles with the Left Mouse button and drag.
    To move, Right Mouse click the button and grab shaded border with the Left Mouse button and drag.
    Public Sub Increment_Customer(Optional sScratch As String)
        Dim sh As Worksheet
        Dim s As String
        Dim i As Integer
        Dim iNew As Integer
        Dim iInStr As Integer
        Dim sCust As String
    
        Set sh = ActiveSheet
        sCust = "Customer "
    
        With sh.Range("A1")
            s = .Value
            iInStr = InStr(s, sCust)
            If iInStr = 0 Then
                s = "Customer 1, Division 1"
                iInStr = InStr(s, sCust)
            End If
    
            i = Val(Mid(s, iInStr + Len(sCust)))
            If i = .Range("A2").Value Then
                iNew = 1
            Else
                iNew = i + 1
            End If
            .Value = Replace(s, sCust & i, sCust & iNew)
            .EntireColumn.AutoFit
        End With
    End Sub
    
    Public Sub Increment_Division(Optional sScratch As String)
        Dim sh As Worksheet
        Dim s As String
        Dim i As Integer
        Dim iNew As Integer
        Dim sDiv As String
        Dim iInStr As Integer
    
        Set sh = ActiveSheet
        sDiv = " Division "
    
        With sh.Range("A1")
            s = .Value
            iInStr = InStr(s, sDiv)
            If iInStr = 0 Then
                s = "Customer 1, Division 1"
                iInStr = InStr(s, sDiv)
            End If
    
            i = Val(Mid(s, iInStr + Len(sDiv)))
            If i = .Range("A3").Value Then
                iNew = 1
            Else
                iNew = i + 1
            End If
            .Value = Replace(s, sDiv & i, sDiv & iNew)
            .EntireColumn.AutoFit
        End With
    End Sub
    
    Sub Create_Buttons()
        Dim dL As Double
        Dim dT As Double
        Dim dW As Double
        Dim dH As Double
        Dim oButton As Object
        Dim r As Range
        Dim iButton As Integer
        Dim sCaption As String
        Dim sh As Worksheet
        
        Set sh = ActiveSheet
        Set r = Selection
        sCaption = "Increment_Customer"
        For iButton = 1 To 2
            With r.Resize(2, 1)
                dL = .Left
                dT = .Top
                dW = .Width
                dH = .Height
            End With
            Set oButton = ActiveSheet.Buttons.Add(dL, dT, dW, dH)
            With oButton
                .Placement = xlMoveAndSize
                .OnAction = sh.CodeName & "." & sCaption
                .Caption = Replace(sCaption, "_", " ")
            End With
            sCaption = "Increment_Division"
            Set r = r.Offset(3, 0)
        Next iButton
    End Sub
    Last edited by foxguy; 07-24-2010 at 11:51 AM.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0