-1

I need to write a macro that searches a specified column and counts all the cells that contain a specified string, such as character "p" and character "q" then associate this in another column i.e the total column, indicating the character which has occurred maximum number of times in the corresponding row
enter image description here Have attached a sample screen shot of the same. Does anyone have any ideas?

Thank you in advance.

Community
  • 1
  • 1
pratheek
  • 41
  • 6
  • 1
    Read [ask] to understand why your question is about to be closed. – Scott Holtzman Jan 16 '18 at 15:41
  • What happens if they appear the same number of times maximum? – QHarr Jan 16 '18 at 15:43
  • You also appear to be doing a COUNTIF of character across a range contained within a row. Is there a reason why you can't solve this with formulas alone? I appreciate you may have simplified your example. – QHarr Jan 16 '18 at 15:46
  • I have around 400 rows which has to do the same operation. its like maximum occurrence of a character as an output. that is all i need. – pratheek Jan 16 '18 at 15:57

2 Answers2

0

You don't need a macro. The below formula will give you what you need. The range being counted appears in the formula 3 times. You would need to adjust this for the range you want to check

=INDEX(A1:C1,MODE(MATCH(A1:C1,A1:C1,0)))

Note: this will return an error if no single character appears more times than any other. In this case you could wrap the above formula in an IFERROR function to return whatever value you would want to see when this happens.

If you have any blank cells in the row, you can use the following array formula, which adds an IF statement to test for empty cells:

=INDEX(A1:C1,MODE(IF(A1:C1<>"",MATCH(A1:C1,A1:C1,0))))

When entering this formula you will need to press Ctrl + Shift + Enter

Leroy
  • 634
  • 1
  • 8
  • 33
  • Thank u for the reply but i need to do the same thing for around 400-500 rows so i was preferring a macro code to do this task. – pratheek Jan 16 '18 at 15:59
  • You can always copy the formula down. It will only take a few seconds – Leroy Jan 16 '18 at 16:00
  • thank u so much .. that worked. – pratheek Jan 16 '18 at 16:08
  • but i think i ll need to replace the row numbers every time rit ?? – pratheek Jan 16 '18 at 16:22
  • If you place your mouse cursor at the lower right corner of the cell which contains the formula, you can then drag this down as many rows as you like. The range will automatically update for each row – Leroy Jan 16 '18 at 16:28
  • I am so sorry Mr. Leroy, the BOB and WOW column contains number which has to be ignored, the column name with "symbol" has to compared i.e pair1 symbol column,pair2 symbol column, pair3 symbol column and so on has to be compared. for that what has to be done ? Have updated the image kindly check. – pratheek Jan 16 '18 at 17:16
  • Hmm, I've had a think and I'm not sure of the best way to find the most common value in a non-contiguous range using the existing Excel functions. There may be a way but I think it might be easier in that case to create a user defined function. It's a completely different approach so I'll post it as a separate answer – Leroy Jan 16 '18 at 22:53
0

Based on your additional criteria of having to exclude certain columns in the row I think it may indeed be easier to use VBA and create a user defined function that you can then enter into the cells in your spreadsheet in the same way as any other function.

I've shown my attempt below which basically checks the column of each cell in the range to ensure it has a header of "Symbol" and if so adds the value of that cell to an Array (after being converted to a number value). There is then another function that gets the mode from that array (this only works on numeric values which is why it was converted in the previous step). Finally that is converted back to a letter.

It's quite a roundabout way and there may be an easier approach but hopefully this will work for now and give you some idea's of how to create these kind of functions for yourself.

Create a new module in your VBA project and copy all 4 of the below procedures into it:

Option Explicit

Public Function MostFrequentValue(RNG As Range) As String
Dim HeaderRow As Integer
Dim a As Range
Dim arr As Variant

HeaderRow = 1 'Change this to whatever row your headers are in

For Each a In RNG.Cells
    If Cells(HeaderRow, a.Column) = "Symbol" Then
        If IsEmpty(arr) Then
            arr = Array(ConvertLetterToNumber(a.Value))
        Else
            ReDim Preserve arr(UBound(arr) + 1)
            arr(UBound(arr)) = ConvertLetterToNumber(a.Value)
        End If
    End If
Next

MostFrequentValue = ConvertNumberToLetter(ArrayMode(arr))

End Function

.

Function ConvertNumberToLetter(ByVal strSource As Integer) As String
ConvertNumberToLetter = LCase(Chr(strSource + 64))
End Function

.

Function ConvertLetterToNumber(ByVal strSource As String) As Integer
Dim i As Integer
Dim strResult As String

strSource = UCase(strSource)
For i = 1 To Len(strSource)
    Select Case Asc(Mid(strSource, i, 1))
        Case 65 To 90:
            strResult = strResult & Asc(Mid(strSource, i, 1)) - 64
        Case Else
            strResult = strResult & Mid(strSource, i, 1)
    End Select
Next
ConvertLetterToNumber = strResult

End Function

.

Function ArrayMode(Ray As Variant) As Integer

With Application
  ArrayMode = .Mode(Ray)
End With
End Function

You would then enter the function into a cell like so =MostFrequentValue("A2:C2")

P.S. This assumes that the symbol in each value in the Symbol column is a lowercase letter of the alphabet (a-z). This appears to be the case from your example

Leroy
  • 634
  • 1
  • 8
  • 33
  • I tried out the macro code and the =MostFrequentValue("A2:C2") function but i get the "#VALUE!" in the cell where I have entered the =MostFrequentValue("A2:C2") function. – pratheek Jan 17 '18 at 03:23
  • Did you amend the HeaderRow variable in the MostFrequentValue function? Is there 1 character that appears more times than any other? Are all of the symbols single lowercase letters? Did you paste the code into a new module (not a sheet module)? Learn how to place breakpoints and step through your code (search how to do this on google). It will be better for you to learn to do this for yourself so you can find which part of the code isn't working for you. – Leroy Jan 17 '18 at 08:37
  • yes i did amend the HeaderRow variable and yea it might be any combination of the "p","q","e".. yes i did paste the code in new vba module. – pratheek Jan 17 '18 at 08:54
  • 1
    Would be better to use the `Col_Letter` function in this post [Function to convert column number to letter](https://stackoverflow.com/questions/12796973/function-to-convert-column-number-to-letter) as it will convert numbers over 26. Also any variable holding a row number should be a `long` to allow for the higher row numbers. – Darren Bartrup-Cook Jan 17 '18 at 08:59
  • 1
    And this post for converting column letters to numbers [Excel column number from column name](https://stackoverflow.com/questions/10106465/excel-column-number-from-column-name) – Darren Bartrup-Cook Jan 17 '18 at 09:03
  • Yes those function would appear to do the job more efficiently than mine. I just wrote them very quickly last night. Technically the letters being converted don't refer to columnns but the numbers that they would return would be the same so that would work. The examples I've provided should still work as long as the values provided are only ever "p","q" or "e" as described by the OP – Leroy Jan 17 '18 at 11:22
  • Pratheek, due to the various conditions you have stipulated the solution I have given does have a bit of a "workaround" feel to it and is a bit abstract so any slight deviation from the exact case you have given in the example may cause it to fall over. It could be something like trailing spaces or non-capitilization of the word "Symbol" or if there were 2 p's and 2 q's (no one symbol more than any other) this would cause an error. I'm afraid I can't spend much more time on this but I would encourage you to step through the code as I mentioned earlier to see where it is not working correctly – Leroy Jan 17 '18 at 11:30
  • Okay.. thank u so much... – pratheek Jan 18 '18 at 10:01