在空白单元格中放置文本“general”

时间:2016-07-21 07:26:24

标签: excel excel-vba is-empty vba

我的代码会在空白单元格中放置一个文本,但不会将其填充到边界内。

我想将文本“general”放在E列的空白单元格中,但不会将其填充到rowcount的结束bcoz。

这是我的代码:

Sub FindandReplace()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String

sourceCol = 5   'column E has a value of 5
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row

'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
       Cells(currentRow, sourceCol) = "general"
    End If
Next
End Sub

结果如下:(仍为空列E)

Column E                               Column F

general                          Use-Limit
general                          Use-Limit
XL354L,XL354H,XL356L,XL356H          Use-Limit
XL353,XL355,XL357                    Use-Limit
                                 Use-Limit
                                 Use-Limit
                                 Use-Limit
                                 Use-Limit

2 个答案:

答案 0 :(得分:0)

您的错误说明:您正在寻找 E列中的最后一行,您有4行数据(通过查看您的示例)。但是,列F 有8行数据。因此,如果要遍历第8行,则需要在列F 中查找最后一行,而不是在列E中查找。

尝试以下修改后的代码:

!VAR1

答案 1 :(得分:0)

Sub FindandReplace()

Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String

sourceCol = 5   ' find last row in column F
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
rowCount2 = Cells(Rows.Count, sourceCol + 2).End(xlUp).Row ' add this to loop 8 times

'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount2
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
       Cells(currentRow, sourceCol) = "general"
    End If
Next

End Sub