dellehurley
Board Regular
- Joined
- Sep 26, 2009
- Messages
- 171
- Office Version
- 365
- Platform
- 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?
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