2017-01-14 8 views
0

誰かが助けることができれば、コードの最後の部分を終了できません。セルが数字でないときは、セル内のデータを削除する必要があります。VBAで特定のセルをクリアする方法

+0

すでに何をしようとしたのですか? –

+0

あなたの質問を編集し、ここにコード*をコピー&ペーストしてください。 –

+0

'.ClearContents'を' Cells(10 + x、9)に変更すると、おそらく.Value = "" '? – Stan

答えて

0

以下のコード試してみてください。IsNumeric()は問題を抱えていることができるので、あなたは少しトリッキーですSpecialCells()アプローチを試してみたいことがあり

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 
0

を:

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 
関連する問題