我遇到以下代码问题。我希望它循环一个范围(AK2直到数据结束),然后任何时候它找到一个空单元格,它将其值更改为Account receivable。它没有这样做,我也没有得到任何错误通知。我的猜测是我在设置变量时做错了什么:
Option Explicit
Private Sub Leere()
Dim rng As range
Dim rcell As range
Dim WS As Worksheet
Set WS = Worksheets("Sheet2")
Set rng = WS.range("AK2", range("AK2").End(xlDown))
For Each rcell In rng
If rcell.Value = " " Then
rcell.Value = "Accounts Receivable"
End If
Next
End Sub
有什么想法吗?
答案 0 :(得分:2)
使用SpecialCells()
With ws
.Range("AK2", .Cells(.Rows.Count, "AK").End(xlUp)).SpecialCells(XlCellType.xlCellTypeBlanks).Value = "Accounts Receivable"
End With
答案 1 :(得分:0)
您应该能够用以下内容替换空(无公式)单元格:
Set ws = ThisWorkbook.Worksheets("Sheet2")
Set rng = ws.Range("AK2", ws.Cells(ws.Rows.Count, "AK").End(xlUp))
rng.Replace "", "Accounts Receivable", xlWhole
另一个非VBA选项可以是空白单元格的条件格式。单元格值仍为空白,但显示的文本更改将是动态的。