2017-04-11 79 views
2

我需要有条件地格式化根据加载的数据左右移动的三列。条件是单元格值为空(="")。我真的很难找出这个问题。更改范围的条件格式化

这里是我的设置:

Sub Conditional_Format() 
    Dim ws2 As Worksheet 
    Dim lRow3 As Long, lCol3 As Long 
    Dim rng8 As Range, rng9 As Range 

    Set ws2 = Worksheets("Unfav GT500 Comments") 

    With ws2 
     lRow3 = .Range("A" & .Rows.Count).End(xlUp).Row 
     lCol3 = .Cells(3, .Columns.Count).End(xlToLeft).Column 

     Set rng8 = .Range(.Cells(4, lCol3 - 2), .Cells(lRow3 - 1, lCol3 - 1)) 
     Set rng9 = .Range(.Cells(4, lCol3), .Cells(lRow3 - 1, lCol3)) 
    End With 
End Sub 
+0

首先,您错过了'End With'。另外,你需要在'rng8'之前'Dim'。你的数据是什么样的?你在哪里寻找空白的细胞?您的帖子似乎有点遗漏。 – BruceWayne

+0

为什么你需要使用VBA?另外,我没有看到CF的任何代码行。你想完成什么? – L42

回答

1

对于动态变化的范围,我建议使用一个搜索到第一位置的列。是否有任何固定的唯一列标题,您正在寻找和在电子表格的任何特定区域?如果是这样,请使用下面的内容来搜索它。您可以更改格式和列标题以适合您的数据。

'Below row will get you the column header. Repeat this line to search for the three columns. 
ws2.Cells.Find(What:="Specific_Column_Header", LookIn:= xlValues).Activate 
startrow = ActiveCell.Row 

For row1 = startrow to ws2.Cells(.Rows.Count, "A").End(xlUp).Row 
    If Cells(row1, ActiveCell.Column) = "" Then 
     'Set your format here 
     Cells(row1,ActiveCell.Column).Select 
     With Selection.Interior 
      .Pattern = xlSolid 
      .ThemeColor = xlThemeColorDark1 
      .TintAndShade = -0.15 
     End With 
    End If 
Next 
+0

谢谢,先生!超级有用。 – ERKSMTY