简体   繁体   中英

.clearcontents vba has bugs

I was trying to clear contents of some cells. In the range "Adjs", if the cell's font is bold, then that row I won't clear, if the cell is not bold. I will clear that entire row.. However it keeps giving me this error when I was running it "Application- defined or object-defined error"
Thank you

Sub CL()
  Application.Calculation = xlCalculationManual
  ThisWorkbook.Activate
  Sheets(1).Select
  For Each c In Range("Adjs").Cells
    If c.Font.Bold = false Then
      Range(c.Offset(0, -6), c.Offset(0, -1)).ClearContents
    End If
  Next
End Sub

Try below code:

Sub CL()
    Dim lCalMode As Long, c As Range
    lCalMode = Application.Calculation
    Application.Calculation = xlCalculationManual

    On Error Resume Next
    With ThisWorkbook.Names("Adjs").RefersToRange
        .Worksheet.Activate
        .Worksheet.Unprotect
        For Each c In .Cells
            If c.Font.Bold = False Then
                With Range(c.Offset(0, -6), c.Offset(0, -1))
                    Debug.Print "Clearing " & .Address
                    .ClearContents
                End With
            End If
        Next
        .Worksheet.Protect
    End With
    Application.Calculation = lCalMode
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM