我刚刚回到3个月前我正在处理的项目中找到我代码中的一些错误。下面的代码旨在用“BLANK”一词填充不同工作表中的空格/单个空格。
问题是代码没有填充所有指定列中的空白,而是填充在其他列的目标范围之外 - 例如,在“管板”列中,U未填充且列s已填充但是“BLANK”这个词。在代码中断模式下,填充的列是不同的。
之前在此处获得了有关此代码的一些很好的反馈,请提前感谢!
Sub FILL_blanks() '''' this searches for blanks and replaces them with BLANK
Dim LastRow As Long '''' HYDRANT, NODE ---->CHANGES LENGTH FOR EACH ASSET
Dim Sheet As Worksheet
Dim TargetRange As Range
Dim TargetRange_1 As Range
Dim TargetRange_2 As Range
Dim TargetRange_3 As Range
Dim TargetRange_4 As Range
'loop through worksheets in this workbook
For Each Sheet In ThisWorkbook.Worksheets
'''''''''''''HYDRANT
If Sheet.Name = "hydrant" Then '<~ concerned with col G on hydrant sheet
With Sheet
LastRow = .Range("G" & .Rows.Count).End(xlUp).Row
Set TargetRange = .Range("r4:r" & LastRow) 'elevation
Set TargetRange_1 = Range("v4:v" & LastRow) 'prop lev
End With
'apply replacement to the target range
'"" accounts for true blank cells (no spaces)
' " " is accounts for one space
TargetRange.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows 'source of error here?
TargetRange.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows 'add more columns later
TargetRange_1.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
''''''''''''''VALVE
''length s
ElseIf Sheet.Name = "valve" Then '<~ concerned with col G on hydrant sheet
With Sheet
LastRow = .Range("j" & .Rows.Count).End(xlUp).Row
Set TargetRange = Range("s4:s" & LastRow) 'length
Set TargetRange_1 = Range("u4:u" & LastRow) 'diam
Set TargetRange_2 = Range("y4:y" & LastRow) 'CW-k
Set TargetRange_3 = Range("aq4:aq" & LastRow) 'construction type
End With
'apply replacement to the target range
'"" accounts for true blank cells (no spaces)
' " " is accounts for one space
TargetRange.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_2.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_2.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_3.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_3.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
ElseIf Sheet.Name = "pipe" Then
With Sheet
LastRow = .Range("j" & .Rows.Count).End(xlUp).Row
Set TargetRange = Range("s4:s" & LastRow) 'length ''''''tHIS IS NOT WORKING IT IS FILLING S3;''''''''''''''''27.08.14
Set TargetRange_1 = Range("u4:u" & LastRow) 'diam
Set TargetRange_2 = Range("y4:y" & LastRow) 'CW-k
Set TargetRange_3 = Range("ae4:ae" & LastRow) 'material
Set TargetRange_4 = Range("ag4:ag" & LastRow) 'year
End With
TargetRange.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_2.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_2.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_3.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_3.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_4.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_4.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
ElseIf Sheet.Name = "meter" Then
With Sheet
LastRow = .Range("j" & .Rows.Count).End(xlUp).Row
Set TargetRange = Range("y4:y" & LastRow) 'CW-k
Set TargetRange_1 = Range("s4:s" & LastRow) 'z '''added
End With
TargetRange.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows '''added
TargetRange_1.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
ElseIf Sheet.Name = "node" Then
With Sheet
LastRow = .Range("g" & .Rows.Count).End(xlUp).Row
Set TargetRange = Range("r4:r" & LastRow) 'z
End With
TargetRange.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
ElseIf Sheet.Name = "address_point" Then
With Sheet
LastRow = .Range("bp" & .Rows.Count).End(xlUp).Row
Set TargetRange = Range("i4:i" & LastRow) 'z
Set TargetRange_1 = Range("r4:r" & LastRow) 'z
End With
TargetRange.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange_1.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
End If
Next Sheet
End Sub