arrays - 当单元格值 = 0 VBA 时将单元格地址添加到数组
问题描述
我有一个 for 循环遍历的单元格范围。如果有一个单元格中包含 0,则更改为某个数字并将单元格地址保存到数组中?
我不确定有多少单元格一开始就会有 0。
下面是我希望它如何工作的伪代码
For i = 1 to 9
For j = 1 to 9
if cell.value = 0 then
cell.value = x
'''Add cell.address to array'''
End if
next j
next i
编辑:感谢大家的帮助。现在我可以引用将地址添加到数组中,有没有办法根据需要返回地址?我希望能够引用我修改的最后一个单元格,就像在三个块引号中看到的那样:
For i = 1 to 9
For j = 1 to 9
if cell.value = 0 then
cell.value = x
nums(n) = cell.adress
n = n+1
Elseif cell.value = y
"""Return back to the last cell added to the array, and put x+1"""
End if
next j
next i
如果我需要提出另一个问题,我可以。
解决方案
你可以这样做:
Dim ranges(81) ' Enough to hold 9x9 ranges
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = 1
For i = 1 to 9
For j = 1 to 9
If cell.value = 0 then
cell.value = x
Set ranges(k) = cell
k = k + 1
ElseIf k>1 Then
Set ranges(k-1).Value = x+1
End if
next j
next i
推荐阅读
- xaml - Xamarin.Forms:当顶部有对象(如标签和另一个图像)时捕获单击按钮的最佳方式
- javascript - 无限循环 useEffect() 钩子
- python - Discord.py 权限系统
- c# - 使用 Docusign admin api [docusignapi] 更新 UserProfile 时出错
- reactjs - 如何使用 Formik 在 onchange 中调用两个函数
- angular - 为什么我的路由器导航功能在 Angular 8 中不起作用?
- reactjs - 新 MERN 项目的正确结构
- nginx - nginx server_name 未打开指定位置
- php - 当 pdo connect 出错时,catch 没有被激活
- php - PHP 抓取 - 自动更新