How can I tell Excel to highlight rows by their row number. For instance, let’s say I wanted row 6, 10, 150, 201 highlighted. Thanks.
asked Jun 6, 2012 at 22:12
Here is another one based on Mote’s .EntireRow.Interior.ColorIndex
This one doesn’t restrict you to enter the row numbers but gives the user the flexibility to choose the rows at runtime.
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
FOLLOWUP
Is there a way to write the macro to read the row numbers from a list and highlight the rows?
Yes there is a way. Let’s say the list in Cell A1 to A10 then you can use this code
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
Reafidy
8,2505 gold badges49 silver badges81 bronze badges
answered Jun 6, 2012 at 23:25
Siddharth RoutSiddharth Rout
146k17 gold badges206 silver badges250 bronze badges
2
As an alternative to Motes’ answer, you can use conditional formatting.
Eg: select A1:J500, Conditional formatting >> New rule >> Use a formula…
For the formula enter: =OR(ROW()=6, ROW()=10, ROW()=150, ROW()=201)
answered Jun 6, 2012 at 22:38
Tim WilliamsTim Williams
150k8 gold badges96 silver badges124 bronze badges
0
For basic VBA code, you can always start recording a macro, perform the action, stop recording, look at what code was generated, and then clean that up to do what you want. For example, recording the action of highlighting a row (setting the value of Interior.Color) gives you:
Rows("13:13").Select
Range("C13").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
The selection commands and extraneous Interior properties can be removed giving you:
Rows("13:13").Interior.Color = 65535
Adding in the row multi-select:
Rows("6:6,10:10,150:150,201:201").Interior.Color = 65535
Summary:
- Record macro
- View Excel’s version
- Use/Edit what code you need
answered Jun 6, 2012 at 22:36
Steve KonvesSteve Konves
2,6283 gold badges24 silver badges44 bronze badges
1
objWB.Cells(rowNum,201).EntireRow.Interior.ColorIndex = 6
etc
answered Jun 6, 2012 at 22:16
MotomotesMotomotes
3,8941 gold badge25 silver badges24 bronze badges
1
Update: Didn’t realize the date on this, but thought I’d add this in since it was relevant to the chosen answer.
In addition to Siddharth Rout’s answer, since I don’t have enough rep to comment yet, you can dynamically figure out how many rows there are in your worksheet with these two lines. xlCellTypeConstants
could be changed to another XlCellType constant that you need, and the range can always be changed to accommodate to your spreadsheet.
Dim numRows As Integer
numRows = Range("A2", Range("A1048576").End(xlUp)).SpecialCells(xlCellTypeConstants).Cells.Count
answered Mar 17, 2015 at 19:56
KelsiusKelsius
4132 gold badges5 silver badges19 bronze badges
Sorry if it is not as concise or elegant as other answers, but it gets the job done. When I was writing code for my own application I needed to loop through my code. Also, instead of highlighting the entire row, I only needed to highlight a portion of the rows.
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
answered Jun 28, 2016 at 19:45
0 / 0 / 0 Регистрация: 23.06.2008 Сообщений: 36 |
|
1 |
|
25.06.2008, 02:16. Показов 26321. Ответов 2
как выделить всю текущую строку? Excel. если извесно, что в строке 5 ячеек? спасибо
0 |
Programming Эксперт 94731 / 64177 / 26122 Регистрация: 12.04.2006 Сообщений: 116,782 |
25.06.2008, 02:16 |
2 |
Victory |
|
25.06.2008, 12:30 |
2 |
Если хочешь выделить всю строку пиши Rows(Activecell.row).select. |
TurboDuck 50 / 49 / 13 Регистрация: 23.11.2015 Сообщений: 401 |
||||
20.07.2016, 07:30 |
3 |
|||
0 |
0 / 0 / 0 Регистрация: 05.06.2012 Сообщений: 37 |
|
1 |
|
26.06.2012, 14:07. Показов 23097. Ответов 2
Вроде-бы вопрос элементарный, но нигде не могу найти. Как выделить (выбрать)
0 |
22 / 5 / 1 Регистрация: 05.09.2010 Сообщений: 370 |
|
26.06.2012, 14:40 |
2 |
Range(Cells(r — 6, 1), Cells(r — 1, 1)).EntireRow.Select
0 |
0 / 0 / 0 Регистрация: 05.06.2012 Сообщений: 37 |
|
26.06.2012, 15:48 [ТС] |
3 |
Огромное спасибо!
0 |
title | ms.prod | ms.assetid | ms.date |
---|---|---|---|
Highlight the Active Cell, Row, or Column |
excel |
51a30ffb-77f2-4bd7-8eb6-b6781dc55d43 |
06/08/2017 |
Highlight the Active Cell, Row, or Column
The following code examples show ways to highlight the active cell or the rows and columns that contain the active cell. These examples use the SelectionChange event of the Worksheet object.
Sample code provided by: Tom Urtis, Atlas Programming Management
Highlighting the Active Cell
The following code example clears the color in all the cells on the worksheet by setting the ColorIndex property equal to 0, and then highlights the active cell by setting the ColorIndex property equal to 8 (Turquoise).
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Application.ScreenUpdating = False ' Clear the color of all the cells Cells.Interior.ColorIndex = 0 ' Highlight the active cell Target.Interior.ColorIndex = 8 Application.ScreenUpdating = True End Sub
Highlighting the Entire Row and Column that Contain the Active Cell
The following code example clears the color in all the cells on the worksheet by setting the ColorIndex property equal to 0, and then highlights the entire row and column that contain the active cell by using the EntireRow and EntireColumn properties.
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub Application.ScreenUpdating = False ' Clear the color of all the cells Cells.Interior.ColorIndex = 0 With Target ' Highlight the entire row and column that contain the active cell .EntireRow.Interior.ColorIndex = 8 .EntireColumn.Interior.ColorIndex = 8 End With Application.ScreenUpdating = True End Sub
Highlighting the Row and Column that Contain the Active Cell, Within the Current Region
The following code example clears the color in all the cells on the worksheet by setting the ColorIndex property equal to 0, and then highlights the row and column that contain the active cell, within the current region by using the CurrentRegion property of the Range object.
Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' Clear the color of all the cells Cells.Interior.ColorIndex = 0 If IsEmpty(Target) Or Target.Cells.Count > 1 Then Exit Sub Application.ScreenUpdating = False With ActiveCell ' Highlight the row and column that contain the active cell, within the current region Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 8 Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count + .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 8 End With Application.ScreenUpdating = True End Sub
About the Contributor
MVP Tom Urtis is the founder of Atlas Programming Management, a full-service Microsoft Office and Excel business solutions company in Silicon Valley. Tom has over 25 years of experience in business management and developing Microsoft Office applications, and is the coauthor of «Holy Macro! It’s 2,500 Excel VBA Examples.»
Download Article
Download Article
This wikiHow teaches you how to start using Visual Basic procedures to select data in Microsoft Excel. As long as you’re familiar with basic VB scripting and using more advanced features of Excel, you’ll find the selection process pretty straight-forward.
-
1
Select one cell on the current worksheet. Let’s say you want to select cell E6 with Visual Basic. You can do this with either of the following options:[1]
ActiveSheet.Cells(6, 5).Select
ActiveSheet.Range("E6").Select
-
2
Select one cell on a different worksheet in the same workbook. Let’s say our example cell, E6, is on a sheet called Sheet2. You can use either of the following options to select it:
Application.Goto ActiveWorkbook.Sheets("Sheet2").Cells(6, 5)
Application.Goto (ActiveWorkbook.Sheets("Sheet2").Range("E6"))
Advertisement
-
3
Select one cell on a worksheet in a different workbook. Let’s say you want to select a cell from Sheet1 in a workbook called BOOK2.XLS. Either of these two options should do the trick:
Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Cells(2,1)
Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("A2")
-
4
Select a cell relative to another cell. You can use VB to select a cell based on its location relative to the active (or a different) cell. Just be sure the cell exists to avoid errors. Here’s how to use :
-
Select the cell three rows below and four columns to the left of the active cell:
ActiveCell.Offset(3, -4).Select
-
Select the cell five rows below and four columns to the right of cell C7:
ActiveSheet.Cells(7, 3).Offset(5, 4).Select
-
Select the cell three rows below and four columns to the left of the active cell:
Advertisement
-
1
Select a range of cells on the active worksheet. If you wanted to select cells C1:D6 on the current sheet, you can enter any of the following three examples:
ActiveSheet.Range(Cells(1, 3), Cells(6, 4)).Select
ActiveSheet.Range("C1:D6").Select
ActiveSheet.Range("C1", "D6").Select
-
2
Select a range from another worksheet in the same workbook. You could use either of these examples to select cells C3:E11 on a sheet called Sheet3:
Application.Goto ActiveWorkbook.Sheets("Sheet3").Range("C3:E11")
Application.Goto ActiveWorkbook.Sheets("Sheet3").Range("C3", "E11")
-
3
Select a range of cells from a worksheet in a different workbook. Both of these examples would select cells E12:F12 on Sheet1 of a workbook called BOOK2.XLS:
Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("E12:F12")
Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("E12", "F12")
-
4
Select a named range. If you’ve assigned a name to a range of cells, you’d use the same syntax as steps 4-6, but you’d replace the range address (e.g., «E12», «F12») with the range’s name (e.g., «Sales»). Here are some examples:
-
On the active sheet:
ActiveSheet.Range("Sales").Select
-
Different sheet of same workbook:
Application.Goto ActiveWorkbook.Sheets("Sheet3").Range("Sales")
-
Different workbook:
Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("Sales")
-
On the active sheet:
-
5
Select a range relative to a named range. The syntax varies depending on the named range’s location and whether you want to adjust the size of the new range.
- If the range you want to select is the same size as one called Test5 but is shifted four rows down and three columns to the right, you’d use:
ActiveSheet.Range("Test5").Offset(4, 3).Select
- If the range is on Sheet3 of the same workbook, activate that worksheet first, and then select the range like this:
Sheets("Sheet3").Activate ActiveSheet.Range("Test").Offset(4, 3).Select
- If the range you want to select is the same size as one called Test5 but is shifted four rows down and three columns to the right, you’d use:
-
6
Select a range and resize the selection. You can increase the size of a selected range if you need to. If you wanted to select a range called Database’ and then increase its size by 5 rows, you’d use this syntax:
Range("Database").Select Selection.Resize(Selection.Rows.Count + 5, _Selection.Columns.Count).Select
-
7
Select the union of two named ranges. If you have two overlapping named ranges, you can use VB to select the cells in that overlapping area (called the «union»). The limitation is that you can only do this on the active sheet. Let’s say you want to select the union of a range called Great and one called Terrible:
-
Application.Union(Range("Great"), Range("Terrible")).Select
- If you want to select the intersection of two named ranges instead of the overlapping area, just replace Application.Union with Application.Intersect.
-
Advertisement
-
1
Use this example data for the examples in this method. This chart full of example data, courtesy of Microsoft, will help you visualize how the examples behave:[2]
A1: Name B1: Sales C1: Quantity A2: a B2: $10 C2: 5 A3: b B3: C3: 10 A4: c B4: $10 C4: 5 A5: B5: C5: A6: Total B6: $20 C6: 20 -
2
Select the last cell at the bottom of a contiguous column. The following example will select cell A4:
ActiveSheet.Range("A1").End(xlDown).Select
-
3
Select the first blank cell below a column of contiguous cells. The following example will select A5 based on the chart above:
ActiveSheet.Range("A1").End(xlDown).Offset(1,0).Select
-
4
Select a range of continuous cells in a column. Both of the following examples will select the range A1:A4:
ActiveSheet.Range("A1", ActiveSheet.Range("a1").End(xlDown)).Select
ActiveSheet.Range("A1:" & ActiveSheet.Range("A1"). End(xlDown).Address).Select
-
5
Select a whole range of non-contiguous cells in a column. Using the data table at the top of this method, both of the following examples will select A1:A6:
ActiveSheet.Range("A1",ActiveSheet.Range("A65536").End(xlUp)).Select
ActiveSheet.Range("A1",ActiveSheet.Range("A65536").End(xlUp)).Select
Advertisement
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Video
-
The «ActiveSheet» and «ActiveWorkbook» properties can usually be omitted if the active sheet and/or workbook(s) are implied.
Thanks for submitting a tip for review!
Advertisement
About This Article
Article SummaryX
1. Use ActiveSheet.Range(«E6»).Select to select E6 on the active sheet.
2. Use Application.Goto (ActiveWorkbook.Sheets(«Sheet2»).Range(«E6»)) to select E6 on Sheet2.
3. Add Workbooks(«BOOK2.XLS») to the last step to specify that the sheet is in BOOK2.XLS.
Did this summary help you?
Thanks to all authors for creating a page that has been read 167,768 times.