Remove data after decimals(macro correction)

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Remove data after decimals(macro correction)

Post by zyxw1234 »

Hi Experts,

Code: Select all

Sub STEP4()
  Dim Wb1 As Workbook
  Set Wb1 = ActiveWorkbook
  Set ws1 = Wb1.Worksheets.Item(2)
  With ws1
  For Each cl In Columns(11).SpecialCells(2, 1)
    cl.Value = Int(Application.Min(60, cl.Value))
  Next cl
  End With
  
  Wb1.Save
End Sub

I am using this macro & getting this error
Error Details: runt time error 1004
no cells were found
Highlighted line: For Each cl In Columns(11).SpecialCells(2, 1)

Thnx For the Help

User avatar
HansV
Administrator
Posts: 78466
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove data after decimals(macro correction)

Post by HansV »

You have seen similar problems before. You can add a test to check whether there are cells with numeric constants.
Also: Columns(11) refers to the active sheet, not necessarily to Ws1 - same as in your question from yesterday.

Code: Select all

Sub STEP4()
    Dim Wb1 As Workbook
    Dim Ws1 As Worksheet
    Dim rng As Range
    Dim cl As Range
    Set Wb1 = ActiveWorkbook
    Set Ws1 = Wb1.Worksheets.Item(2)
    ' Temporarily suppress error messages
    On Error Resume Next
    ' Try to get the range of numeric constants
    Set rng = Ws1.Columns(11).SpecialCells(xlCellTypeConstants, xlNumbers)
    ' Turn on normal error handling again
    On Error GoTo 0
    ' Only proceed if we found numeric constants
    If Not rng Is Nothing Then
        For Each cl In rng
            cl.Value = Int(Application.Min(60, cl.Value))
        Next cl
        Wb1.Save
    End If
End Sub
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Remove data after decimals(macro correction)

Post by zyxw1234 »

Thnx Alot HansV Sir for helping me in solving this Problem
Problem Solved
Have a Awesome Day Sir