The number 0 is not displayed in the cell

Sambecko

New Member
Joined
Jun 24, 2024
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hi All,
Kindly help,I have a macro that can't display the number 0, Loop through numbers 0 to 9 doesn't work well. Thank you.
#
Private Sub MissingNumber_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet3")

Dim i As Integer
Dim num As Integer
Dim found As Boolean

' Clear previous results
ws.Range("M2:V2").ClearContents
ActiveCell.NumberFormat = "0"

' Loop through numbers 0 to 9
For num = 0 To 9
found = False

' Check if num exists in range A2:J2
For i = 1 To 10 ' A to J columns
If ws.Cells(2, i).Value = num Then
found = True
Exit For
End If
Next i

' If num is not found, place it in the result range M2:V2
If Not found Then
For i = 13 To 22 ' M to V columns
If ws.Cells(2, i).Value = "" Then
ws.Cells(2, i).Value = num
Exit For
End If
Next i
End If
Next num

MsgBox "Missing numbers have been found and placed in range M2:V2.", vbInformation

End Sub
 

Attachments

  • excel.JPG
    excel.JPG
    23.3 KB · Views: 14

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
An empty cell will be treated as equivalent to 0. You should test for empty cells specifically - for example:

Code:
If ws.Cells(2, i).Value <> "" and ws.Cells(2, i).Value = num Then
 
Upvote 0
Thank you for contributing to this problem, I have tried but it hasn't worked.
 
Upvote 0
Where did you put the line suggested by @RoryA. It should go here:
VBA Code:
    ' Check if num exists in range A2:J2
        For i = 1 To 10 ' A to J columns
            If ws.Cells(2, i).Value <> "" And ws.Cells(2, i).Value = num Then   '<== line goes here
                found = True
                Exit For
            End If
        Next i

Gives results as below:
1719720645382.png


As a precaution you could also add these two lines after the 'set' statement.
VBA Code:
    Set ws = ThisWorkbook.Sheets("Sheet3")
    ws.Activate                         '<== Add this line
    ActiveWindow.DisplayZeros = True    '<== Add this line
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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