Excel VBA 保存范围参考

发布于 2025-01-16 15:29:57 字数 737 浏览 4 评论 0原文

我有一系列单元格,我正在扫描这些单元格是否有公式。 当它发生时,我想保存列字母和行号,即 E14、E18、F18、N18(参考)做一本字典。 循环遍历特定范围后,我想选择保存在字典中的单元格,以便稍后删除所选单元格中带有公式的所有单元格。

我坚持保护单元格对字典的引用的部分。 示例中的范围只是示例范围。

Sub check_formula_empty()

Dim cell As Range
Dim i As Integer
Dim rng As Range
Set rng = Range("E13:N19")

For i = 1 To rng.Cells.Count

    If rng.Cells(i).HasFormula = True And rng.Cells(i).Offset(-6, 0) = "A" Then

   'save reference range to Dictionary
    
    ElseIf rng.Cells(i).HasFormula = False And rng.Cells(i).Offset(-6, 0) = "F" Then
        
    rng.Cells(i).Offset(-4, 0).Copy _
    Destination:=rng.Cells(i)

    End If

Next

'Here I want to run the "Select my saved range from the Dictionary" and run "delete formulas"

End Sub

I have a range of cells which I'm scanning if the cell has a formular or not.
When it does, I want to save the column letters and row numbers i.e. E14, E18, F18, N18 (Reference) do a dictionary.
Once I've looped through my specific range, I want to select the cells saved in the dictionary to later on delete all cells with formulas in the selected cells.

I am stuck with the part to safe the cell reference to the dictionary.
The range in the example is just an example range.

Sub check_formula_empty()

Dim cell As Range
Dim i As Integer
Dim rng As Range
Set rng = Range("E13:N19")

For i = 1 To rng.Cells.Count

    If rng.Cells(i).HasFormula = True And rng.Cells(i).Offset(-6, 0) = "A" Then

   'save reference range to Dictionary
    
    ElseIf rng.Cells(i).HasFormula = False And rng.Cells(i).Offset(-6, 0) = "F" Then
        
    rng.Cells(i).Offset(-4, 0).Copy _
    Destination:=rng.Cells(i)

    End If

Next

'Here I want to run the "Select my saved range from the Dictionary" and run "delete formulas"

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿透光 2025-01-23 15:29:58

您可以为此目的收集一个集合。您提到了一本字典,但就您的目的而言,键并不那么重要,您只需要一个项目列表(集合支持两者)

Sub check_formula_empty()

Dim cell As Range
Dim i As Integer
Dim rng As Range
Set rng = Range("E13:N19")
dim reflist as Collection
Set reflist = new Collection
For i = 1 To rng.Cells.Count

    If rng.Cells(i).HasFormula = True And rng.Cells(i).Offset(-6, 0) = "A" Then

   'save reference range to Dictionary
        refList.Add rng.Cells(i)
    ElseIf rng.Cells(i).HasFormula = False And rng.Cells(i).Offset(-6, 0) = "F" Then
        
    rng.Cells(i).Offset(-4, 0).Copy _
    Destination:=rng.Cells(i)

    End If

Next

'Here I want to run the "Select my saved range from the Dictionary" and run "delete formulas"

Dim oneCell as Range

    foreach oneCell in refList
        oneCell.Value = vbEmpty
    next
End Sub

如您所见,我们首先将完整的单元格添加到集合中(它是一个引用的对象),然后您可以根据自己的喜好在 foreach 循环中使用它及其所有属性

You can us a collection for this purpose. You are mentioning a dictionary but for your purpose a key is not that important, you only need a list of items (collection supports both)

Sub check_formula_empty()

Dim cell As Range
Dim i As Integer
Dim rng As Range
Set rng = Range("E13:N19")
dim reflist as Collection
Set reflist = new Collection
For i = 1 To rng.Cells.Count

    If rng.Cells(i).HasFormula = True And rng.Cells(i).Offset(-6, 0) = "A" Then

   'save reference range to Dictionary
        refList.Add rng.Cells(i)
    ElseIf rng.Cells(i).HasFormula = False And rng.Cells(i).Offset(-6, 0) = "F" Then
        
    rng.Cells(i).Offset(-4, 0).Copy _
    Destination:=rng.Cells(i)

    End If

Next

'Here I want to run the "Select my saved range from the Dictionary" and run "delete formulas"

Dim oneCell as Range

    foreach oneCell in refList
        oneCell.Value = vbEmpty
    next
End Sub

As you can see we first add the complete cell to the collectdion (it is a referenced object) and later you can use it in the foreach loop to your liking with all its properties

愛放△進行李 2025-01-23 15:29:58

因此,我正在努力解决这个问题,以便比在每列中循环 2-3 倍更快地运行 VBA。

我当前努力解决的问题是:定义的范围“nof”或“DBRW”不断增加,当解析我的最终代码(删除或将公式复制到联合范围)时,会选择整个联合范围并因此,公式会被覆盖整个范围,而不是从一列循环到另一列并使用该列中定义的公式,该公式在固定行 (Cells(6, n)) 中可用。


Option Explicit

Sub Test3()

Dim i As Integer
Dim n As Integer
Dim x As Integer

