VBA to count the number of entry in a row and update in count column

swindel

New Member
Joined
Oct 11, 2021
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,

I am new in this forum and would like to understand the VBA macro, Just wanna know how can I count the number of entries in a row per criteria. I have the Unique ID, and have a couple of data and will insert the count on Count Y and Count X.

IDData 1Data 2Data 3Data 4Data 5Data 6Count YCount X
1​
XYXXX
1​
4​
2​
XXX
0​
3​
3​
YYY
3​
0​
4​
YYYXX
2​
2​
5​
0​
0​
6​
XXYY
1​
2​
7​
YYYXX
3​
2​
 
No, I mean outer loop moves through records, inner loop moves through fields. When all of the fields have been 'looked at' that inner loop is finished, thus it passes control to the outer loop which moves to the next record. Inner loop (field loop) runs again. This repeats until all the records (outer loop) have been accessed.
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi Micron,

Can you guide me how to do it. Will i use this set?

Do Until rs.EOF
For Each .Fields
Counter = Counter + 1
 
Upvote 0
Try this untested code. Substitute your table/query name where indicated.
VBA Code:
Sub Countxy()
Dim rs As DAO.Recordset, fld As Field
Dim intX As Integer, intY As Integer

Set rs = CurrentDb.OpenRecordset("tblMyTableNameHere")
If Not (rs.BOF and rs.EOF) Then
  Do While Not rs.EOF
    rs.MoveFirst
    For each fld in rs.Fields
      If rs.fld = "X" Then intX = intX + 1
      If rs.fld = "Y" Then intY = intY + 1
    Next
    rs.Fields("CountX") = intX
    rs.Fields("CountY") = intY
    rs.MoveNext
  Loop
End If

Set rs = Nothing

End Sub
 
Upvote 0
Nice catch! Possible to edit that code or would I have to re-post another version of untested code? I'd hate for anyone to just stop there and copy that. They'll end up in a never ending loop.
 
Upvote 0
Hello Micron,

Sure i will, is just that I still don't have the time to tested it at the moment. I will this week. Thank you very much.
 
Upvote 0
Hello Micron,

Apologize, i just tested it now cause i just finished my project.. There is an error with this field.
If rs.fld = "X" Then intX = intX + 1
If rs.fld = "Y" Then intY = intY + 1

and when i changed it to rs.Name the error focus on this.

rs.Fields("CountX") = intX
rs.Fields("CountY") = intY
 
Upvote 0
Try this - limited testing but it worked (change Sheet1 to the name of your table). If there is no x or y, I assumed you didn't want 0 to be written to the count fields but upon reviewing earlier post I think you might want a zero. I can fix that if required.
NOTE: you seem to be storing a calculation in a table and that is usually not advised.
VBA Code:
Sub Countxy()
Dim rs As DAO.Recordset
Dim intX As Integer, intY As Integer, i As Integer

Set rs = CurrentDb.OpenRecordset("sheet1")
If Not (rs.BOF And rs.EOF) Then
   rs.MoveFirst
   Do While Not rs.EOF
      For i = 0 To rs.Fields.Count - 1 'If your first field is autonumber, you can change 0 to 1
         If rs.Fields(i) = "X" Then intX = intX + 1
         If rs.Fields(i) = "Y" Then intY = intY + 1
      Next
      If intX > 0 Then
         With rs
            .Edit
            .Fields("CountX") = intX
            .Update
         End With
         intX = 0
      End If
      
      If intY > 0 Then
         With rs
            .Edit
            .Fields("CountY") = intY
            .Update
         End With
         intY = 0
      End If
      rs.MoveNext
   Loop
End If

Set rs = Nothing

End Sub
 
Upvote 0
This would be the 'set field to zero' code:
VBA Code:
Sub CountxyB()
Dim rs As DAO.Recordset
Dim intX As Integer, intY As Integer, i As Integer

Set rs = CurrentDb.OpenRecordset("sheet1")
If Not (rs.BOF And rs.EOF) Then
   rs.MoveFirst
   Do While Not rs.EOF
      For i = 0 To rs.Fields.Count - 1
         If rs.Fields(i) = "X" Then intX = intX + 1
         If rs.Fields(i) = "Y" Then intY = intY + 1
      Next
      With rs
         .Edit
         .Fields("CountX") = intX
         .Fields("CountY") = intY
         .Update
      End With
      intX = 0
      intY = 0
      rs.MoveNext
   Loop
End If

Set rs = Nothing

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,522
Messages
6,120,020
Members
448,939
Latest member
Leon Leenders

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