我有以下 Excel-vba 代碼,它洗掉所有洗掉線文本:
Private Sub DelStrikethroughText()
'Deletes strikethrough text in all selected cells
Dim Cell As Range
For Each Cell In Selection
DelStrikethroughs Cell
Next Cell
End Sub
Private Sub DelStrikethroughs(Cell As Range)
'deletes all strikethrough text in the Cell
Dim NewText As String
Dim iCh As Integer
For iCh = 1 To Len(Cell)
With Cell.Characters(iCh, 1)
If .Font.Strikethrough = False Then
NewText = NewText & .Text
End If
End With
Next iCh
Cell.Value = NewText
Cell.Characters.Font.Strikethrough = False
End Sub
代碼本身在一小部分單元格上運行良好,但是,選擇超過 10 個左右的單元格會導致錯誤 400。
我的代碼總是在第一行的最后一個單元格失敗(所以這取決于我的選擇)。所以宏被中斷了,就像它應該改變到第2行一樣。游標停在這一行NewText = NewText & .Text但是為什么會出現問題?
任何幫助,將不勝感激。為什么 Excel 會拋出錯誤 400,我的代碼需要修復什么?
提前感謝您的任何幫助。
uj5u.com熱心網友回復:
Cell.Characters.Text 不適用于包含公式、數字、日期等的單元格。您可以改用 Mid() 函式,但這可能會將現有數字轉換為文本:
Private Sub DelStrikethroughText()
'Deletes strikethrough text in all selected cells
Dim Cell As Range
For Each Cell In Selection
If Cell.NumberFormat = "General" Then DelStrikethroughs Cell
Next Cell
End Sub
Private Sub DelStrikethroughs(Cell As Range)
'deletes all strikethrough text in the Cell
Dim NewText As String
Dim iCh As Integer
For iCh = 1 To Len(Cell)
With Cell.Characters(iCh, 1)
If .Font.Strikethrough = False Then
NewText = NewText & Mid(Cell, iCh, 1)
End If
End With
Next iCh
Cell.Value = NewText
Cell.Characters.Font.Strikethrough = False
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/338661.html
下一篇:excel按時間計劃分配小時值
