Formula to format worksheet to numbers and replace empty cells with 0

vbanovice123

Board Regular
Joined
Apr 15, 2011
Messages
91
Hi,

I would like to make my macro work automatically without having to do the below three manual steps:
1) Delete titles, text data and column headers that are text
2) Format the sheet containing percent and text data to number
3) Replace blank cells with 0

I read that there is a way to determine the data type for nulls/empty cells within the sheet as below:

IsEmpty(variable) and IsNull(variable) . Is this so and how can I apply it to the macro.

Also if I need to reformat cells to a number , can I use the format funtion?

The manual steps used above defeats the purpose of the macro as it is not full automated.

Any help is greatly appreciated.

Regards,
Anita
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Maybe like this

Code:
Sub test()
On Error Resume Next
With ActiveSheet.UsedRange
    .SpecialCells(xlConstants, xlTextValues).ClearContents
    .NumberFormat = "0.00"
    .SpecialCells(xlCellTypeBlanks).Value = 0
End With
End Sub
 
Upvote 0
Hi,

Thanks for the reply. I added the code you gave me as a Private subroutine and I get a "Type Mismatch" error. When I do the below steps in your code manually, whiel the rest is taken care of by the macro, I do not get the "Type Mismatch" error.

Should I take out the Errhandle subroutine?

Private Sub test()

On Error Resume Next
With ActiveSheet.UsedRange
.SpecialCells(xlConstants, xlTextValues).ClearContents
.NumberFormat = "0.00"
.SpecialCells(xlCellTypeBlanks).Value = 0
End With
End Sub



' Clear all the indicators that previous comparisons may have set.
Private Sub ClearMarkers()
' Clear Sheet1
Sheet1.Select
Sheet1.Cells.Select
Selection.Interior.ColorIndex = xlNone
Selection.Font.ColorIndex = 0
Sheet1.Cells(1, 1).Select ' undo selection of entire sheet
' Clear Sheet2
Sheet2.Select
Sheet2.Cells.Select
Selection.Interior.ColorIndex = xlNone
Selection.Font.ColorIndex = 0
Sheet2.Cells(1, 1).Select ' undo selection of entire sheet
End Sub
Private Sub test()

On Error Resume Next
With ActiveSheet.UsedRange
.SpecialCells(xlConstants, xlTextValues).ClearContents
.NumberFormat = "0.00"
.SpecialCells(xlCellTypeBlanks).Value = 0
End With
End Sub
Sub CompareDiff()

On Error GoTo ErrHandle
Call ClearMarkers

' Determine range of used cells, using the highest row and column counts
' found in either of the two sheets.
Dim HighRow As Long
HighRow = Sheet1.UsedRange.Rows.Count
If Sheet2.UsedRange.Rows.Count > HighRow Then
HighRow = Sheet2.UsedRange.Rows.Count
End If
Dim HighCol As Long
HighCol = Sheet1.UsedRange.Columns.Count
If Sheet2.UsedRange.Columns.Count > HighCol Then
HighCol = Sheet2.UsedRange.Columns.Count
End If

' Walk through the cells of both sheets, comparing and changing colors
' if differences are found.
Dim RowIndex As Long
Dim ColIndex As Long

Dim RowFirst As Long
Dim ColFirst As Long

For RowIndex = 1 To HighRow

For ColIndex = 1 To HighCol

' Compare formulas, not "text" or other formatting-affected attributes.
If Sheet1.Cells(RowIndex, ColIndex).Formula <> Sheet2.Cells(RowIndex, ColIndex).Formula Then
If CLng(Sheet1.Cells(RowIndex, ColIndex).Value) <> CLng(Sheet2.Cells(RowIndex, ColIndex).Value) Then

' Determine how to indicate the difference on Sheet1.
If Sheet1.Cells(RowIndex, ColIndex).Text = "" Then
' If the cell contains no text, highlight the empty cell.
Sheet1.Select
Sheet1.Cells(RowIndex, ColIndex).Select
Selection.Interior.ColorIndex = 38
Else
' If the cell contains text, color the text.
Sheet1.Cells(RowIndex, ColIndex).Font.Color = &HFF
End If

