+ Reply to Thread
Results 1 to 2 of 2

VBA Code to Count a group of '1' values

  1. #1
    Registered User
    Join Date
    07-13-2020
    Location
    New Brunswick, Canada
    MS-Off Ver
    MS 365
    Posts
    1

    VBA Code to Count a group of '1' values

    Hi,
    I am working on a code that goes through a column of '1' and '0' values. The code needs to count the '1' values in the column if they are in adjacent rows. So essentially it need to loop through the entire column, when it gets to a value of '1' it starts counting until the next row is a '0' at which point the counter is written to the first blank row in another worksheet, then the loop should continue for the rest of the column repeating. I started both a Do...While loop and a For loop but neither seemed to work. Here they are;

    Private Sub CommandButton1_Click()

    Dim CellValue As String
    Dim i As Long
    Dim j As Long
    Dim strResult As String
    Dim endRow As Long
    Dim dtCounter As Long

    endRow = 5000
    i = 1
    dtCounter = 0

    Do While i < endRow
    dtCounter = 0
    If ActiveSheet.Cells(i, 8).Value = 1 And ActiveSheet.Cells(i + 1, 8).Value = 1 Then
    i = i + 1
    dtCounter = dtCounter + 1
    ElseIf ActiveSheet.Cells(i, 8).Value = 1 And ActiveSheet.Cells(i + 1, 8).Value = 0 Then
    i = i + 1
    ActiveSheet.Cells(i, 1).Value = dtCounter
    Else
    End If
    i = i + 1
    Loop
    End Sub





    Private Sub CommandButton2_Click()

    Dim i As Long
    Dim endRow As Long
    Dim dtCounter As Long

    For i = 1 To endRow
    dtCounter = 0
    If ActiveSheet.Cells(i, 8).Value = 1 And ActiveSheet.Cells(i + 1, 8).Value = 1 Then
    dtCounter = dtCounter + 1
    ElseIf ActiveSheet.Cells(i, 8).Value = 1 And ActiveSheet.Cells(i + 1, 8) = 0 Then
    ActiveSheet.Cell(i, 1).Value = dtCounter
    ElseIf ActiveSheet.Cells(i, 8).Value = 0 And ActiveSheet.Cells(i + 1, 8) = 0 Then
    End If
    Next i

    End Sub




    Any help would be appreciated,
    Thanks!

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: VBA Code to Count a group of '1' values

    Hi WillistonM & welcome to the forum

    Without a file to test, it is a bit difficult to provide the desired code but this is a try based on your description. This assumes your data in column A of Sheet1 and your other sheet name is Sheet2

    By the way, please take a moment to edit your post, select the code then click on the # to insert the code tags for you

    Please Login or Register  to view this content.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Count a value from a group of values
    By redJohn89 in forum Excel General
    Replies: 3
    Last Post: 01-30-2017, 05:27 PM
  2. [SOLVED] Need VBA code to group col A:A values then sum & average len of column result in A40
    By johnlara in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2015, 02:01 PM
  3. Replies: 8
    Last Post: 01-09-2014, 08:01 PM
  4. [SOLVED] Need VBA code to count number of rows & split into group of 7 rows in same sheet
    By amy_d2 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-05-2013, 11:03 AM
  5. VBA code to count specific values
    By missy22 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-20-2013, 12:58 PM
  6. Replies: 1
    Last Post: 10-19-2012, 07:55 AM
  7. [SOLVED] Count Distinct Values by Group Using Pivot Table (NM)
    By MCP in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-11-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1