2016-07-21 3 views
0

Mein Code platziert einen Text in leere Zelle, füllt es jedoch nicht bis zur Boarderline.Platzieren Sie einen Text "Allgemein" in leere Zellen

Ich möchte den Text "allgemein" in leere Zellen der Spalte E platzieren, aber nicht bis zum Ende bcoz der Zeile zählen.

hier ist mein Code:

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 

Das Ergebnis ist dieses :(noch leere Spalte 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

Sie haben 'sourceCol = 5' 'Spalte F hat einen Wert von 5, tatsächlich hat Spalte F einen Wert von 6, also, wenn Sie Spalte F verwenden möchten, ändern Sie zu' sourceCol = 6' –

+0

SoRry, das ist angeblich Spalte E –

+0

Blick auf meine antworte unten. –

Antwort

0

Erläuterung Ihrer Fehler: Sie suchen letzte Zeile in Spalte E, die Sie 4 Reihen von Daten haben (indem Sie auf Ihr Beispiel schauen). Spalte F hat jedoch 8 Datenzeilen. Deshalb, wenn Sie 8 Zeile wollen Schleife durch, müssen Sie in Spalte für letzte Zeile suchen F, und nicht die Spalte E.

Versuchen der modifizierte Code unten:

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

Hallo @shai Rado Da die sourceCol ist 6, die leere Zelle bei Spalte F und nicht Spalte E ersetzen. Ich muss die leeren Zellen in Spalte E durch "allgemeine" und nicht Spalte F. ersetzen –

+0

@J_Gonzales siehe bearbeiteten Code in Antwort –

+0

danke @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 
Verwandte Themen