Excel VBA查找并替换跳过的某些单元格
我有一段代码,可以在给定的列中找到空格,并用“BLANK”替换它们,这在过去工作得很好,适用于我正在查看的第一栏中的所有工作表 在“仪表”表中,整个栏是空白的,但“查找和替换”填充了空白栏的所有第6栏,没有明显的模式,如下所示。我希望这可能是我周一早上的另一个“用户故障”错误,但如果有任何见解,我将不胜感激 我知道这在循环中会更好,一旦我解决了它缺少一些空格的问题,我就会写循环 干杯Excel VBA查找并替换跳过的某些单元格,excel,replace,vba,Excel,Replace,Vba,我有一段代码,可以在给定的列中找到空格,并用“BLANK”替换它们,这在过去工作得很好,适用于我正在查看的第一栏中的所有工作表 在“仪表”表中,整个栏是空白的,但“查找和替换”填充了空白栏的所有第6栏,没有明显的模式,如下所示。我希望这可能是我周一早上的另一个“用户故障”错误,但如果有任何见解,我将不胜感激 我知道这在循环中会更好,一旦我解决了它缺少一些空格的问题,我就会写循环 干杯 Public Function FILL_blanks() '''' this searches for bla
Public Function FILL_blanks() '''' this searches for blanks
'in the columns in the raw data we are interested in and replaces
'them with BLANK there is a value assigned to BLANK in the flag matrix.
Dim LastRow_g As Long '''' HYDRANT, NODE ---->CHANGES LENGTH FOR EACH ASSET
Dim LastRow_j As Long ''''
Dim LastRow_bp As Long ''''
Dim WS_Count As Integer
Dim i As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
For i = 1 To WS_Count
If ActiveWorkbook.Worksheets(i).Name = "hydrant" Then
Worksheets(i).Select
Range("g4").Select ' this will change j/g/bp only
LastRow_g = Range("g" & Rows.Count).End(xlUp).Row 'define the last row as all of the rows in DMA flag column
Range("r4:r" & LastRow_g).Select
'find and replace below
Selection.Replace What:="", Replacement:="BLANK", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ElseIf ActiveWorkbook.Worksheets(i).Name = "meter" Then
Worksheets(i).Select
Range("j4").Select
LastRow_j = Range("j" & Rows.Count).End(xlUp).Row 'define the last row
Range("y4:y" & LastRow_j).Select
'find and replace below
Selection.Replace What:="", Replacement:="BLANK", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
我会在这里使用
Sub
,而不是Function
,因为似乎没有返回。此代码替换上述列中的空白单元格:
Option Explicit
Sub FillBlanks2() '''' this searches for blanks
Dim LastRow As Long '''' HYDRANT, NODE ---->CHANGES LENGTH FOR EACH ASSET
Dim Sheet As Worksheet
Dim TargetRange As Range
'loop through worksheets in this workbook
For Each Sheet In ThisWorkbook.Worksheets
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(.Cells(4, 7), .Cells(LastRow, 7))
End With
'apply replacement to the target range
TargetRange.Replace What:="", Replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
ElseIf Sheet.Name = "meter" Then '<~ concerned with col J on hydrant sheet
With Sheet
LastRow = .Range("J" & .Rows.Count).End(xlUp).Row
Set TargetRange = .Range(.Cells(4, 10), .Cells(LastRow, 10))
End With
'apply replacement to the target range
TargetRange.Replace What:="", Replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
End If
Next Sheet
End Sub
选项显式
子FillBlanks2()“”“”此函数搜索空格
当长度为“”时,节点-->将更改每个资源的长度
将工作表设置为工作表
变暗目标范围作为范围
'循环浏览此工作簿中的工作表
用于此工作簿中的每张工作表。工作表
如果Sheet.Name=“synthup”,那么“我修改了Dan Wagner的代码,以解释那些看似空白但实际上有空格的单元格。如果单元格可能只包含一个空格或一个空格,则可以使用“”和“”
然而,我相信有一个更优雅的解决方案可以解决所有的空白。SpecialCells(xlCellTypeBlanks)是一种可能性,但它似乎仅限于一定数量的行
Sub FILL_blanks() '''' this searches for blanks
Dim LastRow As Long '''' HYDRANT, NODE ---->CHANGES LENGTH FOR EACH ASSET
Dim Sheet As Worksheet
Dim TargetRange As Range
Sheets("Sheet1").Select
LastRow = Range("a" & Rows.Count).End(xlUp).Row
Set TargetRange = Range("b4:b" & LastRow)
'apply replacement to the target range
'"" accounts for true blank cells (no spaces)
' "*" is a wildcard and accounts for one or more spaces
TargetRange.Replace What:="", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
TargetRange.Replace What:=" ", replacement:="BLANK", LookAt:=xlWhole, SearchOrder:=xlByRows
End Sub
再次感谢您的帮助您所说的“第一栏”和“第六栏”是什么意思?我怀疑上面缺少一些代码。。。例如,变量LastRow\u bp
未使用,如果语句没有结束End
,请仔细查看“仪表”工作表上的J列。最后一排在哪里?这些行是手动隐藏的还是通过过滤器隐藏的?Ron,我的意思是,除了我在VBA中说bar.AFAIK外,使用函数而不是Sub没有任何缺点。很好@iDevlop,我想我的建议更多的是一种约定,而不是语法要求。根据我的经验,函数
通常在返回某物时使用。您对功能
与子功能
有何想法和建议?如有疑问,请使用功能。你可以把它叫做Sub,但反过来说却不是真的。其他优点:通过使用=MyFunction()
语法,可以从多个表单事件调用相同的函数。至少你可以总是返回True/False来表示成功/错误。看起来不错,谢谢Dan,我稍后会测试它,让你知道它是否有效,看起来它能很好地完成这个任务我改变了你给我的循环,使之与我感兴趣的列相关,循环运行良好,但我的问题的根源实际上是我的一些数据列包含空格而不是空格,有没有办法用代码替换空格或空格,这将替换单元格中的任何字符。