every 10th selection of same item from combo box to be highlighted.

KingGoku

New Member
Joined
Jul 12, 2023
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I need help writing a macro that will highlight every 10th selection of same item from userform combo box. is that possible?

VBA Code:
Private Sub ComboBoxProduct_change()

Dim m As String

'Convert date into Julian days
Dim awb As WorksheetFunction, JulianDt As String
Dim ws As Worksheet
Dim DateCell, ProdCell As Range
Dim DayCount, RowCell As Long
Set awb = Application.WorksheetFunction

JulianDt = awb.Text(awb.Days(Date, 1) - awb.Days(DateSerial(Year(Date), 1, 0), 1), "000")

m = FormSelectSht.ComboBoxProduct.Value

For Each ws In ThisWorkbook.Worksheets
    If ws.Name = ComboBoxMachines.Value Then Exit For
Next ws

DayCount = 1

For RowCell = 5 To 200
    If ws.Cells(RowCell, 1).Value = Date And ws.Cells(RowCell, 2) = ComboBoxProduct.Value Then DayCount = DayCount + 1
Next RowCell


FormSelectSht.TextBoxLot.Value = JulianDt + "-" + Format(Date, "yyyy") + "-" + m + "-" + Format(DayCount, "000")


End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Check out this modify code



Private Sub ComboBoxProduct_Change()

Dim m As String
Dim awb As WorksheetFunction
Dim JulianDt As String
Dim ws As Worksheet
Dim DateCell, ProdCell As Range
Dim DayCount, RowCell As Long
Dim SelectionCount As Long

Set awb = Application.WorksheetFunction

JulianDt = awb.Text(awb.Days(Date, 1) - awb.Days(DateSerial(Year(Date), 1, 0), 1), "000")

m = FormSelectSht.ComboBoxProduct.Value

For Each ws In ThisWorkbook.Worksheets
If ws.Name = ComboBoxMachines.Value Then Exit For
Next ws

DayCount = 1
SelectionCount = 1

For RowCell = 5 To 200
If ws.Cells(RowCell, 1).Value = Date And ws.Cells(RowCell, 2) = ComboBoxProduct.Value Then
DayCount = DayCount + 1
' Check if it's the 10th selection of the same item
If SelectionCount Mod 10 = 0 Then
ws.Cells(RowCell, 1).Interior.Color = RGB(255, 255, 0) ' Highlight in yellow
End If
SelectionCount = SelectionCount + 1
End If
Next RowCell

FormSelectSht.TextBoxLot.Value = JulianDt & "-" & Format(Date, "yyyy") & "-" & m & "-" & Format(DayCount, "000")

End Sub
 
Upvote 0
Check out this modify code



Private Sub ComboBoxProduct_Change()

Dim m As String
Dim awb As WorksheetFunction
Dim JulianDt As String
Dim ws As Worksheet
Dim DateCell, ProdCell As Range
Dim DayCount, RowCell As Long
Dim SelectionCount As Long

Set awb = Application.WorksheetFunction

JulianDt = awb.Text(awb.Days(Date, 1) - awb.Days(DateSerial(Year(Date), 1, 0), 1), "000")

m = FormSelectSht.ComboBoxProduct.Value

For Each ws In ThisWorkbook.Worksheets
If ws.Name = ComboBoxMachines.Value Then Exit For
Next ws

DayCount = 1
SelectionCount = 1

For RowCell = 5 To 200
If ws.Cells(RowCell, 1).Value = Date And ws.Cells(RowCell, 2) = ComboBoxProduct.Value Then
DayCount = DayCount + 1
' Check if it's the 10th selection of the same item
If SelectionCount Mod 10 = 0 Then
ws.Cells(RowCell, 1).Interior.Color = RGB(255, 255, 0) ' Highlight in yellow
End If
SelectionCount = SelectionCount + 1
End If
Next RowCell

FormSelectSht.TextBoxLot.Value = JulianDt & "-" & Format(Date, "yyyy") & "-" & m & "-" & Format(DayCount, "000")

End Sub
hi muhammad,

thanks so much it works.
 
Upvote 0
Check out this modify code



Private Sub ComboBoxProduct_Change()

Dim m As String
Dim awb As WorksheetFunction
Dim JulianDt As String
Dim ws As Worksheet
Dim DateCell, ProdCell As Range
Dim DayCount, RowCell As Long
Dim SelectionCount As Long

Set awb = Application.WorksheetFunction

JulianDt = awb.Text(awb.Days(Date, 1) - awb.Days(DateSerial(Year(Date), 1, 0), 1), "000")

