Simple Sub finding blanks not writing to cell

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi,
Can someone explain to me why the first Sub finds the answer but does not write to the cell but the second (which is almost identical) does?
VBA Code:
Sub mrexcel()
'check for BLANKcell cells in A:G and K:L
Dim DbWs As Worksheet
Dim FileName As String
Dim DbLastRow As Long
Dim i, j, CountBlanks, CountRecs As Long
Dim FindName As Range
Dim BLANKcell As Variant

Set DbWs = ThisWorkbook.Sheets("Database")
DbLastRow = DbWs.Cells(Rows.Count, "A").End(xlUp).Row

    CountBlanks = DbWs.Range("A:G,J:L").SpecialCells(xlCellTypeBlanks).Cells.Count
    MsgBox CountBlanks & " Errors Have Been Detected.", vbOKCancel + vbExclamation, "Database Trouble Shooting"

For i = 2 To DbLastRow
FileName = DbWs.Cells(i, 1)
    
    For j = 11 To 11 'missing Rec No in Col K
    BLANKcell = DbWs.Cells(i, j).Value
        If BLANKcell = "" Then     'countif and write to cell
            CountRecs = Application.WorksheetFunction.CountIf(DbWs.Range("A:A"), FileName)
            BLANKcell = CountRecs
            Debug.Print BLANKcell
        End If
    Next j
Next i
End Sub

Sub mrexcel2()
'check for BLANKcell cells in A:G and K:L
Dim DbWs As Worksheet
Dim FileName As String
Dim DbLastRow As Long
Dim i, j, CountBlanks, CountRecs As Long
Dim FindName As Range
Dim BLANKcell As Variant

Set DbWs = ThisWorkbook.Sheets("Database")
DbLastRow = DbWs.Cells(Rows.Count, "A").End(xlUp).Row

    CountBlanks = DbWs.Range("A:G,J:L").SpecialCells(xlCellTypeBlanks).Cells.Count
    MsgBox CountBlanks & " Errors Have Been Detected.", vbOKCancel + vbExclamation, "Database Trouble Shooting"

For i = 2 To DbLastRow
FileName = DbWs.Cells(i, 1)
    
    For j = 11 To 11 'missing Rec No in Col K
    BLANKcell = DbWs.Cells(i, j).Value
        If BLANKcell = "" Then     'countif and write to cell
            CountRecs = Application.WorksheetFunction.CountIf(DbWs.Range("A:A"), FileName)
            DbWs.Cells(i, j).Value = CountRecs
            Debug.Print DbWs.Cells(i, j).Value
        End If
    Next j
Next i
End Sub
 
I suppose excel sees them as declared but not names.
Excel sees them as declared variables of type Variant. If you want them to be Long then you would use

VBA Code:
Dim i As Long, j As Long, CountBlanks As Long, CountRecs As Long
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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