' Determine how to indicate the difference on Sheet2.
If Sheet2.Cells(RowIndex, ColIndex).Text = "" Then
' If the cell contains no text, highlight the empty cell.
Sheet2.Select
Sheet2.Cells(RowIndex, ColIndex).Select
Selection.Interior.ColorIndex = 38
Else
' If the cell contains text, color the text.
Sheet2.Cells(RowIndex, ColIndex).Font.Color = &HFF
End If

' If this is the first row containing a difference, remember the cell
' in which the change occurred so we can return to it later.
If RowFirst = 0 Then
RowFirst = RowIndex
ColFirst = ColIndex
End If
End If
End If
Next
Next

' Either report no differences or focus on the first difference found.
If RowFirst = 0 Then
MsgBox "No differences!"
Else
If ThisWorkbook.ActiveSheet.Index = 1 Then
Sheet1.Cells(RowFirst, ColFirst).Activate
End If
If ThisWorkbook.ActiveSheet.Index = 2 Then
Sheet2.Cells(RowFirst, ColFirst).Activate
End If
End If

Exit Sub

ErrHandle:
MsgBox Err.Description
End Sub
 
Upvote 0
I also noticed that I was not calling the test subroutine.

So I added the below:

Sub CompareDiff()

'On Error GoTo ErrHandle
Call ClearMarkers
Call test

The error occurs at the below line.


If CLng(Sheet1.Cells(RowIndex, ColIndex).Value) <> CLng(Sheet2.Cells(RowIndex, ColIndex).Value) Then
 
Upvote 0
Hi

Can someone please help. What could be the issue?

Error on

If CLng(Sheet1.Cells(RowIndex, ColIndex).Value) <> CLng(Sheet2.Cells(RowIndex, ColIndex).Value) Then


Regards,
 
Upvote 0
That will error if the cells being compared do not contain numbers or dates.
 
Upvote 0
Any suggestions on how to get around the issue.

The data can be formatted to ignore dates. Or can I use a function to check for number / date format before doing the comparison?

I am using the Clng function to ignore the decimal places and hence not report any differences. example 41.234567 and 41.2345999 should not be reported as a difference. Using the CLng helped resolve that issue.

Thanks for your help.

Regards,
vbanovice123
 
Upvote 0
Try like this

Code:
If IsNumeric(Sheet1.Cells(RowIndex, ColIndex).Value) And IsNumeric(Sheet2.Cells(RowIndex, ColIndex).Value) Then
    If CLng(Sheet1.Cells(RowIndex, ColIndex).Value) <> CLng(Sheet2.Cells(RowIndex, ColIndex).Value) Then
 
Upvote 0
This worked fine and I do not get errors.

However, the macro is reporting differences for the first decimal that differs.

example 70.9291159668566 and 70.1334077335272 should not report since the integer part is the same.

All the values that differ in the first decimal are showing as differences.

Is there a way to ignore decimals and just check for integer part?

Thanks again for your help.

Regards,
vbanovice123
 
Upvote 0
All worked fine. Thanks for you help.

Using ActiveSheet replaces zero in just one sheet which is active. The second sheet retains the special format and I get differences.

Since I am comparing 2 worksheets with the same data structure, instead of using activesheet, I need to use each of the referenced sheet name: Sheet1 and Sheet2 in this case.

Hence I use two subroutine and call them separately. Is there a better way of doing this? The below approach works but I am not sure if it is an efficient one.

Sub test1()

On Error Resume Next
With Sheet1.UsedRange
.SpecialCells(xlConstants, xlTextValues).ClearContents
.NumberFormat = "0"
.SpecialCells(xlCellTypeBlanks).Value = 0
End With
End Sub
Sub test2()

On Error Resume Next
With Sheet2.UsedRange
.SpecialCells(xlConstants, xlTextValues).ClearContents
.NumberFormat = "0"
.SpecialCells(xlCellTypeBlanks).Value = 0
End With
End Sub

Sub CompareDiff()

On Error GoTo ErrHandle
Call ClearMarkers
Call test1
Call test2

Regards,
vbanovice123
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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