使用VBA选择并高亮显示Excel行
如何让Excel按行号高亮显示行。例如,假设我想要突出显示第6、10、150、201行。谢谢使用VBA选择并高亮显示Excel行,vba,excel,rows,highlight,Vba,Excel,Rows,Highlight,如何让Excel按行号高亮显示行。例如,假设我想要突出显示第6、10、150、201行。谢谢 objWB.Cells(rowNum,201).EntireRow.Interior.ColorIndex = 6 etc对于基本VBA代码,您始终可以开始录制宏,执行操作,停止录制,查看生成的代码,然后清理这些代码以执行所需操作。例如,记录高亮显示一行的操作(设置Interior.Color的值)将为您提供: Rows("13:13").Select Range("C13").Activate Wi
objWB.Cells(rowNum,201).EntireRow.Interior.ColorIndex = 6
etc对于基本VBA代码,您始终可以开始录制宏,执行操作,停止录制,查看生成的代码,然后清理这些代码以执行所需操作。例如,记录高亮显示一行的操作(设置Interior.Color的值)将为您提供:
Rows("13:13").Select
Range("C13").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
可以删除选择命令和无关的内部特性,以便:
Rows("13:13").Interior.Color = 65535
在行中添加“多选”:
Rows("6:6,10:10,150:150,201:201").Interior.Color = 65535
总结:
- 记录宏
- 查看Excel的版本
- 使用/编辑您需要的代码
对于公式,输入:
=或(ROW()=6,ROW()=10,ROW()=150,ROW()=201)
这里是另一个基于Mote的.EntireRow.Interior.ColorIndex
这并不限制您输入行号,而是为用户提供了在运行时选择行的灵活性
Option Explicit
Sub Sample()
Dim Ret As Range
On Error Resume Next
Set Ret = Application.InputBox("Please select the rows that you would like to color", "Color Rows", Type:=8)
On Error GoTo 0
If Not Ret Is Nothing Then Ret.EntireRow.Interior.ColorIndex = 6
End Sub
跟进
是否有方法编写宏以从列表中读取行号并高亮显示行
是的,有办法。假设单元格A1到A10中的列表,那么您可以使用此代码
Option Explicit
Sub Sample()
Dim i As Long, sh As Worksheet
On Error GoTo Whoa
Application.ScreenUpdating = False
'~~> Set this to the sheet where the rows need to be colored
Set sh = Sheets("Sheet2")
'~~> Change Sheet1 to the sheet which has the list
With Sheets("Sheet1")
For i = 1 To 10
If Not Len(Trim(.Range("A" & i).Value)) = 0 And _
IsNumeric(.Range("A" & i).Value) Then _
sh.Rows(.Range("A" & i).Value).Interior.ColorIndex = 3 '<~~ Red
Next i
End With
LetsContinue:
Application.ScreenUpdating = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
选项显式
子样本()
将i与工作表一样长,sh与工作表一样长
关于错误转到哇
Application.ScreenUpdating=False
“~~>将此设置为需要为行着色的工作表
设置sh=板材(“板材2”)
“~~>将Sheet1更改为包含列表的工作表
附页(“第1页”)
对于i=1到10
如果不是Len(修剪(.Range(“A”&i).Value))=0和_
IsNumeric(.Range(“A”&i).Value)则_
sh.Rows(.Range(“A”&i).Value).Interior.ColorIndex=3'更新:没有意识到这一点上的日期,但认为我应该添加它,因为它与所选答案相关
除了Siddharth Rout的回答之外,因为我还没有足够的代表发表评论,所以您可以用这两行动态计算工作表中有多少行xlCellTypeConstants
可以更改为您需要的另一个XlCellType常量,并且可以随时更改范围以适应您的电子表格
Dim numRows As Integer
numRows = Range("A2", Range("A1048576").End(xlUp)).SpecialCells(xlCellTypeConstants).Cells.Count
抱歉,如果它没有其他答案那么简洁或优雅,但它完成了任务。当我为自己的应用程序编写代码时,我需要遍历代码。另外,我只需要高亮显示部分行,而不是高亮显示整行
Sub Highlight()
Dim ThisWB As Workbook
Dim ThisWS As Worksheet
Dim rows(0 To 3) As Integer
Dim test As String
Set ThisWB = ActiveWorkbook
Set ThisWS = ThisWB.Sheets("Sheet1")
rows(0) = 6
rows(1) = 10
rows(2) = 150
rows(3) = 201
For i = 0 To 3
test = "A" & rows(i) & ":H" & rows(i)
ThisWS.Range(test).Interior.ColorIndex = 15
Next i
End Sub
使用条件格式也可以实现同样的效果
- 将值列表放在一列中(我使用一个单独的选项卡并为列表命名)
- 在条件格式-新规则下-“使用公式确定要格式化的单元格”
- 阅读这篇文章
- 规则在公式中使用vlookup-=$A2=vlookup($A2,list,1,FALSE)
+1详细回答:)您好,只是一个简短的澄清,我应该在之前包括它。电子表格有几千条记录(行)。是否有方法编写宏以从列表中读取行号并高亮显示行?这是我看的一份日报。我想尽可能地自动化。然后我可以按颜色过滤,只看到我需要的。是的,你可以做到。更新帖子。