简体   繁体   中英

VBA to Find And Replace Blank Cells in Excel

Someone closed my question, although I don't know how it could be more "focused"; it seems like a pretty straightforward and simple question:

I'm trying to write some VBA code to replace blanks cells in one column of a worksheet with the value on the same row in another column. In other words, go down all the rows and if column D is blank, replace it with column A .

I tried this but it just replaces the blank value with the contents of the top row of Column A

.Columns("D").Replace What:="", Replacement:=.Columns("A"), LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Any suggestions?

Thanks!

You'd need to loop this:

Dim lastRow as Long
lastRow = Range("D" & rows.count).End(xlUp).Row
Dim i as Long
For i = 2 to lastRow ' change 2 if you want to start at any other row
    If Cells(i,4).Value = "" Then Cells(i,4).Value = Cells(i,1).Value
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM