简体   繁体   English

VBA Excel 选择命名范围内的一系列单元格

[英]VBA Excel select a range of cells within a named range

I have a Named Range col_9395 it is an entire column.我有一个命名范围col_9395它是一整列。 I want to set a range within this Named range.我想在这个命名范围内设置一个范围。 I want the range to start at row 3 to row 200 of same column.我希望范围从第 3 行到同一列的第 200 行。 Whats the best way to do this?什么是最好的方法来做到这一点?

Original working line without Named Range:没有命名范围的原始工作线:

Set rngBlnk = Sheet108.Range("T3:T200").SpecialCells(xlCellTypeBlanks)

This is the code I tried with no luck:这是我没有运气尝试的代码:

Set rngBlnk = Range("col_9395)(3,1):Range("col_9395)(200,1).SpecialCells (xlCellTypeBlanks)

You can see the sort of logic with你可以看到这种逻辑

Option Explicit

Sub test()

Dim colToUse As Long
colToUse = ThisWorkbook.Worksheets("Sheet1").Range("ol_9395").Column

With ThisWorkbook.Worksheets("Sheet1")
Debug.Print .Range(.Cells(3, colToUse), .Cells(200, colToUse)).Address
End With

End Sub
Sub t()
Dim rng As Range
Set rng = Range("col_9395") ' for easier use

Dim blnkRng As Range
Set blnkRng = Range(Cells(rng.Rows(3).Row, rng.Column), Cells(rng.Rows(200).Row, rng.Column)).SpecialCells(xlCellTypeBlanks)
blnkRng.Select
End Sub

What I did was assign your named range to a variable (just for easier referencing).我所做的是将您的命名范围分配给一个变量(只是为了更容易引用)。 Then using the Range() property, I used the 3rd and 200th row of your named range to set the range to look for blank cells.然后使用Range()属性,我使用命名范围的3rd行和200th行来设置范围以查找空白单元格。

The idea is that this will help you in the event your named range isn't simply an entire column.这个想法是,如果您的命名范围不仅仅是一整列,这将对您有所帮助。 It will get the relative 3rd and 200th row from your named range.它将从您的命名范围中获取相对的第 3 行和第 200 行。

Option Explicit

Public Sub TestMe()

    Dim rngBlnk As Range

    Dim firstCell As Range
    Dim lastCell As Range

    Set firstCell = [col_9395].Cells(3, 1)
    Set lastCell = [col_9395].Cells(200, 1)

    If WorksheetFunction.CountBlank(Range(firstCell, lastCell)) > 0 Then
        Set rngBlnk = Range(firstCell, lastCell).SpecialCells(xlCellTypeBlanks)
    End If   

End Sub

A kind of a problem with SpecialCells and assigning to them is that if there are no cells from the specific type, it throws an error. SpecialCells和分配给它们的一个问题是,如果没有来自特定类型的单元格,它会引发错误。

Thus, there is a check with WorksheetFunction.CountBlank()>0 , before the setting of rngBlnk to the special cells.因此,在将rngBlnk设置为特殊单元格之前,需要检查WorksheetFunction.CountBlank()>0

Might be wrong, but I find this the easiest one:可能是错的,但我觉得这是最简单的一个:

Private Sub Test()
    Dim rngBlnk As Range
    Set rngBlnk = Range("col_9395").Rows("3:200").SpecialCells(xlCellTypeBlanks)
End Sub

I'm late to the party, I know, but maybe this will help someone.我参加聚会迟到了,我知道,但也许这会对某人有所帮助。 I've stumbled on a technique that I don't see presented as an option in any of the handful of "range-in-a-range" questions here.我偶然发现了一种技术,在此处的任何少数“范围内”问题中,我都没有看到这种技术。

I discovered you can ask VBA for a range of a range directly.我发现您可以直接向 VBA 询问范围的范围。 I have formatted some data as a Table, but it could be simply a Named Range, or even an unnamed range I suppose.我已经将一些数据格式化为表格,但它可能只是一个命名范围,甚至我认为是一个未命名的范围。 My code that is working looks like this:我正在运行的代码如下所示:

        With Workbooks(Filename)
            .Worksheets(tabName).Activate
            .Worksheets(tabName).Range("SummaryBand").Range("B2:R2").Copy
            End With

My Table is named SummaryBand, which due to a previous step was not necessarily always in the same absolute position on the spreadsheet, but I wanted an absolute Range within SummaryBand.我的表格被命名为 SummaryBand,由于上一步不一定总是在电子表格上的相同绝对位置,但我想要一个在 SummaryBand 内的绝对范围。 In this example, "B2:R2" is the absolute position within the Table, with the top left cell of the Table being A1.在本例中,“B2:R2”是表格内的绝对位置,表格左上角的单元格为 A1。

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

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