试图执行给定的vb代码,但它不适用于某些字符,如"!","〜"

时间:2016-05-02 18:59:14

标签: excel-vba vba excel

请查看%,@符号表示良好,但不能使用〜或!必须包含更多特殊字符。

所有代码运行正常,但没有删除它们请帮助并提前感谢您的帮助。

Private Sub NewOrdersData()

Dim fndList As Variant
Dim x As Long

i = 0
n = 2

fndList = Array("!", "@", "#", "$", "%", "~")

Worksheets("UploadData").Cells.ClearContents
Sheets("ProcessedData").Activate  'data to be entered
u = WorksheetFunction.CountIf(Columns(1), ">0")

    For i = 2 To u ' Till End of Record Count

    If Cells(i, 16) = "Upload" Then
    Sheets("UploadData").Cells(1, 1) = Cells(1, 2)
    Sheets("UploadData").Cells(1, 2) = Cells(1, 3)
    Sheets("UploadData").Cells(1, 3) = Cells(1, 4)
    Sheets("UploadData").Cells(1, 4) = Cells(1, 5)
    Sheets("UploadData").Cells(1, 5) = Cells(1, 6)
    Sheets("UploadData").Cells(1, 6) = Cells(1, 7)
    Sheets("UploadData").Cells(1, 7) = Cells(1, 8)
    Sheets("UploadData").Cells(1, 8) = Cells(1, 9)
    Sheets("UploadData").Cells(1, 9) = Cells(1, 10)
    Sheets("UploadData").Cells(1, 10) = Cells(1, 11)
    Sheets("UploadData").Cells(1, 11) = Cells(1, 12)
    Sheets("UploadData").Cells(1, 12) = Cells(1, 13)
    Sheets("UploadData").Cells(1, 13) = Cells(1, 14)
    Sheets("UploadData").Cells(n, 1) = Cells(i, 2)
    Sheets("UploadData").Cells(n, 2) = Cells(i, 3)
    Sheets("UploadData").Cells(n, 3) = Cells(i, 4)
    Sheets("UploadData").Cells(n, 4) = Cells(i, 5)
    Sheets("UploadData").Cells(n, 5) = Cells(i, 6)
    Sheets("UploadData").Cells(n, 6) = Cells(i, 7)
    Sheets("UploadData").Cells(n, 7) = Cells(i, 8)
    Sheets("UploadData").Cells(n, 8) = Cells(i, 9)
    Sheets("UploadData").Cells(n, 9) = Cells(i, 10)
    Sheets("UploadData").Cells(n, 10) = Cells(i, 11)
    Sheets("UploadData").Cells(n, 11) = Cells(i, 12)
    Sheets("UploadData").Cells(n, 12) = Cells(i, 13)
    Sheets("UploadData").Cells(n, 13) = Cells(i, 14)

    n = n + 1
    End If

    Next
For x = LBound(fndList) To UBound(fndList)
    Worksheets("UploadData").Cells.Replace _
What:=fndList(x), Replacement:="" ', _
    Next x

   Sheets("UploadData").Activate
           MsgBox "Complete"
End Sub

1 个答案:

答案 0 :(得分:2)

变化:

 What:=fndList(x) 

要:

What:="~" & fndList(x) 

~告诉excel查找特定字符而不使用其通配符属性。

所以是的,最后一个字符串是"~~"。但是,这将查找~并替换它。

它也适用于常规字符,因此您无需为已经工作的字符更改它。把它放在一切的前面。

我还清理了一些代码:

Private Sub NewOrdersData()

Dim fndList() As Variant
Dim x As Long
Dim ws As Worksheet
Dim u As Long, i As Long
n = 2

fndList = Array("!", "@", "#", "$", "%", "~")
With Worksheets("UploadData")
    .Cells.ClearContents
    Set ws = Sheets("ProcessedData")  'data to be entered
    u = .Range("A" & .Rows.Count).End(xlUp).Row

    For i = 2 To u ' Till End of Record Count

        If ws.Cells(i, 16) = "Upload" Then
            .Range(.Cells(1, 1), .Cells(1, 13)).Value = ws.Range(ws.Cells(1, 2), ws.Cells(1, 14)).Value
            .Range(.Cells(n, 1), .Cells(n, 13)).Value = ws.Range(ws.Cells(i, 2), ws.Cells(i, 14)).Value
            n = n + 1
        End If

    Next i
    For x = LBound(fndList) To UBound(fndList)
        .Cells.Replace "~" & fndList(x), ""
    Next x
    .Activate
End With

MsgBox "Complete"

End Sub

修改

为了表明循环有效,我使用了这部分代码:

Sub frii()
fndList = Array("!", "@", "#", "$", "%", "~")

    For x = LBound(fndList) To UBound(fndList)
        Selection.Replace What:="~" & fndList(x), Replacement:=""
    Next x

End Sub

在:

enter image description here

后:

enter image description here