0 / 0 / 0 Регистрация: 27.04.2009 Сообщений: 265 |
|
1 |
|
Как узнать номер строки для активной ячейки?22.09.2009, 09:21. Показов 63222. Ответов 8
Как узнать номер строки для активной ячейки?
0 |
Savelev |
|
22.09.2009, 10:51 |
2 |
ActiveCell.Row |
0 / 0 / 0 Регистрация: 27.04.2009 Сообщений: 265 |
|
22.09.2009, 12:08 [ТС] |
3 |
спасибо
0 |
0 / 0 / 0 Регистрация: 04.06.2017 Сообщений: 3 |
|
15.01.2018, 13:33 |
4 |
а для умной таблицы, можно так сделать?
0 |
6875 / 2807 / 533 Регистрация: 19.10.2012 Сообщений: 8,562 |
|
15.01.2018, 14:17 |
5 |
Можно.
1 |
0 / 0 / 0 Регистрация: 04.06.2017 Сообщений: 3 |
|
17.01.2018, 14:56 |
6 |
А как, не подскажете? Будьте добры, если не сложно))
0 |
6875 / 2807 / 533 Регистрация: 19.10.2012 Сообщений: 8,562 |
|
17.01.2018, 15:24 |
7 |
Что именно нужно «а для умной таблицы, можно так сделать» ?
0 |
Казанский 15136 / 6410 / 1730 Регистрация: 24.09.2011 Сообщений: 9,999 |
||||
17.01.2018, 15:53 |
8 |
|||
Stav-ortodox, номер строки в диапазоне данных умной таблицы?
0 |
0 / 0 / 0 Регистрация: 04.06.2017 Сообщений: 3 |
|
17.01.2018, 16:30 |
9 |
Вот, то что нужно! Спаси Господи!!
0 |
Всем добрый день! Ситуация: пользователь выделил диапазон ячеек (например R5C1:R15C1 или R10C2:R20C5) Буду оч благодарен за подсказку, какая конструкция в VBA может определить номер самой верхней и самой нижней строки выделенного диапазона? (т.е. нужно получить 5 и 15 (10 и 20) для первого (второго) случая. |
|
ran Пользователь Сообщений: 7091 |
Sub klb() |
Alex_ST Пользователь Сообщений: 2746 На лицо ужасный, добрый внутри |
Sub Êëóáîâ() С уважением, Алексей (ИМХО: Excel-2003 — THE BEST!!!) |
Alex_ST Пользователь Сообщений: 2746 На лицо ужасный, добрый внутри |
долбаная Виста… Sub Клубов() С уважением, Алексей (ИМХО: Excel-2003 — THE BEST!!!) |
Спасибо. ‘ запрос диапазона End Sub мне нужно чтобы макрос определил первую и последнюю строку диапазона.которую выделил пользователь. |
|
Юрий М Модератор Сообщений: 60581 Контакты см. в профиле |
И чем Вас не устроили предыдущие ответы? |
Не работает. |
|
ran Пользователь Сообщений: 7091 |
А Selection на DLRange заменить слабо? |
Юрий М Модератор Сообщений: 60581 Контакты см. в профиле |
Возьмите две строки, например, RAN и замените Selection на Ваш диапазон: ‘ запрос диапазона |
СПАСИБО!!!! |
|
Alex_ST Пользователь Сообщений: 2746 На лицо ужасный, добрый внутри |
Только не применяйте на листе условное форматирование с формулами условий, а то на таких листах Application.InputBox(…, Type:=8) глючит. Да и при указании диапазона на другой странице — тоже бывает. http://www.planetaexcel.ru/forum.php?thread_id=26054 ) была предложена его не глючная альтернатива. Я уже почти все свои макросы под неё переделал. С уважением, Алексей (ИМХО: Excel-2003 — THE BEST!!!) |
Юрий М Модератор Сообщений: 60581 Контакты см. в профиле |
Алекс, пытаюсь спровоцировать ошибку (при УФ) — не получается |
Alex_ST Пользователь Сообщений: 2746 На лицо ужасный, добрый внутри |
Юрий, я сейчас смог повторить результат (просто нашел свой топик http://www.planetaexcel.ru/forum.php?thread_id=15119 — «Проблемы с Application.InputBox (….., Type:=8) — не всякие значения хочет принимать» ) и подчистил по максимуму пример от всяких лишних кодов. С уважением, Алексей (ИМХО: Excel-2003 — THE BEST!!!) |
Alex_ST Пользователь Сообщений: 2746 На лицо ужасный, добрый внутри |
#14 01.04.2011 13:09:37 Юрий, чтобы не оффтопить здесь, я поднял свой старый топик «Проблемы с Application.InputBox (….., Type:=8) — не всякие значения хочет» С уважением, Алексей (ИМХО: Excel-2003 — THE BEST!!!) |
Определение адреса выделенного диапазона ячеек на листе Excel с помощью кода VBA. Определение номера первой и последней строки. Программное выделение диапазона.
Адрес выделенного диапазона
Для определения адреса выделенного диапазона ячеек в VBA Excel используется свойство Address объекта Selection.
Объект Selection — это совокупность всех выделенных ячеек на листе Excel. Это может быть одна ячейка, смежный или несмежный диапазон ячеек, представляющий коллекцию смежных диапазонов. Если выделение состоит из несмежного диапазона, адреса смежных диапазонов, из которых он состоит, будут перечислены через запятую.
Смежный диапазон — прямоугольная область смежных (прилегающих друг к другу) ячеек.
Несмежный диапазон — совокупность (коллекция) смежных диапазонов (прямоугольных областей смежных ячеек).
Стоит отметить: несмотря на то, что в выделенном диапазоне может содержаться много ячеек, активной может быть только одна. Она представлена объектом ActiveCell. Для определения ее адреса в коде VBA Excel также используется свойство Address.
Sub Primer1() MsgBox «Адрес выделенного диапазона: « & Selection.Address & _ vbNewLine & «Адрес активной ячейки: « & ActiveCell.Address & _ vbNewLine & «Номер строки активной ячейки: « & ActiveCell.Row & _ vbNewLine & «Номер столбца активной ячейки: « & ActiveCell.Column End Sub |
Скопируйте и запустите код на выполнение. В результате получите что-то вроде этого, зависящее от того, какие диапазоны вы выберите:
Определение адресов выделенного диапазона и активной ячейки
Выделение ячеек и диапазонов
Выделить несмежный диапазон ячеек можно следующим образом:
Sub Primer2() Range(«B4:C7,E5:F7,D8»).Select End Sub |
Как видно из примера, в адресной строке объекта Range перечисляются адреса смежных диапазонов, составляющих общий несмежный диапазон, через запятую. Выделение осуществляется методом Select объекта Range.
Определение номеров первой и последней строки
Чтобы вычислить номера первой и последней строки выделенного диапазона, будем исходить из того, что первая ячейка смежного диапазона находится на первой строке, а последняя — на последней строке выделенного диапазона.
Sub Primer3() Dim i1 As Long, i2 As Long i1 = Selection.Cells(1).Row i2 = Selection.Cells(Selection.Cells.Count).Row MsgBox «Первая строка: « & i1 & _ vbNewLine & «Последняя строка: « & i2 End Sub |
Результат будет таким, зависит от выделенного диапазона:
Номера первой и последней строки выделенного смежного диапазона
Таким же образом можно вычислить номера первого и последнего столбцов выделенного диапазона, которые можно использовать для обработки информации по столбцам.
Обратите внимание, что для несмежных диапазонов этот пример не работает.
На практике я использовал определение номеров первой и последней строк по выделенному диапазону для формирования файла загрузки данных держателей дисконтных карт на сервис отправки СМС-сообщений. Оказалось, что базу данных клиентов заполнять в таблице Excel намного удобнее, чем на портале сервиса, а для загрузки в сервис достаточно сформировать несложный файл. Заполнил новые строки, выделил их по любому столбцу, нажал кнопку и файл готов.
Here is my code to add line numbers in the VBE IDE. It is an improvement of the solution provided here by Excel MVP mikerickson. I have worked on this, because in some rare cases I have already met, VBE can’t enter in debug mode, for example when you have a .ReplaceLine method in your code. Indeed, you can’t enter in debug mode once it has been executed, so Erl might be usefully for debug (instead of Debug.Print). I have added several feature such as:
- possibility to either add line numbers as labels:
10: Dim foo as bar
or as single numbers seperated from code by a tab:10 Dim foo as bar
- possibility to add line numbers to End of procedures statements, and to match the indent of the procedure declaration lines to its End statement line once numberered. Or not.
- possibility of add line numbers to empty lines or not
- [WIP] possibility to add line numbers to a specific procedure in a module
- [WIP] match all indentations of code lines with line numbers to match the indent of the last line indented. If last line is
200: End Sub
, the line30: With ActiveSheet
will be re-indented as30: ActiveSheet
- [WIP] add of a VBE IDE command to directly make the calls with the current module/proc as a parameter
Public Enum vbLineNumbers_LabelTypes
vbLabelColon ' 0
vbLabelTab ' 1
End Enum
Public Enum vbLineNumbers_ScopeToAddLineNumbersTo
vbScopeAllProc ' 1
vbScopeThisProc ' 2
End Enum
Sub AddLineNumbers(ByVal wbName As String, _
ByVal vbCompName As String, _
ByVal LabelType As vbLineNumbers_LabelTypes, _
ByVal AddLineNumbersToEmptyLines As Boolean, _
ByVal AddLineNumbersToEndOfProc As Boolean, _
ByVal Scope As vbLineNumbers_ScopeToAddLineNumbersTo, _
Optional ByVal thisProcName As String)
' USAGE RULES
' DO NOT MIX LABEL TYPES FOR LINE NUMBERS! IF ADDING LINE NUMBERS AS COLON TYPE, ANY LINE NUMBERS AS VBTAB TYPE MUST BE REMOVE BEFORE, AND RECIPROCALLY ADDING LINE NUMBERS AS VBTAB TYPE
Dim i As Long
Dim j As Long
Dim procName As String
Dim startOfProcedure As Long
Dim lengthOfProcedure As Long
Dim endOfProcedure As Long
Dim strLine As String
With Workbooks(wbName).VBProject.VBComponents(vbCompName).CodeModule
.CodePane.Window.Visible = False
If Scope = vbScopeAllProc Then
For i = 1 To .CountOfLines
strLine = .Lines(i, 1)
procName = .ProcOfLine(i, vbext_pk_Proc) ' Type d'argument ByRef incompatible ~~> Requires VBIDE library as a Reference for the VBA Project
If procName <> vbNullString Then
startOfProcedure = .ProcStartLine(procName, vbext_pk_Proc)
bodyOfProcedure = .ProcBodyLine(procName, vbext_pk_Proc)
countOfProcedure = .ProcCountLines(procName, vbext_pk_Proc)
prelinesOfProcedure = bodyOfProcedure - startOfProcedure
'postlineOfProcedure = ??? not directly available since endOfProcedure is itself not directly available.
lengthOfProcedure = countOfProcedure - prelinesOfProcedure ' includes postlinesOfProcedure !
'endOfProcedure = ??? not directly available, each line of the proc must be tested until the End statement is reached. See below.
If endOfProcedure <> 0 And startOfProcedure < endOfProcedure And i > endOfProcedure Then
GoTo NextLine
End If
If i = bodyOfProcedure Then InProcBodyLines = True
If bodyOfProcedure < i And i < startOfProcedure + countOfProcedure Then
If Not (.Lines(i - 1, 1) Like "* _") Then
InProcBodyLines = False
PreviousIndentAdded = 0
If Trim(strLine) = "" And Not AddLineNumbersToEmptyLines Then GoTo NextLine
If IsProcEndLine(wbName, vbCompName, i) Then
endOfProcedure = i
If AddLineNumbersToEndOfProc Then
Call IndentProcBodyLinesAsProcEndLine(wbName, vbCompName, LabelType, endOfProcedure)
Else
GoTo NextLine
End If
End If
If LabelType = vbLabelColon Then
If HasLabel(strLine, vbLabelColon) Then strLine = RemoveOneLineNumber(.Lines(i, 1), vbLabelColon)
If Not HasLabel(strLine, vbLabelColon) Then
temp_strLine = strLine
.ReplaceLine i, CStr(i) & ":" & strLine
new_strLine = .Lines(i, 1)
If Len(new_strLine) = Len(CStr(i) & ":" & temp_strLine) Then
PreviousIndentAdded = Len(CStr(i) & ":")
Else
PreviousIndentAdded = Len(CStr(i) & ": ")
End If
End If
ElseIf LabelType = vbLabelTab Then
If Not HasLabel(strLine, vbLabelTab) Then strLine = RemoveOneLineNumber(.Lines(i, 1), vbLabelTab)
If Not HasLabel(strLine, vbLabelColon) Then
temp_strLine = strLine
.ReplaceLine i, CStr(i) & vbTab & strLine
PreviousIndentAdded = Len(strLine) - Len(temp_strLine)
End If
End If
Else
If Not InProcBodyLines Then
If LabelType = vbLabelColon Then
.ReplaceLine i, Space(PreviousIndentAdded) & strLine
ElseIf LabelType = vbLabelTab Then
.ReplaceLine i, Space(4) & strLine
End If
Else
End If
End If
End If
End If
NextLine:
Next i
ElseIf AddLineNumbersToEmptyLines And Scope = vbScopeThisProc Then
End If
.CodePane.Window.Visible = True
End With
End Sub
Function IsProcEndLine(ByVal wbName As String, _
ByVal vbCompName As String, _
ByVal Line As Long) As Boolean
With Workbooks(wbName).VBProject.VBComponents(vbCompName).CodeModule
If Trim(.Lines(Line, 1)) Like "End Sub*" _
Or Trim(.Lines(Line, 1)) Like "End Function*" _
Or Trim(.Lines(Line, 1)) Like "End Property*" _
Then IsProcEndLine = True
End With
End Function
Sub IndentProcBodyLinesAsProcEndLine(ByVal wbName As String, ByVal vbCompName As String, ByVal LabelType As vbLineNumbers_LabelTypes, ByVal ProcEndLine As Long)
Dim procName As String
Dim startOfProcedure As Long
Dim endOfProcedure As Long
With Workbooks(wbName).VBProject.VBComponents(vbCompName).CodeModule
procName = .ProcOfLine(ProcEndLine, vbext_pk_Proc)
bodyOfProcedure = .ProcBodyLine(procName, vbext_pk_Proc)
endOfProcedure = ProcEndLine
strEnd = .Lines(endOfProcedure, 1)
j = bodyOfProcedure
Do Until Not .Lines(j - 1, 1) Like "* _" And j <> bodyOfProcedure
strLine = .Lines(j, 1)
If LabelType = vbLabelColon Then
If Mid(strEnd, Len(CStr(endOfProcedure)) + 1 + 1 + 1, 1) = " " Then
.ReplaceLine j, Space(Len(CStr(endOfProcedure)) + 1) & strLine
Else
.ReplaceLine j, Space(Len(CStr(endOfProcedure)) + 2) & strLine
End If
ElseIf LabelType = vbLabelTab Then
If endOfProcedure < 1000 Then
.ReplaceLine j, Space(4) & strLine
Else
Debug.Print "This tool is limited to 999 lines of code to work properly."
End If
End If
j = j + 1
Loop
End With
End Sub
Sub RemoveLineNumbers(ByVal wbName As String, ByVal vbCompName As String, ByVal LabelType As vbLineNumbers_LabelTypes)
Dim i As Long
With Workbooks(wbName).VBProject.VBComponents(vbCompName).CodeModule
For i = 1 To .CountOfLines
procName = .ProcOfLine(i, vbext_pk_Proc)
If procName <> vbNullString Then
If i = .ProcBodyLine(procName, vbext_pk_Proc) Then InProcBodyLines = True
LenghtBefore = Len(.Lines(i, 1))
If Not .Lines(i - 1, 1) Like "* _" Then
InProcBodyLines = False
.ReplaceLine i, RemoveOneLineNumber(.Lines(i, 1), LabelType)
Else
If IsInProcBodyLines Then
' do nothing
Else
.ReplaceLine i, Mid(.Lines(i, 1), RemovedChars_previous_i + 1)
End If
End If
LenghtAfter = Len(.Lines(i, 1))
LengthBefore_previous_i = LenghtBefore
LenghtAfter_previous_i = LenghtAfter
RemovedChars_previous_i = LengthBefore_previous_i - LenghtAfter_previous_i
If Trim(.Lines(i, 1)) Like "End Sub*" Or Trim(.Lines(i, 1)) Like "End Function" Or Trim(.Lines(i, 1)) Like "End Property" Then
LenOfRemovedLeadingCharacters = LenghtBefore - LenghtAfter
procName = .ProcOfLine(i, vbext_pk_Proc)
bodyOfProcedure = .ProcBodyLine(procName, vbext_pk_Proc)
j = bodyOfProcedure
strLineBodyOfProc = .Lines(bodyOfProcedure, 1)
Do Until Not strLineBodyOfProc Like "* _"
j = j + 1
strLineBodyOfProc = .Lines(j, 1)
Loop
LastLineBodyOfProc = j
strLastLineBodyOfProc = strLineBodyOfProc
strLineEndOfProc = .Lines(i, 1)
For k = bodyOfProcedure To j
.ReplaceLine k, Mid(.Lines(k, 1), 1 + LenOfRemovedLeadingCharacters)
Next k
i = i + (j - bodyOfProcedure)
GoTo NextLine
End If
Else
' GoTo NextLine
End If
NextLine:
Next i
End With
End Sub
Function RemoveOneLineNumber(ByVal aString As String, ByVal LabelType As vbLineNumbers_LabelTypes)
RemoveOneLineNumber = aString
If LabelType = vbLabelColon Then
If aString Like "#:*" Or aString Like "##:*" Or aString Like "###:*" Then
RemoveOneLineNumber = Mid(aString, 1 + InStr(1, aString, ":", vbTextCompare))
If Left(RemoveOneLineNumber, 2) Like " [! ]*" Then RemoveOneLineNumber = Mid(RemoveOneLineNumber, 2)
End If
ElseIf LabelType = vbLabelTab Then
If aString Like "# *" Or aString Like "## *" Or aString Like "### *" Then RemoveOneLineNumber = Mid(aString, 5)
If aString Like "#" Or aString Like "##" Or aString Like "###" Then RemoveOneLineNumber = ""
End If
End Function
Function HasLabel(ByVal aString As String, ByVal LabelType As vbLineNumbers_LabelTypes) As Boolean
If LabelType = vbLabelColon Then HasLabel = InStr(1, aString & ":", ":") < InStr(1, aString & " ", " ")
If LabelType = vbLabelTab Then
HasLabel = Mid(aString, 1, 4) Like "# " Or Mid(aString, 1, 4) Like "## " Or Mid(aString, 1, 4) Like "### "
End If
End Function
Function RemoveLeadingSpaces(ByVal aString As String) As String
Do Until Left(aString, 1) <> " "
aString = Mid(aString, 2)
Loop
RemoveLeadingSpaces = aString
End Function
Function WhatIsLineIndent(ByVal aString As String) As String
i = 1
Do Until Mid(aString, i, 1) <> " "
i = i + 1
Loop
WhatIsLineIndent = i
End Function
Function HowManyLeadingSpaces(ByVal aString As String) As String
HowManyLeadingSpaces = WhatIsLineIndent(aString) - 1
End Function
You can make calls like this :
Sub AddLineNumbers_vbLabelColon()
AddLineNumbers wbName:="EvaluateCall.xlsm", vbCompName:="ModLineNumbers_testDest", LabelType:=vbLabelColon, AddLineNumbersToEmptyLines:=True, AddLineNumbersToEndOfProc:=True, Scope:=vbScopeAllProc
End Sub
Sub AddLineNumbers_vbLabelTab()
AddLineNumbers wbName:="EvaluateCall.xlsm", vbCompName:="ModLineNumbers_testDest", LabelType:=vbLabelTab, AddLineNumbersToEmptyLines:=True, AddLineNumbersToEndOfProc:=True, Scope:=vbScopeAllProc
End Sub
Sub RemoveLineNumbers_vbLabelColon()
RemoveLineNumbers wbName:="EvaluateCall.xlsm", vbCompName:="ModLineNumbers_testDest", LabelType:=vbLabelColon
End Sub
Sub RemoveLineNumbers_vbLabelTab()
RemoveLineNumbers wbName:="EvaluateCall.xlsm", vbCompName:="ModLineNumbers_testDest", LabelType:=vbLabelTab
End Sub
And as a reminder, here as some compile rules about about line numbers:
- not allowed before a Sub/Function declaration statement
- not allowed outside of a proc
- not allowed on a line following a line continuation character «_» (underscore)
- not allowed to have more than one label/line number per code line ~~> Existing labels other than line numbers must be tested otherwise a compile error will occur trying to force a line number.
- not allowed to use characters that already have a special VBA meaning ~~> Allowed characters are [a-Z], [0-9], é, è, ô, ù, €, £, § and even «:» alone !
- compiler will trim any space before a label ~~> So if there is a label, the first char of the line is the first char of the label, it cannot be a space.
- appending a line number with a colon will result in having a space inserted between the «:» and the fist next char if there is none
- when appending a line number with a tab/space, there must be at least one space between the last digit and the first next char, compiler won’t add it as it does for a label with a colon separator
- the .ReplaceLine method will overide the compile rules without displaying any compile error as it does in design mode when selecting a new line or when manually relaunching compilation
- the compiler is ‘quicker than the VBA environment/system’: for example, just after a line number with colon and without any space has been inserted with .ReplaceLine, if the .Lines property is called to get the new string, the space (between the colon character and the first character of the string) is already appended in that string !
- it is not possible to enter debug mode after a .ReplaceLine has been called (from within or outside the module it is editting), not till the code is running, and execution reset.
I know this question has been addressed in several places here, but none so far are solving my problem, and I can’t think of anything else to search for.
Specifically, within an Excel macro I need to find the row number of the cell on a specific sheet that has a value that matches a variable. Then I need to use that row number to perform an action on another cell in that row. The value I’m looking for will be in Column C, then the action will be performed in Column A.
I have tried these solutions but no luck yet:
How to find the row number of a specific value in Excel using vbscript
Assigning the value of a worksheet cell to a constant
I’ve tried pasting my variable value into B1 as a placeholder, but I can’t find a way to find the matching value elsewhere on the sheet (there will be only one other). I’ve tried:
Public Const SKUVALUE As String = "$B$1"
but of course that returns the quoted string, not the value of the B1 cell.
If it would help for me to explain the bigger picture of my project I will be happy to do that, but it’s somewhat complicated. In short I am trying to bring content from one sheet to another sheet based on a numeric value (SKU) that is assigned to a specific row on each sheet. The row numbers will not match because of filters being applied on the first sheet. If I can mirror those filters on the second sheet then I would know the row number, but that sounds like a nightmare to implement. I can’t begin to think how to do it.
I’m really at my wit’s end with this. Any suggestions will be much appreciated.
Thank you.
Определение номера столбца и номера строки |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |