count shapes for list names automatically instead of manually

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
240
Office Version
  1. 2019
  2. 2010
hello

I have this macro works very well when I write specific names it will add oval shape for entire row contains items or values for each name and it will count the oval shape into column N based on column M for each name , but I have to write them manually to count them . so what I want justl little adjusting for the code . should show the names in column M automatically when run the macro . and should replace the data when show the names or count them every time run the macro.
VBA Code:
Public Sub AddRedOva3()
Dim Shp As Shape
Dim rg As Range, c As Range, fnd As Range, tRow As Long
Dim Value As Variant, t() As String
Application.ScreenUpdating = False
For Each Shp In ActiveSheet.Shapes
    If Shp.Name <> "Button 1" Then Shp.Delete
Next Shp
For Each c In Range("N2:N" & Cells(Rows.Count, "N").End(xlUp).Row)
    c = 0
Next c
 
Application.ScreenUpdating = True
  Value = InputBox("Enter Value:", "Input")
  t = Split(Value, ",")
  lastrow = Range("B" & Rows.Count).End(xlUp).Row
  Set rg = Sheets("sheet1").Range("B2:J" & lastrow)
For i = 0 To UBound(t)
    invalue = t(i)
    GoSub update_Counter
    For r = 1 To lastrow
        If rg(r, 1) <> "" And rg(r, 1) = invalue Then
            For col = 1 To 9
                Set c = rg(r, col)
                If c <> "" Then
                    Set Shp = ActiveSheet.Shapes.AddShape(msoShapeOval, c.Left, c.Top, c.Width, c.Height)
                    Shp.Fill.Transparency = 1
                    Shp.Line.ForeColor.RGB = RGB(255, 0, 0)
                    Shp.Line.Weight = 1
                    If tRow > 0 Then Cells(tRow, "N") = Cells(tRow, "N") + 1
                End If
            Next col
        End If
    Next r
  Next i
Exit Sub

update_Counter:
With Range("M:M")
    Set fnd = .Find(What:=invalue, LookIn:=xlValues, Lookat:=xlWhole)
    If Not fnd Is Nothing Then
        tRow = fnd.Row
    Else
    End If
End With
Return
End Sub

1.PNG
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
should show the names in column M automatically when run the macro
Is something like this what you are thinking?
VBA Code:
    Dim a As Variant, sTemp As String, i As Integer
    
    a = Range("M2:M" & Cells(Rows.Count, "M").End(xlUp).Row).Value
    For i = 1 To UBound(a)
        sTemp = sTemp & a(i, 1) & ","
    Next
    sTemp = Left(sTemp, Len(sTemp) - 1)
    Value = InputBox("Enter Value:", "Input", sTemp)

should replace the data when show the names or count them every time run the macro
I am not sure what you want here. What data should be replaced?
 
Upvote 0
where I put your suggestion ? may you update the whole code to show how works ,please?
 
Upvote 0
Sure.
VBA Code:
Public Sub AddRedOva3()
    Dim Shp As Shape
    Dim rg As Range, c As Range, fnd As Range, tRow As Long
    Dim Value As Variant, t() As String
    
    Application.ScreenUpdating = False
    For Each Shp In ActiveSheet.Shapes
        If Shp.Name <> "Button 1" Then Shp.Delete
    Next Shp
    For Each c In Range("N2:N" & Cells(Rows.Count, "N").End(xlUp).Row)
        c = 0
    Next c
    
    Application.ScreenUpdating = True
    
    Dim a As Variant, sTemp As String, i As Integer
    
    a = Range("M2:M" & Cells(Rows.Count, "M").End(xlUp).Row).Value
    For i = 1 To UBound(a)
        sTemp = sTemp & a(i, 1) & ","
    Next
    sTemp = Left(sTemp, Len(sTemp) - 1)
    If sTemp = "" Then Exit Sub
    
    Value = InputBox("Enter Value:", "Input", sTemp)
    
    t = Split(Value, ",")
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    Set rg = Sheets("sheet1").Range("B2:J" & LastRow)
    For i = 0 To UBound(t)
        invalue = t(i)
        GoSub update_Counter
        For r = 1 To LastRow
            If rg(r, 1) <> "" And rg(r, 1) = invalue Then
                For col = 1 To 9
                    Set c = rg(r, col)
                    If c <> "" Then
                        Set Shp = ActiveSheet.Shapes.AddShape(msoShapeOval, c.Left, c.Top, c.Width, c.Height)
                        Shp.Fill.Transparency = 1
                        Shp.Line.ForeColor.RGB = RGB(255, 0, 0)
                        Shp.Line.Weight = 1
                        If tRow > 0 Then Cells(tRow, "N") = Cells(tRow, "N") + 1
                    End If
                Next col
            End If
        Next r
    Next i
    Exit Sub

update_Counter:
    With Range("M:M")
        Set fnd = .Find(What:=invalue, LookIn:=xlValues, Lookat:=xlWhole)
        If Not fnd Is Nothing Then
            tRow = fnd.Row
        End If
    End With
    Return
End Sub
 
Upvote 0
thanks , unfortunately it doesn't work . what I want after write name or more into inputbox based on orginal code and when add oval shap should show the names which contains oval shape in column M and count in column N , but based on code I have to write the names manually .

short of words , should show names contains oval shape in column M without interfere from me .
 
Upvote 0
What is an example of the text you put in the InputBox?
 
