0
如果有人可以協助,我無法完成代碼的最後部分。當一個單元格不是數字時,我需要它刪除單元格中的數據。如何清除VBA中的特定單元
如果有人可以協助,我無法完成代碼的最後部分。當一個單元格不是數字時,我需要它刪除單元格中的數據。如何清除VBA中的特定單元
嘗試下面的代碼:
Sub ValueOnly()
Dim x As Integer
Application.ScreenUpdating = False
With Sheets("Consolidated Data")
For x = 1 To 3107
With .Cells(10 + x, 9)
If Not IsNumeric(.Value) Then .ClearContents
End With
With .Cells(10 + x, 10)
If Not IsNumeric(.Value) Then .ClearContents
End With
Next x
End With
End Sub
因爲IsNumeric()
可以有問題,你可能想嘗試SpecialCells()
方法,這是有點麻煩:
Option Explicit
Sub ValueOnly()
Dim numericRng As Range, lastNumericRng As Range, lastRng As Range
Dim iArea As Long
With Sheets("Consolidated Data").Range("I11:I3317").SpecialCells(xlCellTypeConstants) '<--| consider only your wanted range "not blank" values
Set numericRng = .SpecialCells(xlCellTypeConstants, xlNumbers) '<--| store "numeric" values
If Intersect(.Cells(1), numericRng) Is Nothing Then '<--| check if first value is not numeric
.Parent.Range(.Cells(1), numericRng(1).Offset(-1)).ClearContents
End If
With numericRng
For iArea = 2 To .areas.Count '<--| clear all not numeric values between numeric ones
.Parent.Range(.areas(iArea - 1).Offset(.areas(iArea - 1).Count).Resize(1), _
.areas(iArea).Resize(1).Offset(-1)).ClearContents
Next
End With
Set lastRng = .areas(.areas.Count).Cells(.areas(.areas.Count).Count)
If Intersect(lastRng, numericRng) Is Nothing Then '<--| check if last value is not numeric
With numericRng
Set lastNumericRng = .areas(.areas.Count).Offset(.areas(.areas.Count).Count).Resize(1)
End With
.Parent.Range(lastNumericRng, lastRng).ClearContents
End If
End With
End Sub
那你嘗試了嗎? –
請編輯您的問題,並在此處複製/粘貼代碼*。 –
也許如果你把'.ClearContents'改成'Cells(10 + x,9).Value =「」'? – Stan