vba Excel 2007 宏获取选定的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21924990/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:04:20  来源:igfitidea点击:

Excel 2007 macro get selected rows

excelvbaexcel-vba

提问by Haris

I would like to get the rows which are already selected by a user in an excel sheet using macros. How should I do that?

我想使用宏在 Excel 工作表中获取用户已经选择的行。我该怎么做?

I have attached an image. I have selected row number 3. I want to get that selected row in a macro. If the user selects more than one row, I want to get all those selected rows in the macro.

我附上了一张图片。我选择了第 3 行。我想在宏中获取所选行。如果用户选择多于一行,我想在宏中获取所有这些选定的行。

Selected row in excel sheet

Excel 表格中的选定行

回答by Santosh

Selectionwill get the current selected range.

Selection将获得当前选择的范围。

Sub test()

        Dim rng As Range
        Set rng = Selection

         'Will return address of selected range
        MsgBox rng.Address

        'will return row num
       Msgbox rng.Row

       'will give start row
       MsgBox "Start Row : " & rng.Row

      'will give end row
        MsgBox "End Row : " & rng.Row + rng.Rows.Count - 1

    End Sub