2017-08-30 71 views
0

需要插入VDB(i,35)=时删除整行内部颜色变色指数22(珊瑚)的语句。根据列值更改整行的内部颜色VBA

我需要在这个块内执行这一步,而不需要在新块中添加代码(除非绝对必要),因为工作表有20K个条目。我假设,因为这是我确定物品何时处于“已删除”状态的位置,并且将“已删除”放在第35列中,我应该能够在同一步骤/区块中对其进行着色,并且这将是最有效的方法。我可能是错误的..

是否有另一行我可以在最后一行后添加,这将彩色索引35中的这些条目=“已删除”吗?

我曾尝试将vDB(i,35)作为范围传递给另一个变量,并对其进行设置,然后使用if =删除以更改entirerow.interior.color index = 22,但无法获取措辞正确,并可能采取错误的做法。我仍然处于学习的曲线中,但在仔细研究小组之前,试图弄清楚我自己的问题,但我似乎无法做到。

这里是剪切它。

'Execute Find (Vlookup) 

For i = 1 To UBound(vDB, 1) 
'If sht.Cells(i, 1).Value <> "" Then 
    If vDB(i, 1) <> "" Then 
     Set rng = rngData.Find(vDB(i, 1), LookIn:=xlValues, Lookat:=xlWhole) 'Matches entire contents of cell for an exact match 
     If Not rng Is Nothing Then 
      'If found return value of column 9 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix monthly ABC Code column, as determined by variable 
      vDB(i, ABCCodeCell) = rng.Offset(, 7) 
      'If found, return the value of column 7 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix column 27 
      vDB(i, 27) = rng.Offset(, 5) 
      'If found, return the value of column 11 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix column 34 
      vDB(i, 33) = rng.Offset(, 9) 
      'If found, place value of ABCMatrixMonthSelect.ComboBox1 in column AO Col Index 41 
      vDB(i, 41) = ABCMatrixMonthSelect.ComboBox1.value 
     Else 
      vDB(i, 35) = "Deleted" 
      vDB(i, 41) = ABCMatrixMonthSelect.ComboBox1.value 
      With vDB(i, 1) = sht.Cells.Interior.Color = RGB(247, 119, 109) 'Light Red 
      End With 
     End If 
    End If 
    If vDB(i, ABCCodeCell) = vDB(i, lastMonthABCCode) Then 
    vDB(i, 36) = "No" 
    Else 
    vDB(i, 36) = "Yes" 
    End If 
DoEvents 
Next 
rngDB = vDB 

Dim LR As Long 
LR = sht.Cells(Rows.Count, 1).End(xlUp).Row 
sht.Cells.FormatConditions.Delete 
With sht.Range("1:" & LR) 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AI1=""Deleted""" 'Searches for value "Deleted" in Range 1 to last row 
    With .FormatConditions(.FormatConditions.Count) 
     .SetFirstPriority 
     With .Interior 
      .Color = RGB(247, 119, 109) 'Light Red 
     End With 
    End With 
End With 

'Reset Excel Status Bar 
Application.StatusBar = False 
e here 
+0

您是否尝试过使用条件格式? – Cyril

+0

我需要这个VBA来获得报告。不是我需要设置的东西,或者依赖于用户来设置的,除非您通过VBA说我可以设置条件格式,对着色行进行着色......这可能会有所不同。这是一个巨大的宏,从各种报告中获取价值,并逐月编译。在35中的“已删除”值的行将突出显示,直到处理报告的下个月,然后我将它们清除,重新运行以识别新的“删除:”的步骤。但是我可以轻松地擦除它们价值35(因为这是隐藏的我自己的意图),如果这是更好的 – SharePoint0508

+0

我打开任何最有意义的方式,是最好的办法,并且运作最快,因为这是一个如此大的工作表。 – SharePoint0508

回答

0

如果你可以使用条件格式,使用规则类型:使用公式确定哪些单元格格式化,在公式编辑栏中键入= $ AJ35 =“删除”,然后定义你的范围,例如作为= 1:1000。 $在公式中的位置是关键。然后,您将选择满足条件时发生的情况并设置规则。

您可以从VBA做到这一点,设置条件格式,如:

Dim LR as Integer 
LR = Cells(Rows.Count, 1).End(xlUp).Row 'just finding the row # of the last row 
sht.Cells.FormatConditions.Delete 
With sht.Range("1:" & LR) 'The range you're working with... specify the Rows() to ensure the whole row is colored 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AI1=""Deleted""" 'Where you put the cell you want to evaluate and criteria 
    With .FormatConditions(.FormatConditions.Count) 
     .SetFirstPriority 
     With .Interior 
      .Color = RGB(0, 70, 255) 'Arbitrary; you will need to find the specific color you want. 
      .TintAndShade = 0.8 
     End With 
    End With 
End With 

注意,我删除每次现有的条件。

此外,另一种选择是只说,不使用条件格式:

With sht.Rows(i).Interior 
    .Color=RGB(0,255,0) 'arbitrary color 
    .TintAndShade = 0.8 
End With 

编辑:有了您的调整代码和渴望列1到35进行彩色编码:

Else 
    vDB(i, 35).Value = "Deleted" 
    With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior 
     .Color = RGB(247, 167, 184) 'light red 
     .TintAndShade = 0.8 
    End With 
End If 

我在想你的vDB有什么问题,所以也许它会更适合使用单元格?

Else 
    Cells(i, 35).Value = "Deleted" 
    With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior 
     .Color = RGB(247, 167, 184) 'light red 
     .TintAndShade = 0.8 
    End With 
End If 

同理:

Dim i, LR as Integer 
LR = Cells(Rows.Count,1).End(xlUp).Row 'Assumes row 1 is contiguous... probably not, given the rest of this code 
For i = 1 To LR 
    If Cells(i, 1) <> "" Then 
     Set rng = rngData.Find(Cells(i, 1), LookIn:=xlValues, Lookat:=xlWhole) 
     If Not rng Is Nothing Then 
      Cells(i, ABCCodeCell).Value = rng.Offset(, 7).Value 
      Cells(i, 27).Value = rng.Offset(, 5).Value 
      Cells(i, 34).Value = rng.Offset(, 9).Value 
     Else 
      Cells(i, 35).Value = "Deleted" 
      With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior 
       .Color = RGB(247, 167, 184) 'light red 
       .TintAndShade = 0.8 
      End With 
+0

该值在AI或列索引35中,但它需要被表达,并且在列AI内的任何单元格的值为“已删除”时,它需要是浅红色/珊瑚色索引22。只需要对单元格着色,我需要在AI的值被删除的任何行中突出显示整行或从A到AI的范围,直到表单中包含的最后一行。一个月又一个月,最后一行将移动,因为每个月更新大约增加1500行。 – SharePoint0508

+0

我尝试使用条件格式,我没有看到在哪里定义范围,或设置整个行。我似乎无法得到这个工作 – SharePoint0508

+0

它也给第二行的语法错误:与sht.Range(sht.Rows(1),sht.Rows(sht.Cells(sht.Rows.Count.End( xlUp).Row,1)) – SharePoint0508