简体   繁体   English

Excel VBA:如何扩展给定当前选择的范围

[英]Excel VBA: How to Extend a Range Given a Current Selection

I want to do something like:我想做类似的事情:

E18-(1,1) &":" &E18+(1,1)

My intent is to keep the selection of range E18 (value = B) and extend the selection to D16:F20 .我的意图是保留范围E18 (值 = B)的选择并将选择扩展到D16:F20

Cell_Image_Excel_Highlighted_B

If I have a cell's range of E18 and I want to extend the range to D16:F20 , how can I do this?如果我有一个单元格的范围E18并且我想将范围扩展到D16:F20 ,我该怎么做?

You mean like this?你的意思是这样?

SYNTAX句法

ExpandRange [ Range ], [ Number of Col on left ], [ Number of Rows on Top ], [ Number of Col on right ], [ Number of Rows down ] ExpandRange [范围]、[左侧列数]、[顶部行数]、[右侧列数]、[向下行数]

Sub Sample()
    Debug.Print ExpandRange(Range("B5"), 1, 1, 1, 1)            '<~~ $A$4:$C$6
    Debug.Print ExpandRange(Range("A1"), 1, 1, 1, 1)            '<~~ Error
    Debug.Print ExpandRange(Range("XFD4"), 1, 1, 1, 1)          '<~~ Error
    Debug.Print ExpandRange(Range("XFD1048576"), 1, 1, 1, 1)    '<~~ Error
    Debug.Print ExpandRange(Range("E5"), 1, 1, 1, 1)            '<~~ $D$4:$F$6
End Sub

Function ExpandRange(rng As Range, lft As Long, tp As Long, _
rt As Long, dwn As Long) As String
    If rng.Column - lft < 1 Or _
       rng.Row - tp < 1 Or _
       rng.Column + rt > ActiveSheet.Columns.Count Or _
       rng.Row + dwn > ActiveSheet.Rows.Count Then
        ExpandRange = "Error"
        Exit Function
    End If

    ExpandRange = Range(rng.Offset(-1 * tp, -1 * lft).Address & ":" & _
                        rng.Offset(dwn, rt).Address).Address
End Function

Here is the simple code that I use to resize an existing selection.这是我用来调整现有选择大小的简单代码。

Selection.Resize(Selection.Rows.Count + 5, Selection.Columns.Count + 50).Select

This will add 5 to the row count and 50 to the column count.这将使行数增加 5,列数增加 50。 Adapt to suit your needs.适应您的需求。

You can use Application.WorksheetFunction.Offset() which is richer than VBA's Offset and does everything required by the question.您可以使用比 VBA 的 Offset 更丰富的Application.WorksheetFunction.Offset()并完成问题所需的一切。
I think it does what Siddharth Rout ExpandRange does, without the need of a UDF.我认为它可以完成 Siddharth Rout ExpandRange 所做的工作,而无需 UDF。

Range(Cells(WorksheetFunction.Max(1, Selection.Row - 1), _
      WorksheetFunction.Max(1, Selection.Column - 1)), _
      Cells(WorksheetFunction.Min(Selection.Worksheet.Rows.Count, _
      Selection.Row + 1), _
      WorksheetFunction.Min(Selection.Worksheet.Columns.Count, _
      Selection.Column + 1))).Select

upd: thanks Siddharth Rout for formating my msg upd:感谢 Siddharth Rout 格式化我的味精

Instead of returning an absolute address, I modifying the syntax above to return a range.我没有返回绝对地址,而是修改了上面的语法以返回一个范围。 Credit goes to Siddharth Rout = )归功于 Siddharth Rout = )

Function ExpandRG(rng As Variant, lft As Long, tp As Long, rt As Long, dwn As Long) _
 As Range
 Set ws = rng.Parent
If rng.Column - lft < 1 Or _
   rng.Row - tp < 1 Or _
   rng.Column + rt > ActiveSheet.Columns.Count Or _
   rng.Row + dwn > ActiveSheet.Rows.Count Then
        MsgBox "Out of range"
        Exit Function
End If

 Set rng = ws.Range(rng.Offset(-1 * tp, -1 * lft).Address & ":" & _
                    rng.Offset(dwn, rt).Address)                        
End Function

Sub aa()
Dim ori_add, O_add, New_add As Range
Set ori_add = Range("B2")
Set O_add = ori_add

Call ExpandRG(ori_add, 1, 1, 1, 1)
Set New_add = ori_add

MsgBox "Original address " & O_add.Address & ", new address is" & New_add.Address
End Sub

How to select and extend a range from anywhere on a sheet, to anywhere on a sheet.如何选择范围并将范围从工作表上的任何位置扩展到工作表上的任何位置。

This is my first post.这是我的第一篇文章。 I know I'm a little bit late to the party, and it's obvious to me that most of the people here are far far more experienced and skilled than I am.我知道我参加聚会有点晚了,很明显,这里的大多数人都比我更有经验和技能。 So I doubt my solutions include much of their "big picture" nuanced considerations, but I've verified they work for me and I hope they work for all of you too.所以我怀疑我的解决方案是否包含他们的许多“大局”细微考虑,但我已经证实它们对我有用,我希望它们也对你们所有人有用。

Okay, so back to the question.好的,那么回到问题。 Here is how I do it.这是我如何做到的。

  • Example One示例一
    To do this for the exact scenario posed by your question, if you're starting at E18 and you want to extend the range to D16:F20, use the code below.要针对您的问题提出的确切场景执行此操作,如果您从 E18 开始并且希望将范围扩展到 D16:F20,请使用以下代码。 As long as you have room for the full range, your active cell can actually be anywhere , and that range will follow it.只要您有完整范围的空间,您的活动单元格实际上可以在任何地方,并且该范围将跟随它。

    Range(ActiveCell.Offset(-2, -1), ActiveCell.Offset(2, 1)).Select

  • Example Two示例二
    If you've already selected a range, and then you want to expand it further (let's say and additional 2 rows down and 1 column to the right), then do this:如果您已经选择了一个范围,然后您想进一步扩展它(假设向下增加 2 行,向右增加 1 列),请执行以下操作:

    Range(Selection, Selection.Offset(2, 1)).Select

  • Example Three例三
    If you want to select a range of all the contiguous cells containing data, starting from the active cell and continuing down until it reaches a blank cell, and then also add the cells from 1 column to the left , then do this:如果要选择包含数据的所有连续单元格的范围,从活动单元格开始并继续向下直到到达空白单元格,然后还将从 1 列到左侧的单元格添加,请执行以下操作:

    Range(ActiveCell, Selection.End(xlDown).Offset(0, -1)).Select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM