2016-12-07 47 views
2

我是VBA編碼新手,請幫助我創建一個符合以下條件的VBA腳本。如何使用vba突出顯示小數或小於3且大於6的字符的單元格?

  1. 應突出顯示包含小數的單元格。
  2. 應該突出顯示字符數小於3或大於6的單元格。
  3. 應該從列G(G1)執行直到最後一行最後使用的單元格。

我的數據是字母數字或數字。

我試過使用characters.countValue.count但它沒有解決。希望它能與len一起工作,但我不確定如何開始。

附件是樣品excel文件與顯現細胞

我曾嘗試下面的代碼。由於我的數據是字母數字,所以字符數不會有幫助。

Sub HighlightCells() 
Range(" G1").Select 
Do 
    If ActiveCell.Characters.Count < 3 Then 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 65535 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
    End If 
    ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row  last used cell 
Loop Until ActiveCell = "" 

Range(" G1").Select 
Do 
    If ActiveCell.Characters.Count > 6 Then 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 65535 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
    End If 
    ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row last used cell 
Loop Until ActiveCell = "" 
End Sub 
+0

的條件是***和***條件或*** *** OR條件??? –

+0

如果包含***沒有***字符,您希望單元格突出顯示嗎? –

+0

條件僅限OR。沒有字符的空白單元不需要突出顯示。 – Mahesh

回答

0

前:

enter image description here

此代碼幾乎是直接翻譯你的英語描述成VBA:

Sub Dural() 
    Dim N As Long, i As Long, s As String, L As Long 

    N = Cells(Rows.Count, "G").End(xlUp).Row 
    For i = 1 To N 
     s = Cells(i, "G").Text 
     L = Len(s) 
     If InStr(1, s, ".") > 0 Or (L < 3 Or L > 6) Then 
      With Cells(i, "G").Interior 
       .Pattern = xlSolid 
       .PatternColorIndex = xlAutomatic 
       .Color = 65535 
       .TintAndShade = 0 
       .PatternTintAndShade = 0 
      End With 
     End If 
    Next i 
End Sub 

後:

enter image description here

+0

謝謝加里。它的工作輝煌..!你是否也可以在其中包含其他行。我的意思是G,H,I,J ... BX,BY,BZ.它現在只在G行中有效。 – Mahesh

+0

@Mahesh我需要一個完整列表 –

+0

嗨加里,我看不到你的評論完全。 – Mahesh

0
Sub Test() 
    Application.ScreenUpdating = False 
    LastRow = Rows(ActiveSheet.UsedRange.Row + _ 
    ActiveSheet.UsedRange.Rows.Count - 1).Row 
    LastCol = Columns(ActiveSheet.UsedRange.Column + _ 
    ActiveSheet.UsedRange.Columns.Count - 1).Column 
    For Each cll In Range(Cells(1, 7), Cells(LastRow, LastCol)) 
    s = cll.Value 
    l = Len(s) 
    If ((l > 0) And (l < 3)) Or (l > 6) Or (s Like "*#.#*") _ 
     Then cll.Interior.Color = vbRed 
    Next cll 
    Application.ScreenUpdating = True 

結束子

+0

上面的代碼對我來說非常合適。 – Mahesh

相關問題