Upvote 0
orginal data
COUNT.xlsm
ABCDEFGHIJKLMN
1DATENAMEINV NOORDER NOBRANDM-SIZPRRCASEDEBITCREDITNAMECOUNT
21/10/2019MUSSAINV-BU1000ARR-1FOOD1MS-100RPP-1PA12,000.00200.00MUSSA-
31/11/2019MUSSINV-BU1001ARR-2FOOD2MS-101RPP-2NO PA1,000.00MUSS-
41/12/2019MUSSA3INV-BU1002ARR-3FOOD3MS-102RPP-3NO PA20,000.00MUSSIA-
51/13/2019MUSSA4INV-BU1003ARR-4FOOD4MS-103RPP-4NO PA10,000.00MUSSA3-
61/14/2019MUSSA5INV-BU1004ARR-5FOOD5MS-104RPP-5NO PA30,000.00MUSSA4-
71/15/2019MUSSA6INV-BU1005ARR-6FOOD6MS-105RPP-6NO PA20,000.00MUSSA5-
81/16/2019MUSSA7INV-BU1006ARR-7FOOD7MS-106RPP-7NO PA70,000.00MUSSA6-
91/17/2019MUSSA8INV-BU1007ARR-8FOOD8MS-107RPP-8NO PA12,000.00MUSSA7-
101/18/2019MUSSA9INV-BU1008ARR-9FOOD9MS-108RPP-9NO PA1,000.00MUSSA8-
111/20/2019MUSSA10INV-BU1010ARR-11FOOD11MS-110RPP-1NO PA3,000.00MUSSA9-
121/21/2019MUSSA11INV-BU1011ARR-12FOOD12MS-111RPP-2NO PA4,000.00MUSSA10-
131/22/2019MUSSIAINV-BU1012ARR-13FOOD13MS-112RPP-3PA20,000.00100.00MUSSA11-
141/23/2019MUSSA13INV-BU1013ARR-14FOOD14MS-113RPP-4NO PA90,000.00MUSSA13-
151/24/2019MUSSA14INV-BU1014ARR-15FOOD15MS-114RPP-5NO PA100,000.00MUSSA14-
161/25/2019MUSSAINV-BU1015ARR-16FOOD16MS-103RPP-6NO PA4,000.00
171/26/2019MUSSINV-BU1016ARR-17FOOD17MS-116RPP-17NO PA6,000.00
181/27/2019MUSSA10INV-BU1000ARR-1FOOD1MS-100RPP-18NO PA3,000.00
sheet1


fill names into inputbox
1.PNG


the result
1.PNG





new requirements .see the column M

before run the code

COUNT.xlsm
BCDEFGHIJKLMN
1NAMEINV NOORDER NOBRANDM-SIZPRRCASEDEBITCREDITNAMECOUNT
2MUSSAINV-BU1000ARR-1FOOD1MS-100RPP-1PA12,000.00200.00-
3MUSSINV-BU1001ARR-2FOOD2MS-101RPP-2NO PA1,000.00
4MUSSA3INV-BU1002ARR-3FOOD3MS-102RPP-3NO PA20,000.00
5MUSSA4INV-BU1003ARR-4FOOD4MS-103RPP-4NO PA10,000.00
6MUSSA5INV-BU1004ARR-5FOOD5MS-104RPP-5NO PA30,000.00
7MUSSA6INV-BU1005ARR-6FOOD6MS-105RPP-6NO PA20,000.00
8MUSSA7INV-BU1006ARR-7FOOD7MS-106RPP-7NO PA70,000.00
9MUSSA8INV-BU1007ARR-8FOOD8MS-107RPP-8NO PA12,000.00
10MUSSA9INV-BU1008ARR-9FOOD9MS-108RPP-9NO PA1,000.00
11MUSSA10INV-BU1010ARR-11FOOD11MS-110RPP-1NO PA3,000.00
12MUSSA11INV-BU1011ARR-12FOOD12MS-111RPP-2NO PA4,000.00
13MUSSIAINV-BU1012ARR-13FOOD13MS-112RPP-3PA20,000.00100.00
14MUSSA13INV-BU1013ARR-14FOOD14MS-113RPP-4NO PA90,000.00
15MUSSA14INV-BU1014ARR-15FOOD15MS-114RPP-5NO PA100,000.00
16MUSSAINV-BU1015ARR-16FOOD16MS-103RPP-6NO PA4,000.00
sheet1

after
1643994866425.png

hope this help to understand me
 
Upvote 0
Thank you. This explanation was very clear. I think this will be what you need.
VBA Code:
Public Sub AddRedOva3()
    Dim Shp As Shape
    Dim rg As Range, c As Range, fnd As Range
    Dim tRow As Long, lastrow As Long
    Dim i As Integer, r As Integer, col As Integer
    Dim Value As Variant, t() As String, invalue As String
   
    Application.ScreenUpdating = False
    For Each Shp In ActiveSheet.Shapes
        If Shp.Name <> "Button 1" Then Shp.Delete
    Next Shp
    Range("M1").CurrentRegion.Offset(1, 0).ClearContents
   
    Value = InputBox("Enter Value:", "Input")
    If Value = "" Then Exit Sub
    t = Split(Value, ",")
    lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Set rg = Sheets("sheet1").Range("B2:J" & lastrow)
    For i = 0 To UBound(t)
        invalue = Trim(t(i))
        tRow = Range("M" & Rows.Count).End(xlUp).Row + 1
        Cells(tRow, 13).Value = t(i)
       
        For r = 1 To lastrow
            If rg(r, 1) <> "" And rg(r, 1) = invalue Then
                For col = 1 To 9
                    Set c = rg(r, col)
                    If c <> "" Then
                        Set Shp = ActiveSheet.Shapes.AddShape(msoShapeOval, c.Left, c.Top, c.Width, c.Height)
                        Shp.Fill.Transparency = 1
                        Shp.Line.ForeColor.RGB = RGB(255, 0, 0)
                        Shp.Line.Weight = 1
                        If tRow > 0 Then Cells(tRow, "N") = Cells(tRow, "N") + 1
                    End If
                Next col
            End If
        Next r
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top