vba条件查找&更换

时间:2013-07-25 10:57:10

标签: vba conditional

今天我在尝试替换点(。)时遇到了这段代码,但只有在sheet1的列表中与另一个工作簿的另一个列表中的一个值(同一工作簿)不匹配时才会这样。 “参数不可选”是错误,但它不会给我任何其他提示。

子过滤器(wsS As Worksheet,wsN As Worksheet,i As Integer,j As Integer,k As Integer,l As Integer,as As Integer) “ '代替宏 “     Application.ScreenUpdating = False     范围(“a1”)。FormulaR1C1 =“已排序”     设置wsS =表格(“sheet1”)     设置wsN =表格(“non_confid”)     col1 =“A”     col2 =“E”     col3 =“C”

For a = 1 To 200
     If wsS.Range(col1 & a) = wsN.Range("AB2:ab600") Then
        a = a + 1
        Else: Range(col1 & a).Replace What:=".", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        a = a + 1
     End If
Next

Range("a1").AutoFilter
ActiveWorkbook.Worksheets("sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("sheet1").AutoFilter.Sort.SortFields.Add Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("sheet1").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Range("a1").Select
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 15773696
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With

With Selection.Font
    .Color = -10477568
    .TintAndShade = 0
End With

ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$a$500"), , xlYes).Name = "Table1"
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:="<>"

    Set wsS = Sheets("sheet1") 'this has assigned the worksheet sheet1 to wsS
    Set wsN = Sheets("non_confid") 'this has assigned the worksheet sheet1 to wsU

    i = 2

    For j = 2 To 300
        If Not IsEmpty(wsS.Range(col1 & j).Value) Then
            wsS.Range(col3 & i - 1).Value = wsS.Range(col1 & j).Value
            i = i + 1
        End If
    Next

Range("A:B").EntireColumn.Delete

    For k = 1 To 300
           If IsEmpty(wsS.Range(col1 & k).Value) Then
                i = i + 1 'Exit For 'this jumps out of the loop, so no more copying.
           Else
                wsN.Range(col2 & i).Value = wsS.Range(col1 & k).Value
                i = i + 1
           End If
    Next

Sheets("non_confid").Select
Columns("A:G").EntireColumn.AutoFit
Range("e1").Select
ActiveSheet.ListObjects("Status").Range.AutoFilter Field:=4, Criteria1:="<>"
Range("A1").Select
ActiveWorkbook.Saved = True
Application.ScreenUpdating = True

End Sub

提前感谢您的时间!

1 个答案:

答案 0 :(得分:1)

Replace所需的唯一参数是您提供的内容替换

我没有收到有关此内容的错误消息,但我使用此行:

If wsS.Range(col1 & a) = wsN.Range("AB2:ab600") Then

因为您尝试将单个值与Range进行比较。

(我假设wsS和wsN正确设置为工作表。)

这是任何错误的可能原因。但由于您的错误消息不同,我怀疑您没有发布您的实际代码,或者足以让我们复制您的问题。