我有一些看起來像這樣的資料(超過 400 列):
| 年 | ID | fake_num1 | fake_num2 | 文本1 |
|---|---|---|---|---|
| 2019年 | 11 | 36 000 | 10,000 | 文本1 |
| 2020年 | 12 | -1 275 | 1 000,00 | 正文 2 |
列 fake_num1 和 fake_num2 存盤為文本。我想要實作的是
- 識別那些假數字列
- 使用 for 回圈清理資料(例如洗掉空格、列、用點替換逗號)
我需要步驟 1 的一些幫助。我必須識別列 fake_num1 和 fake_num2,同時避免像 text1 這樣的列。我正在考慮使用正則運算式,但也許還有另一種解決方案。
我在這里使用了部分代碼:SO regexp,但是我不確定如何從那里開始。
Dim strPattern as String: strPattern = "^[0-9]$"
會找到任何以數字開頭和結尾的東西,并且只有數字(如果我的理解是正確的)。管理上表所列案例的最佳方法是什么?
uj5u.com熱心網友回復:
請嘗試下一個代碼,它將“假數字列”視為替換必要字符使字串成為數字的那些:
Sub testMakeNumbers()
Dim sh As Worksheet, lastR As Long, lastCol As Long, i As Long, rngCol As Range
Set sh = ActiveSheet 'you can use here the necessary sheet
lastR = sh.Range("A" & sh.rows.Count).End(xlUp).row
lastCol = sh.cells(1, Columns.Count).End(xlToLeft).Column
'determine the problematic columns:
For i = 1 To lastCol
If Not IsNumeric(sh.cells(2, i).Value) And _
IsNumeric(Replace(Replace(Replace(sh.cells(2, i).Value, " ", ""), "'", ""), ",", ".")) Then
If rngCol Is Nothing Then
Set rngCol = sh.cells(2, i)
Else
Set rngCol = Union(rngCol, sh.cells(2, i))
End If
End If
Next
'replace the characters making the string as number:
With Intersect(rngCol.EntireColumn, sh.Range("A2", sh.cells(lastR, lastCol)))
.Replace ",", "."
.Replace Chr(160), ""
.Replace " ", ""
.Replace "'", ""
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/396628.html
下一篇:使用Regex回傳第一次出現
