2016-07-21 9 views
0

Мой код помещает текст в пустую ячейку, но не заполняет его до границы.Поместите текст «общий» в пустые ячейки

Я хочу поместить текст «общий» в пустые ячейки столбца E, но не заполняет его до конца 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

Hello @shai Rado Поскольку sourceCol - это 6, который заменит пустую ячейку в столбце F, а не на столбце E. Мне нужно заменить пустые ячейки в столбце E «общим», а не столбцом F. –

+0

@J_Gonzales см. Отредактированный код в ответ –

+0

спасибо @shai Rado –

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 

 Смежные вопросы

  • Нет связанных вопросов^_^