2017-10-30 15 views
0

を削除し、これはスクリプトです:は、私は理解できない問題を抱えているセル

Sub WorksheetLoop() 

    Dim WS_Count As Integer 
    Dim I As Integer 
    Dim ws As String 
    Dim sht As Worksheet 

    ' Set WS_Count equal to the number of worksheets in the active 
    ' workbook. 
    WS_Count = ActiveWorkbook.Worksheets.Count 
    ws = ActiveSheet.Name 

    ' Begin the loop. 
    For I = 1 To WS_Count 

     For Each sht In ActiveWorkbook.Worksheets 
      ws = Worksheets(I).Name 
      Dim x As Range 
      Set x = Worksheets(I).UsedRange 
      x.ClearComments 

      x.Replace What:="*€£$*", Replacement:="" 

      For Each Cell In Worksheets(I).UsedRange 
       If x.Font.Color <> Black Then 
        x.Delete 
       End If 
      Next 
     Next sht 

    Next I 

End Sub 

コードのすべてを除いて動作します:

For Each Cell In Worksheets(I).UsedRange 
    If x.Font.Color <> Black Then 
      x.Delete 
    End If 
Next 

これを黒色に変更しようとしましたが、これはまだ動作しません。私はその時点でステップアップを試みました。セルを削除する資格を持つセルは決して見つけられません。

+0

私はフォントが、実際に黒ではないと思いたいです。セルの1つを選択し、 'Activecell.font.color'をテストしようとしましたか? – Rory

+0

私が達成しようとしている主なことは、フォントの色が黒でないすべてのセルを削除することです。確かに黒ではないセルがありますが、私はアクティブセルのアイデアを試してみましょう。ありがとうございました –

+0

すべてのセルは複数の色を含む可能性があります。任意のセルの最初の文字は黒で、2番目の文字は赤で、3番目の文字は緑になる可能性があります。したがって、任意のセルの最初の文字を次のようにチェックすることができます: 'x.Characters(1,1).Font.Color <> vbBlackおよびx.Characters(1,1).Font.Color <> 5920858それから。 – Ralph

答えて

1

ループ内でCellを参照する必要があります。このように:あなたのループFor Each Cell In Worksheets(I).UsedRange

For Each Cell In Worksheets(I).UsedRange 
    If Cell.Font.Color <> vbBlack Then 
      Cell.Delete 
    End If 
Next 
+0

ありがとう!それは私が行方不明だったことです、私はそれが何か単純なことを知っていた –

0

VBAで直接色を参照することはできません。代わりに、数値参照で作業する必要があります。黒が0であるので、あなたのコードは、それはあなたのコードで定義した変数ですので、私はまた、xに、セル毎に変更しました。この

For Each x In Worksheets(I).UsedRange 
    If x.Font.Color = 0 Then 
      x.Delete 
    End If 
Next 

ようになりべきです。この場合、セルが黒と等しいかどうかをテストしたい場合は、

+0

返信いただきありがとうございます。実際には、フォントの色が黒でないすべてのセルを削除したいので、 "<>"を使用してください。残念ながら、これはまだ動作しません、私はまた、カラーインデックスを使用するときに使用する必要がありますので、私は思ったx.Font.ColorIndexを試してみましたか? –

+0

あなたの質問のタイトルを修正する必要があります –

+0

ありがとう、私は逃した必要があります! –

0

、あなたはCellRangeオブジェクト)との範囲をループしている、とIf x.Font.Color <> Black Then代わりのIf Cell.Font.Color <> Black Then場合、その後、あなたがチェックしています。

また、Cell.Font.Colorは数値を返します。これはvbBlackと一致し、Blackではありません。

修正コード

Option Explicit 

Sub WorksheetLoop() 

Dim WS_Count As Long 
Dim I As Long 
Dim ws As String 
Dim sht As Worksheet 
Dim x As Range ' define outside the loop 
Dim C As Range 

' Set WS_Count equal to the number of worksheets in the active workbook 
WS_Count = ActiveWorkbook.Worksheets.Count 

' Begin the loop. 
For I = 1 To WS_Count 
    With Worksheets(I) 
    'For Each sht In ActiveWorkbook.Worksheets ' <-- No need for this loop 
     ws = .Name 

     Set x = .UsedRange 
     x.ClearComments 

     x.Replace What:="*€£$*", Replacement:="" 

     For Each C In x ' I replaced Cell with C (Cell is too close to Cells) 
      If C.Font.Color <> vbBlack Then 
       C.Delete 
      End If 
     Next C 
'  Next sht 
    End With 

Next I 

End Sub 
関連する問題