+ Reply to Thread
Results 1 to 3 of 3

macro to run on entire range, not just changed cells

Hybrid View

  1. #1
    Registered User
    Join Date
    05-13-2011
    Location
    Belfast, Northen Ireland
    MS-Off Ver
    Excel 2007
    Posts
    12

    macro to run on entire range, not just changed cells

    hi all

    i have a macro running grand whenver i change a cell to a value within the range specified in the code.

    what i want is for the macro to activate when the cell is open, scan every sheet for specified values, and call a 2nd macro for every corresponding value.

    here is the code as is (after some help here yesterday!)

    
    Option Explicit
    Private Sub Worksheet_change(ByVal Target As Range)
       Dim buf As String
       Dim cell As Range
    
       If Not Intersect(Range("E5:CG100"), Target) Is Nothing Then
          For Each cell In Intersect(Range("E5:CG100"), Target)
             ' only want to process the odd numbered columns,
             ' E being column 5, G column 7 and so on
             If cell.Column Mod 2 = 1 Then
                If cell.Value = Date + 60 Then
                   buf = buf & vbLf & cell.Address & " = " & cell.Value _
                         & " (training expires in 60 days - refresher required: " & Cells(4, cell.Column).Value _
                         & " / " & Cells(cell.Row, 1).Value & ")"
                ElseIf cell.Value = Date + 10 Then
                   buf = buf & vbLf & cell.Address & " = " & cell.Value _
                         & " (training expires in 10 days - refresher urgently required: " & Cells(4, cell.Column).Value _
                         & " / " & Cells(cell.Row, 1).Value & ")"
                ElseIf cell.Value = Date Then
                   buf = buf & vbLf & cell.Address & " = " & cell.Value _
                         & " (training expires today - refresher required immediately: " & Cells(4, cell.Column).Value _
                         & " / " & Cells(cell.Row, 1).Value & ")"
                ElseIf cell.Value < Date And cell.Value > 100 Then
                   buf = buf & vbLf & cell.Address & " = " & cell.Value _
                         & " (training has expired, refresher urgently required: " & Cells(4, cell.Column).Value _
                         & " / " & Cells(cell.Row, 1).Value & ")"
                ElseIf cell.Value = "" Then
                   buf = buf & vbLf & cell.Address & " = " & cell.Value _
                         & " (never been trained - training required: " & Cells(4, cell.Column).Value _
                         & " / " & Cells(cell.Row, 1).Value & ")"
                End If
             End If
          Next cell
          If buf <> "" Then Call Mail_small_Text_Outlook(buf)
       End If
    
    End Sub
     
     
    Sub Mail_small_Text_Outlook(sText As String)
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
     
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
     
        strbody = "Hi there" & vbNewLine & vbNewLine & "nightshift" & vbNewLine & vbNewLine & sText & ""
     
        On Error Resume Next
        With OutMail
            .To = "[email protected]"
            .CC = ""
            .BCC = ""
            .Subject = "training alert"
            .Body = strbody
            .Send
        End With
        On Error GoTo 0
     
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    how do i go about that?

    thanks
    Last edited by davemcguire; 05-24-2011 at 10:02 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: macro to run on entire range, not just changed cells

    instead of this worksheet event code use workbook event code

    open vb editor (alt F11)
    hit control R
    in the left project window all open files are listed
    goto your file and rightclick "thisworkbook" and click code

    there on the left hand window choose "workbook"
    on the right window choose "sheet selection change"

    then write your macro. you have to specify the sheet names where you want this code to run or not.

  3. #3
    Registered User
    Join Date
    05-13-2011
    Location
    Belfast, Northen Ireland
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: macro to run on entire range, not just changed cells

    thanks very much venkat, that sorted it.

+ 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