2016-07-21 30 views
0

我的代碼在空白單元格中放置了文本,但沒有填充到邊界線上。在空白單元格中放置文本「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 
+0

你有'sourceCol = 5''列的F值爲5,實際上列F的值爲6,所以如果你想使用F列,修改爲'sourceCol = 6' –

+0

SOrry,據說是列E –

+0

看着我回答如下。 –

回答

0

你的錯誤的說明:您在列尋找最後一行E,您有4行數據(通過查看您的示例)。但是,列F有8行數據。因此,如果通過8行要循環,你需要尋找的最後一排F列,而不是列E.

嘗試下面的修改後的代碼:

Sub FindandReplace() 

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

sourceCol = 6 ' find last row in column F 
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-1).Value 
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then 
     Cells(currentRow, sourceCol-1) = "general" 
    End If 
Next 

End Sub 
+0

你好@shai雷達 由於sourceCol是6,將在F欄替換空單元而不是列E.我需要「一般」,而不是列F. –

+0

@J_Gonzales見編輯的代碼來替換在列E中的空單元在回答 –

+0

謝謝@shai雷達 –

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 
相關問題