+ Reply to Thread
Results 1 to 7 of 7

Macro partially working

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Macro partially working

    Hello,

    In the attached workbook, I have two macros. The first macro (More than), should highlight cells under the condition that the sum of a range in column B is greater than the average in range column C. The bounds of the range is determined by unique items found in column A.
    It should do this for each unique item in column A (cells in bold should highlight. For some reason, only A9:A15 are highlighting.

    In my second macro (Less Than), the opposite condition happens. For this macro, it should highlight cells A21:A22, but it is also highlighting A9.

    Any thoughts?
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Macro partially working

    Hi phbryan,

    Let me know how this goes:

    Option Explicit
    Sub Macro1()
    
        Call MyHighlight(True) 'True to highlight where SUM > AVG, or False to highlight where SUM < AVG
    
    End Sub
    Sub MyHighlight(blnSumIsGreaterThanAvg As Boolean)
    
        Dim objMyArray As Object
        Dim lngLastRow As Long, lngMyRow As Long
        Dim ws As Worksheet
        Dim varItem As Variant
        Dim dblSUMValue As Double, dblAVGValue As Double
        
        Set ws = ThisWorkbook.Sheets("Sheet2") 'Sheet name containing data. Change to suit if necessary.
        Set objMyArray = CreateObject("System.Collections.ArrayList")
        
        If WorksheetFunction.CountA(ws.Cells) = 0 Then
            MsgBox "There is no data in """ & ws.Name & """ to work with.", vbExclamation
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        ws.Range("A2:A" & lngLastRow).Interior.Color = xlNone 'Clear any previous highlighting
        
        'Create an unique array of items in Col. A
        For lngMyRow = 2 To lngLastRow
            If objMyArray.Contains(CStr(Trim(ws.Range("A" & lngMyRow)))) = False Then
                objMyArray.Add CStr(Trim(ws.Range("A" & lngMyRow)))
            End If
        Next lngMyRow
        
        For Each varItem In objMyArray
            dblSUMValue = Application.WorksheetFunction.SumIf(ws.Range("A2:A" & lngLastRow), varItem, ws.Range("B2:B" & lngLastRow))
            dblAVGValue = Application.WorksheetFunction.AverageIf(ws.Range("A2:A" & lngLastRow), varItem, ws.Range("C2:C" & lngLastRow))
            For lngMyRow = 2 To lngLastRow
                If Trim(ws.Range("A" & lngMyRow)) = varItem Then
                    If blnSumIsGreaterThanAvg = True Then
                        If dblSUMValue > dblAVGValue Then
                            ws.Range("A" & lngMyRow).Interior.Color = RGB(255, 255, 0) 'Yellow if SUM > AVG
                        End If
                    Else
                        If dblSUMValue < dblAVGValue Then
                            ws.Range("A" & lngMyRow).Interior.Color = RGB(0, 255, 0) 'Green if SUM < AVG
                        End If
                    End If
                End If
            Next lngMyRow
        Next varItem
        
        Application.ScreenUpdating = True
        
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Macro partially working

    Hey Robert,

    I get a Runtime Error at this line:
    Set objMyArray = CreateObject("System.Collections.ArrayList")
    Last edited by 6StringJazzer; 06-26-2021 at 11:05 AM. Reason: Please do not quote the entire post to respond to it

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: Macro partially working

    The .NET Framework version 3.5 is required for this solution. There may be a different version on your computer, but 3.5 is required. Additionally, install version 3.5 and enjoy the possibility of using ArrayList.

    Artik

  5. #5
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Re: Macro partially working

    Thank you Artik and trebor. The macro now runs.
    The macro should have highlighted A2:A6 and A9:A13, but it did not. See this most updated workbook.
    Attached Files Attached Files
    Last edited by phbryan; 06-26-2021 at 08:00 AM.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Macro partially working

    Here's another (virtually same) way but uses collections:

    Option Explicit
    Sub Macro1()
    
        Call MyHighlight(False) 'True to highlight where SUM > AVG, or False to highlight where SUM < AVG
    
    End Sub
    Sub MyHighlight(blnSumIsGreaterThanAvg As Boolean)
    
        Dim varMyArray As New Collection
        Dim lngLastRow As Long, lngMyRow As Long
        Dim ws As Worksheet
        Dim varItem As Variant
        Dim dblSUMValue As Double, dblAVGValue As Double
        
        Set ws = ThisWorkbook.Sheets("Sheet2") 'Sheet name containing data. Change to suit if necessary.
        If WorksheetFunction.CountA(ws.Cells) = 0 Then
            MsgBox "There is no data in """ & ws.Name & """ to work with.", vbExclamation
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        ws.Range("A2:A" & lngLastRow).Interior.Color = xlNone 'Clear any previous highlighting
        
        'Create an unique array of items in Col. A
        For lngMyRow = 2 To lngLastRow
            On Error Resume Next
                varMyArray.Add CStr(Trim(ws.Range("A" & lngMyRow)))
            On Error GoTo 0
        Next lngMyRow
        
        For Each varItem In varMyArray
            dblSUMValue = Application.WorksheetFunction.SumIf(ws.Range("A2:A" & lngLastRow), varItem, ws.Range("B2:B" & lngLastRow))
            dblAVGValue = Application.WorksheetFunction.AverageIf(ws.Range("A2:A" & lngLastRow), varItem, ws.Range("C2:C" & lngLastRow))
            For lngMyRow = 2 To lngLastRow
                If Trim(ws.Range("A" & lngMyRow)) = varItem Then
                    If blnSumIsGreaterThanAvg = True Then
                        If dblSUMValue > dblAVGValue Then
                            ws.Range("A" & lngMyRow).Interior.Color = RGB(255, 255, 0) 'Yellow if SUM > AVG
                        End If
                    Else
                        If dblSUMValue < dblAVGValue Then
                            ws.Range("A" & lngMyRow).Interior.Color = RGB(0, 255, 0) 'Green if SUM < AVG
                        End If
                    End If
                End If
            Next lngMyRow
        Next varItem
        
        Application.ScreenUpdating = True
        
    End Sub

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Macro partially working

    Try this:

    Option Explicit
    Sub SUM_More_Than_Avg()
    
        Call MyHighlight(True, "Sheet2") 'True to highlight where SUM > AVG, or False to highlight where SUM < AVG
        
    End Sub
    Sub SUM_Less_Than_Avg()
    
        Call MyHighlight(False, "Sheet2") 'True to highlight where SUM > AVG, or False to highlight where SUM < AVG
        
    End Sub
    Sub MyHighlight(blnSumIsGreaterThanAvg As Boolean, strSheetName As String)
    
        Dim varMyArray As New Collection
        Dim lngLastRow As Long, lngMyRow As Long
        Dim ws As Worksheet
        Dim varItem As Variant
        Dim dblSUMValue As Double, dblAVGValue As Double
        
        Set ws = ThisWorkbook.Sheets(strSheetName)
        If WorksheetFunction.CountA(ws.Cells) = 0 Then
            MsgBox "There is no data in """ & ws.Name & """ to work with.", vbExclamation
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        ws.Range("A2:A" & lngLastRow).Interior.Color = xlNone 'Clear any previous highlighting
        
        'Create an unique array of items in Col. A
        For lngMyRow = 2 To lngLastRow
            On Error Resume Next
                varMyArray.Add CStr(Trim(ws.Range("A" & lngMyRow)))
            On Error GoTo 0
        Next lngMyRow
        
        For Each varItem In varMyArray
            dblAVGValue = Application.WorksheetFunction.AverageIf(ws.Range("A2:A" & lngLastRow), varItem, ws.Range("C2:C" & lngLastRow))
            For lngMyRow = 2 To lngLastRow
                If Trim(ws.Range("A" & lngMyRow)) = varItem Then
                    If blnSumIsGreaterThanAvg = True Then
                        If Application.WorksheetFunction.SumIf(ws.Range("A2:A" & lngLastRow), varItem, ws.Range("B2:B" & lngLastRow)) < dblAVGValue Then
                            Exit For
                        Else
                            dblSUMValue = IIf(dblSUMValue = 0, ws.Range("B" & lngMyRow), dblSUMValue + ws.Range("B" & lngMyRow))
                            ws.Range("A" & lngMyRow).Interior.Color = RGB(255, 255, 0) 'Yellow if SUM > AVG. Change to suit if necessary.
                            If dblSUMValue > dblAVGValue Then
                                Exit For
                            End If
                        End If
                    ElseIf blnSumIsGreaterThanAvg = False Then
                        If Application.WorksheetFunction.SumIf(ws.Range("A2:A" & lngLastRow), varItem, ws.Range("B2:B" & lngLastRow)) > dblAVGValue Then
                            Exit For
                        Else
                            dblSUMValue = IIf(dblSUMValue = 0, ws.Range("B" & lngMyRow), dblSUMValue + ws.Range("B" & lngMyRow))
                            If dblSUMValue < dblAVGValue Then
                                ws.Range("A" & lngMyRow).Interior.Color = RGB(0, 255, 0) 'Green if SUM < AVG. Change to suit if necessary.
                            Else
                                Exit For
                            End If
                        End If
                    End If
                End If
            Next lngMyRow
            dblSUMValue = 0
        Next varItem
        
        Application.ScreenUpdating = True
        
    End Sub
    Last edited by Trebor76; 06-26-2021 at 08:05 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] ISERROR partially working
    By jimbokeep in forum Excel General
    Replies: 5
    Last Post: 11-18-2016, 12:03 PM
  2. save as macro partially working
    By tigergutt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-05-2016, 07:12 AM
  3. [SOLVED] Calculation partially not working
    By WilliamWelch in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-22-2015, 05:27 PM
  4. [SOLVED] Help with partially working Macro to separate groups
    By kleptilian in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-30-2015, 02:14 PM
  5. [SOLVED] Code only partially working
    By Saeber4777 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-11-2014, 10:53 PM
  6. [SOLVED] PC Macro only partially working on Macs
    By sdnorton in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-20-2014, 01:38 PM
  7. Macro only partially working
    By LilSisKin in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-26-2013, 06:04 PM

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.6.0 RC 1