【发布时间】:2013-11-30 12:26:43
【问题描述】:
嗨,我的工作表有 103 列和 18550 行来自数据库的数据。基于 B 列单元格的值,我必须为相应的行应用格式,例如 [如果 B2 值为 1,则该行的内部颜色应为橙色,否则为为 -1 则应为蓝色,否则为 0 则列 F & G 应为绿色,并且不应锁定这些绿色单元格。并且每个 1 值行和立即 -1 值行都应该分组。目前我有以下代码几乎需要 8 分钟的时间来应用格式。
With ThisWorkBook.Sheets("RoAe").Range("A1:A" & rowLen)
'=================For 1 valued Rows==========
Set C = .Find("1", LookIn:=xlValues)
x=0
If Not C Is Nothing Then
firstAddress = C.Address
Do
valR = Split(C.Address, "$")
actVal = valR(2)
ReDim Preserve HArray(x)
HArray(x) = actVal + 1
x = x + 1
With ThisWorkBook.Sheets("RoAe").Range("D" & actVal & ":FN" & actVal)
.Rows.AutoFit
.WrapText = True
.Font.Bold = True
.Interior.Color = RGB(252,213,180)
.Borders.Color = RGB(0, 0, 0)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Set C = .FindNext(C)
Loop While Not C Is Nothing And C.Address <> firstAddress
End If
'=================For -1 valued Rows==========
Set C = .Find("-1", LookIn:=xlValues)
y=0
If Not C Is Nothing Then
firstAddress = C.Address
Do
valR = Split(C.Address, "$")
actVal = valR(2)
ReDim Preserve HArray(y)
FArray(y) = actVal + 1
y = y + 1
With ThisWorkBook.Sheets("RoAe").Range("D" & actVal & ":FN" & actVal)
.Rows.AutoFit
.WrapText = True
.Font.Bold = True
.Interior.Color = RGB(141,180,226)
.Borders.Color = RGB(0, 0, 0)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Set C = .FindNext(C)
Loop While Not C Is Nothing And C.Address <> firstAddress
End If
'===================For 0(Zero) Valued Rows============
For p = 0 To UBound(HArray)
groupRange = "A" & HArray(p) & ":A" & FArray(p)
For i = 0 To UBound(arrUnlockMonthStart)
unlockRange = F & (HArray(p) + 1) & ":" & G & FArray(p)
ThisWorkBook.Sheets("RoAe").Range(unlockRange).Locked = False
ThisWorkBook.Sheets("RoAe").Range(unlockRange).Interior.Color = RGB(216,228,188)
Next
next
end with
ThisWorkBook.Sheets("RoAe").protect "12345"
我们可以对条件格式做同样的事情吗?根据单元格值对行应用格式和锁定/解锁。任何帮助将不胜感激。
【问题讨论】:
-
Can we do the same with Conditional Formatting. Applying format & locking/unlocking for the rows based on cell value.不,您不能以条件格式锁定/解锁单元格,但您可以使用 vba 代码首先应用条件格式,然后锁定/解锁单元格。顺便说一句,它应该需要 8 分钟。让我检查你的代码。 -
@Siddharth Rout 嗨,您能否告诉我如何使用单元格值对行应用条件格式。每当我进行条件格式设置时,它只适用于该单元格而不是该行。
-
片刻。我只是在测试 :)