Dim DBRW As Range
Dim DBRWrange(1 To 32) As Range
Dim nof As Range
Dim nofRange(1 To 32) As Range
Dim rangef As Range

    For n = 5 To 6
        For i = 13 To 20
            If Cells(i, n).HasFormula = True And Cells(7, n) = "A" Then
               
        
                Set DBRWrange(i) = Cells(i, n)
                    If DBRW Is Nothing Then
                        Set DBRW = DBRWrange(i)
                        Else
                        Set DBRW = Union(DBRW, DBRWrange(i))
                    End If
                
            ElseIf Cells(i, n).HasFormula = False And Cells(7, n) = "F" Then
            
                Set nofRange(i) = Cells(i, n)
                    If nof Is Nothing Then
                        Set nof = nofRange(i)
                        Else
                        Set nof = Union(nof, nofRange(i))
                    End If

            End If

        Next i
        
        Set rangef = Cells(6, n)

        rangef.Copy nof
        

'Ranges in nof and DBRW are kept (incremented), is there a way to "refresh" the Union reference, to restart creating the range from after this step?

    Next n

End Sub


´´´

So I was working on resolving the issue to run the VBA faster than looping 2-3x through each column.

My current issue, which I struggle to resolve is: that the defined range "nof" or "DBRW" keeps to increase, which when resolving my final code (delete or copy formula to the Union ranges), the whole Union ranges are selected and therefore formulars are overwritten for the full range, instead of looping from column to column and using the defined formula in that column, which is available in a fixed row (Cells(6, n)).


Option Explicit

Sub Test3()

Dim i As Integer
Dim n As Integer
Dim x As Integer

Dim DBRW As Range
Dim DBRWrange(1 To 32) As Range
Dim nof As Range
Dim nofRange(1 To 32) As Range
Dim rangef As Range

    For n = 5 To 6
        For i = 13 To 20
            If Cells(i, n).HasFormula = True And Cells(7, n) = "A" Then
               
        
                Set DBRWrange(i) = Cells(i, n)
                    If DBRW Is Nothing Then
                        Set DBRW = DBRWrange(i)
                        Else
                        Set DBRW = Union(DBRW, DBRWrange(i))
                    End If
                
            ElseIf Cells(i, n).HasFormula = False And Cells(7, n) = "F" Then
            
                Set nofRange(i) = Cells(i, n)
                    If nof Is Nothing Then
                        Set nof = nofRange(i)
                        Else
                        Set nof = Union(nof, nofRange(i))
                    End If

            End If

        Next i
        
        Set rangef = Cells(6, n)

        rangef.Copy nof
        

'Ranges in nof and DBRW are kept (incremented), is there a way to "refresh" the Union reference, to restart creating the range from after this step?

    Next n

End Sub


´´´
呆橘 2025-01-23 15:29:58

所以我已经解决了我的问题,对于未来的谷歌用户来说,这可能会有所帮助:)

Public Sub copy_paste_delete()

Dim i As Integer
Dim n As Integer

Dim DBRW As Range
Dim DBRWrange(1 To 150) As Range
Dim nof As Range
Dim nofRange(1 To 150) As Range
Dim rangef As Range

Application.ScreenUpdating = False

Worksheets("Tab1").Activate
Range("K29").Select

Set DBRW = Nothing
Set nof = Nothing

    For n = 61 To 75
    
    Set nof = Nothing
    Set DBRW = Nothing
    
        For i = 33 To 38
            If Cells(i, n).HasFormula = True And Cells(6, n) = "F" Then
        
                Set DBRWrange(i) = Cells(i, n)
                    If DBRW Is Nothing Then
                        Set DBRW = DBRWrange(i)
                        Else
                        Set DBRW = Union(DBRW, DBRWrange(i))
                    End If
                
                
            ElseIf Cells(i, n).HasFormula = False And Cells(6, n) = "A" And Cells(7, n) = "Done" Then
            
                Set nofRange(i) = Cells(i, n)
                     If nof Is Nothing Then
                        Set nof = nofRange(i)
                        Else
                        Set nof = Union(nof, nofRange(i))
                    End If
            End If
        Next i
                        Set rangef = Cells(19, n)
                        
                        On Error Resume Next
                        
                        rangef.Copy nof
   
    Next n

DBRW.Select
'Do some stuff

Application.ScreenUpdating = True

End Sub

so I have solved my issue and for future googlers, this might be helpful :)

Public Sub copy_paste_delete()

Dim i As Integer
Dim n As Integer

Dim DBRW As Range
Dim DBRWrange(1 To 150) As Range
Dim nof As Range
Dim nofRange(1 To 150) As Range
Dim rangef As Range

Application.ScreenUpdating = False

Worksheets("Tab1").Activate
Range("K29").Select

Set DBRW = Nothing
Set nof = Nothing

    For n = 61 To 75
    
    Set nof = Nothing
    Set DBRW = Nothing
    
        For i = 33 To 38
            If Cells(i, n).HasFormula = True And Cells(6, n) = "F" Then
        
                Set DBRWrange(i) = Cells(i, n)
                    If DBRW Is Nothing Then
                        Set DBRW = DBRWrange(i)
                        Else
                        Set DBRW = Union(DBRW, DBRWrange(i))
                    End If
                
                
            ElseIf Cells(i, n).HasFormula = False And Cells(6, n) = "A" And Cells(7, n) = "Done" Then
            
                Set nofRange(i) = Cells(i, n)
                     If nof Is Nothing Then
                        Set nof = nofRange(i)
                        Else
                        Set nof = Union(nof, nofRange(i))
                    End If
            End If
        Next i
                        Set rangef = Cells(19, n)
                        
                        On Error Resume Next
                        
                        rangef.Copy nof
   
    Next n

DBRW.Select
'Do some stuff

Application.ScreenUpdating = True

End Sub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文