【问题标题】:VBA Range Issues - Set and DynamicVBA 范围问题 - 设置和动态
【发布时间】:2017-12-01 18:37:35
【问题描述】:

更新:只需要学习如何使 fullrng 动态化

大图: 我正在尝试根据列中的数据自动创建命名范围。

我发布的子过程将数组的元素作为参数。所有这些逻辑都可以正常工作。我可以为数组的每个元素创建命名范围。

我想出了如何在我的范围内获取我想要的值的逻辑。

for rownum = 2 to finalrow 下一行

包含此逻辑。

问题是我的范围有问题。

如果我不使用 Set my range = Range(cells) 那么我会收到一个对象错误。这是有道理的,对象已被声明但未设置任何内容。

我只是不知道如何初始化这些范围,然后将它们更改为我想要的单元格组

fullrng 也需要是动态的是 2 个单元格高,然后是 3 ,然后是 4,等等。

Sub Createranges(ByVal TableName As String)

 If TableName <> "" Then

    Debug.Print TableName
    Dim fullrng As Range
    Dim temprng As Range
    Dim thiscell As Range
    Dim nextcell As Range

    Set fullrng = Range("H1")

    fullrng.Name = TableName

        For rownum = 2 To finalrow

            'Checking to see if cell should be in named range
            If Cells(rownum, ColumnVar).Value = TableName Then

                'Modify Ranges for union
                Set thiscell = Range(ColumnVar & Cells.Row, ColumnVar & Cells.Column)
                Set nextcell = Range(ColumnVar & Cells.Row + 1, ColumnVar & Cells.Column)

                'union the range to include all past matching values
                If thiscell.Value2 = nextcell.Value2(2, 1) Then
                    Set temprng = Range(thiscell, nextcell)
                    'Figure out dynamic ranges here
                    Set fullrng = Range(
                    fullrng = Union(fullrng, temprng)
                End If

            End If

        Next rownum
    End If


End Sub

【问题讨论】:

  • 这样可以吗?
  • 如果您要设置范围(即使通过重新分配),您需要使用 Set 关键字。

标签: vba excel object range


【解决方案1】:

您的所有错误都是因为您以不正确的方式分配范围对象。

这条线不好

Set temprng = Range(thiscell, nextcell)

thiscellnextcell 已经是范围。您应该使用Union 来组合它们:

set temrange=application.union(thiscell, nextcell)

以同样的方式改变这部分:

 set fullrange=application.union(fullrng,temprange)

作为一项改进,您必须始终确保在取消 Union 之前定义您正在组合的两个范围 (is not nothing)

例如,第一次执行上述行时,fullrange 可能会抛出错误,以避免这样做:

if not fullrange is nothing then
    set fullrange=application.union(fullrng,temprange)
else
    set fullrange=temprange
end if

【讨论】:

    【解决方案2】:

    原来有很多问题!

    我想这就是你不创建自己的对象时得到的结果,你必须探索并找出对象模型。

    除了一些明显错误的东西 --- 使用 Cells.Column 而不是我的 ColumnVar 和 rownum 变量,这是我所做的更改:

    我最初将我的范围设置为一个随机单元格,以便我可以命名我的范围。 fullrng.Name = TableName 在设置 fullrng 后,我刚刚将这一行移到了右侧。

    Sub Createranges(ByVal TableName As String)
    
    If TableName <> "" Then
    
    Debug.Print TableName
    Dim fullrng As Range
    Dim temprng As Range
    Dim thiscell As Range
    Dim nextcell As Range
    
    
        For rownum = 2 To finalrow
    
            'Checking to see if cell should be in named range
            If Cells(rownum, ColumnVar).Value = TableName Then
    
                'Modify Ranges for union
                Set thiscell = Range(ColumnVar & rownum, ColumnVar & rownum)
                Set nextcell = Range(ColumnVar & rownum + 1, ColumnVar & rownum + 1)
    
                'union the range to include all past matching values
                If thiscell.Text = nextcell.Text Then
                    Set temprng = Application.Union(thiscell, nextcell)
                    If Not fullrng Is Nothing Then
                        Set fullrng = Application.Union(fullrng, temprng)
                        fullrng.Name = TableName
                    Else
                        Set fullrng = temprng
                    End If
    
                End If
    
            End If
    
        Next rownum
    End If
    
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多