清除文本中的数字并粘贴为外部工作簿中的值

时间:2019-02-05 07:15:39

标签: excel vba

我正在寻找我的项目的解决方案。 我有源数据workbook_src和目标数据worbook_destworkbook_src中的数字格式为="123456789"。我的目标是将workbook_src设为只读,将带有数字的行复制到内存中,将其清除为仅数字,然后将特殊值粘贴到workbook_dest作为值。

以只读方式打开workbook_src

Dim src As Workbook
Set src = Workbooks.Open("C:\xxx.xlsx", True, True)
ThisWorkbook.Activate

清除数字的功能

Function GetNumeric(CellRef As String)
    Dim StringLength As Integer
    StringLength = Len(CellRef)
    For i = 1 To StringLength
        If IsNumeric(Mid(CellRef, i, 1)) Then Result = Result & Mid(CellRef, i, 1)
    Next i 
    GetNumeric = Result
End Function

但是我无法在整个行上应用此功能

1 个答案:

答案 0 :(得分:0)

找到解决方案

Function cleanString(str As String) As String
    Dim ch, bytes() As Byte: bytes = str
    For Each ch In bytes
        If Chr(ch) Like "[A-Z.a-z 0-9]" Then cleanString = cleanString & Chr(ch)
    Next ch
End Function

Sub btn_CleanAll()
    Dim rng As Range

    For Each rng In Sheets("Sheet1").Range("A2:A30000").Cells
    rng.Value = cleanString(rng.Value)
    Next
    MsgBox "Done"
End Sub