m = FormSelectSht.ComboBoxProduct.Value

For Each ws In ThisWorkbook.Worksheets
If ws.Name = ComboBoxMachines.Value Then Exit For
Next ws

DayCount = 1
SelectionCount = 1

For RowCell = 5 To 200
If ws.Cells(RowCell, 1).Value = Date And ws.Cells(RowCell, 2) = ComboBoxProduct.Value Then
DayCount = DayCount + 1
' Check if it's the 10th selection of the same item
If SelectionCount Mod 10 = 0 Then
ws.Cells(RowCell, 1).Interior.Color = RGB(255, 255, 0) ' Highlight in yellow
End If
SelectionCount = SelectionCount + 1
End If
Next RowCell

FormSelectSht.TextBoxLot.Value = JulianDt & "-" & Format(Date, "yyyy") & "-" & m & "-" & Format(DayCount, "000")

End Sub
how can i get the code to look at only first first 4 string code "0000")
 
Upvote 0
how can i get the code to look at only first first 4 string code "0000")
VBA Code:
Private Sub ComboBoxProduct_Change()

    Dim m As String
    Dim awb As WorksheetFunction
    Dim JulianDt As String
    Dim ws As Worksheet
    Dim DateCell, ProdCell As Range
    Dim DayCount, RowCell As Long
    Dim SelectionCount As Long
   
    Set awb = Application.WorksheetFunction
   
    JulianDt = awb.Text(awb.Days(Date, 1) - awb.Days(DateSerial(Year(Date), 1, 0), 1), "000")
   
    m = FormSelectSht.ComboBoxProduct.Value
   
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = ComboBoxMachines.Value Then Exit For
    Next ws
   
    DayCount = 1
    SelectionCount = 1
   
    For RowCell = 5 To 200
        If ws.Cells(RowCell, 1).Value = Date And ws.Cells(RowCell, 2) = ComboBoxProduct.Value Then
            DayCount = DayCount + 1
            ' Check if it's the 10th selection of the same item
            If SelectionCount Mod 10 = 0 Then
                ' Check if the first 4 characters of TextBoxLot.Value are "0000"
                If Left(FormSelectSht.TextBoxLot.Value, 4) = "0000" Then
                    ' You can add your code here to change the cell color if needed.
                End If
            End If
            SelectionCount = SelectionCount + 1
        End If
    Next RowCell
   
    FormSelectSht.TextBoxLot.Value = JulianDt & "-" & Format(Date, "yyyy") & "-" & m & "-" & Format(DayCount, "000")

End Sub
 
Last edited by a moderator:
Upvote 0
VBA Code:
Private Sub ComboBoxProduct_Change()

    Dim m As String
    Dim awb As WorksheetFunction
    Dim JulianDt As String
    Dim ws As Worksheet
    Dim DateCell, ProdCell As Range
    Dim DayCount, RowCell As Long
    Dim SelectionCount As Long
  
    Set awb = Application.WorksheetFunction
  
    JulianDt = awb.Text(awb.Days(Date, 1) - awb.Days(DateSerial(Year(Date), 1, 0), 1), "000")
  
    m = FormSelectSht.ComboBoxProduct.Value
  
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = ComboBoxMachines.Value Then Exit For
    Next ws
  
    DayCount = 1
    SelectionCount = 1
  
    For RowCell = 5 To 200
        If ws.Cells(RowCell, 1).Value = Date And ws.Cells(RowCell, 2) = ComboBoxProduct.Value Then
            DayCount = DayCount + 1
            ' Check if it's the 10th selection of the same item
            If SelectionCount Mod 10 = 0 Then
                ' Check if the first 4 characters of TextBoxLot.Value are "0000"
                If Left(FormSelectSht.TextBoxLot.Value, 4) = "0000" Then
                    ' You can add your code here to change the cell color if needed.
                End If
            End If
            SelectionCount = SelectionCount + 1
        End If
    Next RowCell
  
    FormSelectSht.TextBoxLot.Value = JulianDt & "-" & Format(Date, "yyyy") & "-" & m & "-" & Format(DayCount, "000")

End Sub
Hi Muhammad,
it didn't work. so what i want is if you have look at picture. i want code to look at first 4 words or number in product column and if it has appeared 10th time. i want row to highlight. the first 4 digit could vary. sometime it's a word sometime it's numbers.
 

Attachments

  • Screenshot 2023-10-04 073409.png
    Screenshot 2023-10-04 073409.png
    61.9 KB · Views: 5
Upvote 0

Similar threads

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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