Excel VBA 保存范围参考
我有一系列单元格,我正在扫描这些单元格是否有公式。 当它发生时,我想保存列字母和行号,即 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为此目的收集一个集合。您提到了一本字典,但就您的目的而言,键并不那么重要,您只需要一个项目列表(集合支持两者)
如您所见,我们首先将完整的单元格添加到集合中(它是一个引用的对象),然后您可以根据自己的喜好在 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)
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
因此,我正在努力解决这个问题,以便比在每列中循环 2-3 倍更快地运行 VBA。
我当前努力解决的问题是:定义的范围“nof”或“DBRW”不断增加,当解析我的最终代码(删除或将公式复制到联合范围)时,会选择整个联合范围并因此,公式会被覆盖整个范围,而不是从一列循环到另一列并使用该列中定义的公式,该公式在固定行 (Cells(6, n)) 中可用。
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)).
所以我已经解决了我的问题,对于未来的谷歌用户来说,这可能会有所帮助:)
so I have solved my issue and for future googlers, this might be helpful :)