VBA code to count anything number over 5 digits per row

Kingchaos64

New Member
Joined
Jan 15, 2021
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hello

I would like a code that would count all numbers over 4 digits per cell, per row and that total count per row would show up in that rows C Column.

I have a formula that kinda works but it gets easily messed up and since this will be used by other people I need something that can't be accidentally deleted or changed.
 
Maybe:

Code:
Sub Over4Digits()
Dim lr As Long, i As Long, rng As String
lr = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
 For i = 1 To lr
 rng = "A" & i & ":B" & i
Sheets("Sheet1").Range("C" & i) = Application.WorksheetFunction.CountIf(Range(rng), ">=1000")
 Next i
End Sub
 
Upvote 0
Solution

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You want to count how many columns in a row which contains only number with more than 4 digits.

The code will count from row E5 to wherever row ends and count for number over 4 digit from column E5 to wherever column ends. The resulting count is written in column C in each row

VBA Code:
Sub CountElement()

Dim nCount&
Dim cellRow As Range, cellCol As Range
Dim rngRow As Range, rngCol As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.ActiveSheet
Set rngRow = ws.Range("E5", ws.Cells(Rows.Count, "E").End(xlUp))

For Each cellRow In rngRow
    nCount = 0
    Set rngCol = ws.Range("E" & cellRow.Row, ws.Cells(cellRow.Row, Columns.Count).End(xlToLeft))
    For Each cellCol In rngCol
        If IsNumeric(cellCol) And Len(cellCol) > 4 Then
            nCount = nCount + 1
        End If
    Next
    ws.Range("C" & cellRow.Row) = nCount
Next
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,831
Members
449,051
Latest member
excelquestion515

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