我正在尝试为电子表格创建边框并对两个值进行比较。要做到这一点,我必须增加我的范围内的行,但我使用的公式不会增加。
Set ws = Sheets(strSheetName)
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Stop
'Create borders
For Each c In Range("A1:A" & lRow)
If Trim(c.Value) = PracticeName Then First = "A" & c.Row
If Trim(c.Value) = "SubTotal" Then SubTotal = "B" & c.Row
If Trim(c.Value) = "" Then
Set Rng = Sheets(strSheetName).Range(First & ":" & SubTotal)
Call SetRangeBorder(Rng)
End If
If Trim(c.Value) = PracticeName Then FirstDataRow = "B" & c.Row + 2
If Trim(c.Value) = "IDX" Then LastDataRow = "B" & c.Row
Stop
If LastDataRow <> "" And SubTotal <> "" Then
If Trim(c.Value) = "SubTotal" Then Range(SubTotal).Formula = "=SUM("
& FirstDataRow & ":" & LastDataRow & ")"
'Check if Values are equal
Stop
compare1 = Range(SubTotal)
FinalRow = "B" & c.Row + 1
compare2 = Range(FinalRow)
'If equal set color to green
If compare1 = compare2 Then Range(FinalRow).Interior.ColorIndex = 4
'If not equal set color to red
If compare1 <> compare2 Then Range(FinalRow).Interior.ColorIndex = 3
Range("A" & c.Row) = Range("A" & c.Row).Offset(3, 0)
Stop
End If
Next
答案 0 :(得分:0)
你可以改用它,它会起作用:
For i = 1 to lRow
在引用您使用的单元格时
ws.Cells(i,1).Value
&#34; i&#34;是行号,&#34; 1&#34;是列号。这将在您想要的任何列中增加行。这种方法只是偏好,对我来说更容易想象。