Counting and Then Notating a Color Pattern Within a Row

AustinMaly

New Member
Joined
Oct 6, 2022
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
After much digging, I don't know if what I'm looking for is possible or not. Its a reach for sure.

I am working on a knitting project, and it would be really helpful if I could get Excel to count the patterns in each for me. So far I have cells being conditionally formatted whether they are blank or if they contain an "X".

What i would like to know is: Can Excel count the Pattern and then notate the number of cells that have that specific color? Or is there an easier way to Flash Fill the pattern?

This is what i have as a base for my pattern:

Knit1.jpg


Here are a couple options of what I'm HOPING achieve. I have tried to Flash Fill / Auto-Fill many of my rows as depicted for "Possible Option 3", but there are so many rows, and the patterns get kind of crazy, that its going to take me hours just to do this.

Knit2.jpg


I hope this makes sense!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,
I'm sure this is possible .. but for ease of pain would you be willing to share your excel? feel free to simplify it or remove the conditional formatting.
 
Upvote 0
Hi,
I'm sure this is possible .. but for ease of pain would you be willing to share your excel? feel free to simplify it or remove the conditional formatting.
Would sharing it via Google Sheets work okay? If not, I'll figures something out.
 
Upvote 0
should be fairly easy
 
Upvote 0
Upvote 0
Would sharing it via Google Sheets work okay?
The best way is to use XL2BB as I have done below. That way you do not eliminate possible helpers who choose not to download files or are prevented from doing so by workplace restrictions and your data (& expected results) are immediately visible directly in your post, similar to posting images like you did but with the advantage we can copy from it.

I did not really understand your "possible options" image as the results did not seem to correlate with the data. However, would you consider a vba approach?
This is my interpretation of your request.

VBA Code:
Sub CountBlocks()
  Dim rRow As Range, rA As Range, rConst As Range
  Dim lr As Long, i As Long, FirstResultCol As Long
  
  Const FirstRow As Long = 3              '<- Adjust to suit
  Const ColumnsToCheck As String = "A:T"  '<- Adjust to suit
  
  lr = Range(ColumnsToCheck).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  FirstResultCol = Range(ColumnsToCheck).Column + Range(ColumnsToCheck).Columns.Count + 1
  Application.ScreenUpdating = False
  For Each rRow In Range(Replace(ColumnsToCheck, ":", FirstRow & ":") & lr).Rows
    i = 0
    Set rConst = Nothing
    On Error Resume Next
    Set rConst = rRow.SpecialCells(xlConstants)
    On Error GoTo 0
    If Not rConst Is Nothing Then
      For Each rA In rConst.Areas
        Cells(rRow.Row, FirstResultCol + i).Value = rA.Count
        i = i + 1
      Next rA
    End If
  Next rRow
  Application.ScreenUpdating = True
End Sub

My original sample data is in columns A:T and the code has produced the results shown in columns V onwards.

AustinMaly.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1
2
3xxxxxxxxxxxxxxx7332
4
5xxxxxxxxx333
6xxxxxxxxxx1111111111
7
Patterns
 
Upvote 0
I did not really understand your "possible options" image as the results did not seem to correlate with the data. However, would you consider a vba approach?
This is my interpretation of your request.
I think that this is really close. I will have to work on using XLBB2 to upload a snippet. I think the option

Thank you for your reply. I will work on installing the XL2BB.

I did run this VBA and it is actually pretty close to what I'm looking for! Thank you so much!

I think the interpretation didn't make sense, because I was also hoping that it would count out the blank cell also.
 
Last edited by a moderator:
Upvote 0
... I was also hoping that it would count out the blank cell also.
Ah, now I understand those options better. :)

See if this version is closer.

VBA Code:
Sub CountBlocks_v2()
  Dim rRow As Range, rA As Range, rConst As Range, rBlanks As Range
  Dim lr As Long, i As Long, FirstResultCol As Long
  Dim bFirstIsBlank As Boolean
  
  Const FirstRow As Long = 3              '<- Adjust to suit
  Const ColumnsToCheck As String = "A:T"  '<- Adjust to suit
  
  lr = Range(ColumnsToCheck).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  FirstResultCol = Range(ColumnsToCheck).Column + Range(ColumnsToCheck).Columns.Count + 1
  Application.ScreenUpdating = False
  For Each rRow In Range(Replace(ColumnsToCheck, ":", FirstRow & ":") & lr).Rows
    bFirstIsBlank = IsEmpty(rRow.Cells(1).Value)
    Set rConst = Nothing
    Set rBlanks = Nothing
    On Error Resume Next
    Set rConst = rRow.SpecialCells(xlConstants)
    Set rBlanks = rRow.SpecialCells(xlBlanks)
    On Error GoTo 0
    If Not rConst Is Nothing Then
      i = 0
      For Each rA In rConst.Areas
        Cells(rRow.Row, FirstResultCol + i - bFirstIsBlank).Value = rA.Count
        i = i + 2
      Next rA
    End If
    If Not rBlanks Is Nothing Then
      i = 1
      For Each rA In rBlanks.Areas
        Cells(rRow.Row, FirstResultCol + i + bFirstIsBlank).Value = rA.Count
        i = i + 2
      Next rA
    End If
  Next rRow
  Application.ScreenUpdating = True
End Sub

Sample data and results

AustinMaly.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAP
1
2
3xxxxxxxxxxxxxxx7331312
420
5xxxxxxxxx3343133
6xxxxxxxxxx11111111111111111111
7xxxxxxxxxxxxxxxxxxxx20
Patterns
 
Upvote 0
Solution
This worked! You are a gosh darn genius! Thank you so much! VBA has been my weakness!
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
Members
453,021
Latest member
Justyna P

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