Consulting

Results 1 to 12 of 12

Thread: Solved: VBA Message Box

  1. #1

    Solved: VBA Message Box

    I am making a workbook that loads staff data off of a website and displays it in Excel.
    I have a field called Length of service and want to automatically load a message box to tell the user if there is anyone who is coming up to a 5/10/15 etc years of service.
    the raw data is already in the workbook so it will just be a case of checking in a column for any of those years.
    Last edited by gregoryt2002; 04-03-2008 at 03:28 AM.

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    This should work
    [vba]Sub RangeLoop()


    Dim rCell As Range, MyRange As Range, CelVal As String
    Set MyRange = Worksheets("Sheet1").Range("A1:A1000")
    For Each rCell In MyRange.Cells

    If rCell.Value + 1839 = Date Then
    rCell.Select
    CelVal = rCell.Offset(0, 1).Value
    MsgBox CelVal & " has been here nearly 5 years"
    End If

    If rCell.Value + 3665 = Date Then
    rCell.Select
    CelVal = rCell.Offset(0, 1).Value
    MsgBox CelVal & " has been here nearly 10 years"
    End If

    If rCell.Value + 5489 = Date Then
    rCell.Select
    CelVal = rCell.Offset(0, 1).Value
    MsgBox CelVal & " has been here nearly 15 years"
    End If
    Next rCell

    End Sub[/vba]

    its a bit rough about a week and a halfs notice but you could tinker

    you could also put this in the workbook to call it when you open the workbook.
    [vba]Private Sub Workbook_Open()
    Call RangeLoop
    End Sub[/vba]

    Hope this helps

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This puts a comment in a corresponding cell, Change to a MsgBox if desired.
    [VBA]
    Option Explicit
    Sub RangeLoop()
    Dim rCell As Range, MyRange As Range
    Dim YrDate As Date
    Const NoticePeriod = 21 '<=== change to suit

    Set MyRange = Worksheets("Sheet1").Range("A1:A1000")
    For Each rCell In MyRange
    YrDate = CDate(Day(rCell) & "/" & Month(rCell) & "/" & Year(Date))
    If YrDate - Date > 0 And YrDate - Date < NoticePeriod Then
    Select Case Year(Date) - Year(rCell)
    Case 5
    rCell.Offset(, 3) = "5 years"
    Case 10
    rCell.Offset(, 3) = "10 years"
    Case 15
    rCell.Offset(, 3) = "15 years"
    End Select
    End If
    Next rCell
    End Sub

    [/VBA]
    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'

  4. #4
    Sub RangeLoop()
    Dim rCell As Range, MyRange As Range
    Dim YrDate As Date
    Const NoticePeriod = 7

    Set MyRange = Worksheets("Raw_Data").Range("L:L")
    For Each rCell In MyRange
    YrDate = CDate(Day(rCell) & "/" & Month(rCell) & "/" & Year(Date))
    If YrDate - Date > 0 And YrDate - Date < NoticePeriod Then
    Select Case Year(Date) - Year(rCell)
    Case 5
    rCell.Offset(, 3) = "5 years"
    Case 10
    rCell.Offset(, 3) = "10 years"
    Case 15
    rCell.Offset(, 3) = "15 years"
    End Select
    End If
    Next rCell
    End Sub

    I seem to get an error on the highlighted line. Im a complete VBA novice so im sure its probably something im doign.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post some sample data so I can check this out. It may be that your dates are in a text format. Use Manage Attachments in the Go Advanced section
    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'

  6. #6
    I've attached some sample data. Im sure im just being a spoon and the code works fine.
    Cheers for having a look

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The code failed where the cell contents were not valid dates. This should fix it. Note that setting the constant to 1 will show a result only on the anniversary date.
    [VBA]
    Sub RangeLoop()
    Dim rCell As Range, MyRange As Range
    Dim YrDate As Date
    Const NoticePeriod = 10
    Set MyRange = Worksheets("Raw_Data").Range("B:B")
    For Each rCell In MyRange
    If IsDate(rCell) Then
    YrDate = CDate(Day(rCell) & "/" & Month(rCell) & "/" & Year(Date))
    If YrDate - Date > 0 And YrDate - Date < NoticePeriod Then
    Select Case Year(Date) - Year(rCell)
    Case 5
    rCell.Offset(, 3) = "5 years"
    Case 10
    rCell.Offset(, 3) = "10 years"
    Case 15
    rCell.Offset(, 3) = "15 years"
    End Select
    End If
    End If
    Next rCell
    End Sub

    [/VBA]
    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'

  8. #8
    Is this code easy to change into a pop up message box?

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Yes.
    Check how it is done in Post 2
    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'

  10. #10
    Tried to follow the example in Post 2 but still get no message box. Any Ideas?

    [VBA]Sub RangeLoop()
    Dim rCell As Range, MyRange As Range
    Dim YrDate As Date
    Const NoticePeriod = 1
    Set MyRange = Worksheets("Raw_Data").Range("L:L")
    For Each rCell In MyRange
    If IsDate(rCell) Then
    YrDate = CDate(Day(rCell) & "/" & Month(rCell) & "/" & Year(Date))
    If YrDate - Date > 0 And YrDate - Date < NoticePeriod Then
    Select Case Year(Date) - Year(rCell)
    Case 5
    CelVal = rCell.Offset(, 3).Value
    MsgBox CelVal & "5 years"
    Case 10
    CelVal = rCell.Offset(, 3).Value
    MsgBox CelVal & "10 years"
    Case 15
    CelVal = rCell.Offset(, 3).Value
    MsgBox CelVal & "15 years"
    End Select
    End If
    End If
    Next rCell
    End Sub[/VBA]

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA] Select Case Year(Date) - Year(rCell)
    Case 5
    MsgBox "Celebrating 5 years"
    Case 10
    MsgBox "Celebrating 10 years"
    Case 15
    MsgBox "Celebrating 15 years"
    End Select
    [/VBA]
    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'

  12. #12
    Thanks

Posting Permissions

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