excel - 如果字段为空,VBA 循环取值

标签 excel vba

在VBA中需要一些解决方案->如果A列中的空白值然后从B列中获取值。
我写了一些代码,但我不知道为什么这不起作用。

dim LastR as Long
LastR = Worksheets("Sheet1").Range("BU" & Worksheets("Sheet1").Rows.Count).End(xlUp).Row
dim i as long
For i = LastR To 2 Step -1
    If IsEmpty(Cells(i, "a")) Then Cells(i, "a").Value = Cells(i, "b").Value
Next i

最佳答案

您应该检查该值是否为空。
看两个例子:

Dim LastR As Long
LastR = Worksheets("Sheet1").Range("BU" & Worksheets("Sheet1").Rows.Count).End(xlUp).Row
Dim i As Long
For i = LastR To 2 Step -1
    'If Sheets("Sheet1").Cells(i, "a") = "" Then Sheets("Sheet1").Cells(i, 1).Value = Cells(i, 2).Value
    If IsEmpty(Sheets("Sheet1").Cells(i, "a").Value) = True Then Sheets("Sheet1").Cells(i, 1).Value = Cells(i, 2).Value
Next i

关于excel - 如果字段为空,VBA 循环取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68563902/

相关文章:

excel - 复制 Excel 范围并仅粘贴值 (pasteSpecial),并在 VBScript 中跳过空格

vba - 需要帮助使用 VBA 锁定选定的单元格范围

xml - 通过 VBA 和剪贴板将 HTML 复制到 Word 会丢失特殊字符

excel - 关闭源文件时,无法在 for 循环中重用内容

vba - 两台计算机试图访问第三台计算机(服务器)中的 excel 文件进行写入。方法的问题

excel - 使用 Excel VBA : wait for the page to fully load 使用 XML HTTP 请求抓取网站

vba - Excel VBA中的循环引用

excel - 如何编写使用写入单元作为引用的 UDF

excel - 在随机 5x5 数组中查找值的单元格引用

VBA Office 2010 Outlook 问题