0 / 0 / 0 Регистрация: 23.06.2008 Сообщений: 36 |
|
1 |
|
25.06.2008, 02:16. Показов 26332. Ответов 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 |
! Знаю ячейку, а как выделить всю строку?
- GAL
- Обычный пользователь
-
- Сообщения: 69
- Зарегистрирован: 05.11.2004 (Пт) 15:57
! Знаю ячейку, а как выделить всю строку?
Привет!
Люди, подскажите, пожалуйста..
Как выделить строку если я знаю ячейку в этой строке?
Заранее большое спасибо.
- uhm
- Продвинутый гуру
-
- Сообщения: 1597
- Зарегистрирован: 02.12.2004 (Чт) 15:21
uhm » 06.04.2005 (Ср) 10:38
Rows(номер_строки).Select
Если в строке есть ячейки, объединенные с другими строками, выделится несколько строк.
- GAL
- Обычный пользователь
-
- Сообщения: 69
- Зарегистрирован: 05.11.2004 (Пт) 15:57
GAL » 06.04.2005 (Ср) 10:49
Я не знаю номер строки.
Я немогу явно указать номер этой строки.
У меня в макросе запускается поиск, нахожу ячейку с таким-то содержимым и мне нужно выделить строку содержащую эту ячейку.
Есть такая возможность?
- GAL
- Обычный пользователь
-
- Сообщения: 69
- Зарегистрирован: 05.11.2004 (Пт) 15:57
GAL » 06.04.2005 (Ср) 10:52
Я незнаю номер строки.
Я немогу указать его явно.
У меня макрос ищет ячейку с таким-то содержимым и когда найдет мне нужно выделить строку содержащую эту ячейку.
Есть такая возможность?
- uhm
- Продвинутый гуру
-
- Сообщения: 1597
- Зарегистрирован: 02.12.2004 (Чт) 15:21
uhm » 06.04.2005 (Ср) 10:53
Да, если x — это твоя найденная ячейка, то она содержится в столбце x.Column и в строке x.Row. Соответственно, можешь выделить строку так:
Rows(x.Row).Select
- GAL
- Обычный пользователь
-
- Сообщения: 69
- Зарегистрирован: 05.11.2004 (Пт) 15:57
GAL » 06.04.2005 (Ср) 11:03
Да, так получилось.
Спасибо uhm!
- GSerg
- Шаман
- Сообщения: 14286
- Зарегистрирован: 14.12.2002 (Сб) 5:25
- Откуда: Магадан
GSerg » 06.04.2005 (Ср) 12:51
x.entirerow.select
Как только вы переберёте все варианты решения и не найдёте нужного, тут же обнаружится решение, простое и очевидное для всех, кроме вас
Вернуться в VBA
Кто сейчас на конференции
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 2
Выделения относительно активной ячейки
Выделить вниз до первой или последней заполненной ячейки (равносильно нажатию Ctrl+Shift+Down)
Sub CtrlShiftDown()
Range(ActiveCell, ActiveCell.End(xlDown)).Select
End Sub
Выделить вверх до первой или последней заполненной ячейки (равносильно нажатию Ctrl+Shift+Up)
Sub CtrlShiftUp()
Range(ActiveCell, ActiveCell.End(xlUp)).Select
End Sub
Выделить вправо до первой или последней заполненной ячейки (равносильно нажатию Ctrl+Shift+Right)
Sub CtrlShiftRight()
Range(ActiveCell, ActiveCell.End(xlToRight)).Select
End Sub
Выделить влево до первой или последней заполненной ячейки (равносильно нажатию Ctrl+Shift+Left)
Sub CtrlShiftLeft()
Range(ActiveCell, ActiveCell.End(xlToLeft)).Select
End Sub
Выделить текущую область (выделяется диапазон неразрывно заполненных ячеек — равносильно нажатию кнопок Ctrl+Shift+*)
Sub CtrlShiftUmn()
ActiveCell.CurrentRegion.Select
End Sub
Выделить активную область (происходит выделение всего заполненного диапазона Ctrl+Shift+Home, End, Home)
Sub CtrlShiftHome()
Range(Range("A1"), ActiveCell.SpecialCells(xlLastCell)).Select
End Sub
Выделить смежные (заполненные прилегающие к активной ячейке) ячейки в столбце с активной ячейкой
Sub SelectActiveColumn()
Dim TopCell As Range
Dim BottomCell As Range
If IsEmpty(ActiveCell) Then Exit Sub
On Error Resume Next
If IsEmpty(ActiveCell.Offset(-1, 0)) Then Set TopCell = _
ActiveCell Else Set TopCell = ActiveCell.End(xlUp)
If IsEmpty(ActiveCell.Offset(1, 0)) Then Set BottomCell = _
ActiveCell Else Set BottomCell = ActiveCell.End(xlDown)
Range(TopCell, BottomCell).Select
End Sub
Выделить смежные ячейки в строке с активной ячейкой
Sub SelectActiveRow()
Dim LeftCell As Range
Dim RightCell As Range
If IsEmpty(ActiveCell) Then Exit Sub
On Error Resume Next
If IsEmpty(ActiveCell.Offset(0, -1)) Then Set LeftCell = _
ActiveCell Else Set LeftCell = ActiveCell.End(xlToLeft)
If IsEmpty(ActiveCell.Offset(0, 1)) Then Set RightCell = _
ActiveCell Else Set RightCell = ActiveCell.End(xlToRight)
Range(LeftCell, RightCell).Select
End Sub
Выделить весь активный столбец
Sub SelectionEntireColumn()
Selection.EntireColumn.Select
End Sub
Выделить всю активную строку
Sub SelectEntireRow()
Selection.EntireRow.Select
End Sub
Выделить рабочий лист
Sub SelectEntireSheet()
Cells.Select
End Sub
Выделить следующую пустую ячейку снизу
Sub CellNextDown()
ActiveCell.Offset(1, 0).Select
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Выделить следующую пустую ячейку справа
Sub CellNextRight()
ActiveCell.Offset(0, 1).Select
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1).Select
Loop
End Sub
Выделение от первой непустой до последней непустой ячеек в строке
Sub SelectFirstToLastInRow()
Dim LeftCell As Range
Dim RightCell As Range
Set LeftCell = Cells(ActiveCell.Row, 1)
Set RightCell = Cells(ActiveCell.Row, 256)
If IsEmpty(LeftCell) Then Set LeftCell = LeftCell.End(xlToRight)
If IsEmpty(RightCell) Then Set RightCell = RightCell.End(xlToLeft)
If LeftCell.Column = 256 And RightCell.Column = 1 Then ActiveCell. _
Select Else Range(LeftCell, RightCell).Select
End Sub
Выделение от первой непустой до последней непустой ячеек в столбце
Sub SelectFirstToLastInColumn()
Dim TopCell As Range
Dim BottomCell As Range
Set TopCell = Cells(1, ActiveCell.Column)
Set BottomCell = Cells(16384, ActiveCell.Column)
If IsEmpty(TopCell) Then Set TopCell = TopCell.End(xlDown)
If IsEmpty(BottomCell) Then Set BottomCell = BottomCell.End(xlUp)
If TopCell.Row = 16384 And BottomCell.Row = 1 Then ActiveCell. _
Select Else Range(TopCell, BottomCell).Select
End Sub
You can use the following methods in VBA to highlight rows:
Method 1: Highlight Active Row
Sub HighlightActiveRow()
ActiveCell.EntireRow.Interior.Color = vbYellow
End Sub
This particular macro will highlight the currently active row.
Method 2: Highlight Specific Row
Sub HighlightSpecificRow()
Rows("4:4").Interior.Color = vbYellow
End Sub
This particular macro will highlight row 4 in the current sheet.
Method 3: Highlight Several Specific Rows
Sub HighlightSpecificRows()
Range("2:2,4:4,6:6,8:8").Interior.Color = vbYellow
End Sub
This particular macro will highlight rows 2, 4, 6, and 8 in the current sheet.
Note: To highlight all rows between 2 and 8, you can type Range(“2:8”) instead.
The following examples show how to use each method in practice.
Example 1: Highlight Active Row
Suppose we currently have cell B3 selected.
We can create the following macro to highlight each cell in the currently active row
Sub HighlightActiveRow()
ActiveCell.EntireRow.Interior.Color = vbYellow
End Sub
When we run this macro, we receive the following output:
Notice that each cell in row three is highlighted and all other rows are simply left untouched.
Example 2: Highlight Specific Row
Suppose we would like to highlight row four only.
We can create the following macro to do so:
Sub HighlightSpecificRow()
Rows("4:4").Interior.Color = vbYellow
End Sub
When we run this macro, we receive the following output:
Notice that each cell in row four is highlighted and all other rows are simply left untouched.
Example 3: Highlight Several Specific Rows
Suppose we would like to highlight rows 2, 4, 6, and 8 in the current sheet.
We can create the following macro to do so:
Sub HighlightSpecificRows()
Range("2:2,4:4,6:6,8:8").Interior.Color = vbYellow
End Sub
When we run this macro, we receive the following output:
Notice that rows 2, 4, 6, and 8 are all highlighted and all other rows are left untouched.
Note: In each example we chose to use yellow (vbYellow) as the highlight color, but you can choose a different color such as vbRed, vbGreen, vbBlue, etc.
Additional Resources
The following tutorials explain how to perform other common tasks in VBA:
VBA: How to Highlight Cells
VBA: How to Apply Conditional Formatting to Cells
VBA: How to Apply Conditional Formatting to Duplicate Values
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.»