Count and output letters in order

bestnagi

New Member
Joined
Mar 12, 2013
Messages
14
What is the best way to have the table output the letters in certain order and certain number of time.

Table has input X,Y,Z . For each X,Y,Z, it shows how many times they are repeated in the output in that order X,Y,Z.

XYZ
211XXYZ
120XYY

<tbody>
</tbody>
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Will there always only be 1 set of each letter if it's there in each row?

In other words, would you ever have this: X X Y Z X
for example.
 
Upvote 0
Then a simple COUNTIF should do it:

In A2:
=COUNTIF($D2:$Z2,A$1)

Copy across and down.

Excel Workbook
ABCDEFG
1XYZ****
2211XXYZ
3120XYY*
Sheet1
 
Upvote 0
I'm fairly new to VBA and I'm sure someone else has a better solution.

Not sure exactly what you are asking, but if you merely want to count the number of occurrences of x,y and z in the whole table then the following works: just paste your data in cell A1 on the sheet. Instead of the message box you can put the results back in the sheet.

Private Sub CommandButton1_Click()
Dim x As Integer, y As Integer, z As Integer


x = 0
y = 0
z = 0


For Each cell In Range("A1:G3")
If cell = "x" Or cell = "X" Then
x = x + 1
ElseIf cell = "y" Or cell = "Y" Then
y = y + 1
ElseIf cell = "z" Or cell = "Z" Then
z = z + 1
Else
End If
Next cell
MsgBox ("x = " & x & vbNewLine & "y = " & y & vbNewLine & "z = " & z)



End Sub
 
Upvote 0
bestnagi,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


How about a macro solution?

The following macro will adjust for a varying number of rows, and, columns.

Sample raw data:


Excel 2007
ABCDEFG
1XYZ
2211
3120
4
Sheet1


After the macro:


Excel 2007
ABCDEFG
1XYZ
2211XXYZ
3120XYY
4
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub OutputLetters()
' hiker95, 10/30/2014, ME815157
Dim r As Long, lr As Long, c As Long, lc As Long, nc As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
For r = 2 To lr
  For c = 1 To lc
    If Cells(r, c) > 0 Then
      nc = Cells(r, Columns.Count).End(xlToLeft).Column + 1
      Cells(r, nc).Resize(, Cells(r, c).Value) = Cells(1, c).Value
    End If
  Next c
Next r
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the OutputLetters macro.
 
Upvote 0
If you only have 3 inputs, try this formula copied across and down.

Excel Workbook
ABCDEFGHI
1XYZ
2211XXYZ
3120XYY
4000
5004ZZZZ
6301XXXZ
Output Letters
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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