excel - 如何循环遍历 K 列中的每一行,直到 A 列为空
问题描述
我是 VBA 新手,正在尝试弄清楚如何执行以下操作:
- 循环遍历 K 列中的所有行,直到 A 为空
- 如果 K 列中的单元格为空,则用 E 列中同一行的值填充它
- 如果 K 仍然为空,则用 G 列中同一行的值填充它
- 如果 K 仍然为空,则用 I 列同一行中的值填充它
我可以毫不费力地让代码查看一个特定的行,但我不知道如何让它循环遍历 A 不为空的所有行。
Sub FillEmptyCells()
Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Do Until IsEmpty("A1:A")
If IsEmpty(Range("K1").Value) = True Then
Range("K1") = Range("E1")
End If
If IsEmpty(Range("K1").Value) = True Then
Range("K1") = Range("G1")
End If
If IsEmpty(Range("K1").Value) = True Then
Range("K1") = Range("I1")
End If
Loop
End Sub
解决方案
这是一种方法:
Sub tgr()
Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet
With ws.Range("K1:K" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
.Formula = "=IF(E" & .Row & "<>"""",E" & .Row & ",IF(G" & .Row & "<>"""",G" & .Row & ",I" & .Row & "))"
.Value = .Value
End With
End Sub
推荐阅读
- javascript - 使用生成器/迭代器包装结果集回调函数
- python - 为什么我不能使用 os.listdir 在我的 Python 程序中更改我的文件名
- ruby - 如何修复 WSL 上 Ruby Gems 安装程序的错误?
- c++ - 在 Visual Studio 中创建第一个 C++ 项目
- c# - 如何使用 C# 在 WPF 中的画布内拖放图像
- jenkins - 如何使用 UFT 自动化 Jenkins 工作场景?
- node.js - 模拟 apollo graphql 客户端
- powershell - Powershell 打开新日历事件
- java - 单个观察者没有调用 onError()
- javascript - 在jquery每个循环之外访问变量