Consulting

Results 1 to 6 of 6

Thread: Solved: Help with looping code

  1. #1
    VBAX Regular
    Joined
    Mar 2006
    Posts
    11
    Location

    Solved: Help with looping code

    I don't even know if "looping code" is what you call it but I am at a loss as to how to do this without copying and pasting my code 25 times. I have a little section of code that when you click in a cell it checks to see if certain conditions exist and then creates a formula. Below is the code.

     
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo Finished
    If Target.Address = "$I$17" Then
        If Range("$H$17") = "Personal Vehicle" And Range("$I$17") = "" Then
     
            Dim days As Integer
            days = InputBox(Prompt:="Enter the number days you used your " _ &
                    personal Vehicle for Company Use for this expense reporting period", _
                    Title:="Days used")
            ActiveCell.Formula = "=((M4-M3-M5) *.4) +(10*" & days & ")"
        End If
        Else
    End If
    Finished:
    End Sub
    What that does is if the value of H17 is Personal vehicle and I17 is empty find out how many days they used their company vehicle and then create a formula to calculate how much thay are to get paid for mileage for using their personal vehicle. This code works perfectly but only for the 17th row. I want it to do it for every row between 8 and 32 without having to copy and paste that code 24 more times.

    There are other sections of code in the form I am workin on that I have already created the code 25 times (fortunately only 2 lines of code each time LOL) and I figured it is high time I figure out how to do this properly.

    Any help would be greatly appreciated.

    Sean

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo Finished
    With Target
    If .Row >= 8 And .Row <= 32 And .Column = 9 Then
    If Range("H" & .Row) = "Personal Vehicle" And .Value = "" Then
    Dim days As Integer
    days = InputBox(Prompt:="Enter the number days you used your " _ &
    "personal Vehicle for Company Use for this expense reporting period", _
    Title:="Days used")
    ActiveCell.Formula = "=((M4-M3-M5) *.4) +(10*" & days & ")"
    End If
    End If
    End With
    Finished:
    End Sub
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Mar 2006
    Posts
    11
    Location
    Thank you ever so kindly!!!

    That just cut out tonnes of code that I had done because I didn't know how to do that.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Sean,
    Welcome to VBAX.
    Please note that when you're posting code, inserting line breaks keeps it all on the screen, making it easier to follow.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Mar 2006
    Posts
    11
    Location
    yeah I was meaning to do that as soon as I got it working. I actually stored it in a variable after I got the code working. I am a messy coder LOL Thanks for the tip!!

    Sean

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It's not a problen so much in the VBE code window, but for users with smaller resolutions, it can be a PIA here.
    Glad you found your solution though. I'll mark this solved manually as that function is not working properly just now. (Thread Tools is where you'll find it)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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