在工作表 VBA 上插入值之前或之后更改整行的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19733769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change color of entire row before or after inserting values on a sheet VBA
提问by Splendonia
I'm Trying to change the value of a row when a determine condition happens, and I was thinking of doing it after the insert was made, but maybe it's easier before the insert (?) The thing is i've tried both, and i'm doing it wrong because it's not working
我正在尝试在确定条件发生时更改行的值,并且我正在考虑在插入后执行此操作,但在插入之前可能更容易(?)问题是我已经尝试了这两种方法,并且我做错了,因为它不起作用
Code:
代码:
With ThisWorkbook.Worksheets("Site Configuration List")
' Tried this .Range("A").EntireRow.Interior.Color = 49407
.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Code
.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = Name
.Range("C" & Rows.Count).End(xlUp).Offset(1).Value = "" & Contract & ""
.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = SiteCode
.Range("E" & Rows.Count).End(xlUp).Offset(1).Value = SiteName
.Range("G" & Rows.Count).End(xlUp).Offset(1).Value = Approver
.Range("K" & Rows.Count).End(xlUp).Offset(1).Value = ItemCode
.Range("M" & Rows.Count).End(xlUp).Offset(1).Value = RequiredQty
.Range("N" & Rows.Count).End(xlUp).Offset(1).Value = ControlFlag
If Color = 0 Then
', this .Offset(-1).Interior.Color = 49407
',this .Interior.Color = 49407
'and this .EntireRow.Interior.Color = 255
Else
End If
End With
Obviously not at the same time, but no of those work. What am I doing wrong and how can I fix it? I'm new to VBA and some of the simplest things usually take me more time to figure out. What I've used in order to try to achieve what I want I've taken it from other similar questions. The call to the sub where the code above is implemented, is inside a loop, however, with each one of the things I've tried, when it gets to the line the program just stops. No error or anything.
显然不是同时,但没有这些工作。我做错了什么,我该如何解决?我是 VBA 的新手,一些最简单的事情通常需要我花更多时间来弄清楚。我为了尝试实现我想要的东西而使用的东西我从其他类似的问题中得到了它。对实现上述代码的 sub 的调用在一个循环内,但是,对于我尝试过的每件事,当它到达该行时,程序就会停止。没有错误或任何东西。
回答by Jaycal
The range was not declared correctly. instead of
范围未正确声明。代替
.Range("A").EntireRow.Interior.Color = 49407
it needs to be
它需要是
.Range("1:1").EntireRow.Interior.Color = 49407
if you want to hightlight the entire row or
如果您想突出显示整行或
.Range("A:A").EntireColumn.Interior.Color = 49407
to highlight the entire column.
突出显示整个列。
EDIT
编辑
To color the last row of data, you'll need to use the Rows
method, along with the number of the last row that you get from your Rows.Count
. Code would look like the following
要为最后一行数据着色,您需要使用该Rows
方法,以及从Rows.Count
. 代码如下所示
Rows(Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Interior.Color = 49407
Or
或者
Rows(Range("A" & Rows.Count).End(xlUp).offset(1).Row) _
.EntireRow.Interior.Color = 49407
回答by DaveU
Changing your code as follows should do the trick.
如下更改您的代码应该可以解决问题。
With ThisWorkbook.Worksheets("Site Configuration List")
.Range("A" & Rows.Count).End(xlUp).Offset(1).EntireRow.Interior.Color = 49407
.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Code
.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = Name