iT邦幫忙

0

將儲存格中的資料擷取數字部分並加總

  • 分享至 

  • xImage

大家好
因為網路上找不到適當的範例 所以又在這裡求救啦
我有一個excel檔,每個儲存格都有文字與數字,數字又有千位逗點
我現在需要把每一列儲存格的所有數字擷取出來並加總

請問要如何撰寫VBA 或是現有ecel公式可以解決

高手路過 別錯過 先謝謝各位不吝分享

https://ithelp.ithome.com.tw/upload/images/20200430/20118293waOVIK8hh9.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-04-30 17:43:01
最佳解答

假設文字在 A1
把游標停在 B1 的地方
執行巨集 main
成功的話會在 C1,D1(,E1)出現數字
不成功的話
就另請高明

Sub Main()
    Dim str, sOneChar As String
    Dim sNumber As String
    Dim isNumber As Boolean
    Dim nCount As Integer
    nCount = 0
    isNumber = False
    sNumber = ""
    str = ActiveCell.Offset(0, -1)
    For nI = 1 To Len(str)
        sOneChar = Mid(str, nI, 1)
        If isYourNumber(sOneChar) Then
            sNumber = sNumber & sOneChar
            isNumber = True
        Else
            If isNumber Then
                ActiveCell.Offset(0, nCount).Value = stringToValue(sNumber)
                nCount = nCount + 1
                sNumber = ""
                isNumber = False
            End If
        End If
    Next nI
End Sub
Function isYourNumber(ByVal ch As String) As Boolean
    isYourNumber = False
    If ch = "0" Or ch = "1" Or ch = "2" Or ch = "3" Or ch = "4" Or ch = "5" Or ch = "6" Or ch = "7" Or ch = "8" Or ch = "9" Or ch = "," Or ch = "." Then
        isYourNumber = True
    End If
End Function
Function stringToValue(ByVal str As String) As Integer
    Dim s As String
    s = Replace(str, ",", "")
    stringToValue = Val(s)
End Function
看更多先前的回應...收起先前的回應...

太感謝了 有一點bug 但是我修了一下就可以運行了
就是把 dim s as string 改成 long

謝謝喽

如果 OK 了就選個最佳解答把問題結案
還有你之前的兩個問題也是

https://ithelp.ithome.com.tw/upload/images/20200430/20118293JGUow6MY2X.png

海綿寶寶 你好
感謝不吝分享代碼
因為上面的代碼只能對作用中儲存格起作用
我改寫如下可以批次產生 但是擷取出來的數字像階梯般排列 不知要怎麼修才可以齊頭式排列


Sub Main()
Dim str, sOneChar As String
Dim sNumber As String
Dim isNumber As Boolean
Dim nCount As Integer
Dim j As Integer
nCount = 0
isNumber = False
sNumber = ""
For j = 1 To 7
str = Cells(j, "B").Offset(0, -1)
For nI = 1 To Len(str)
sOneChar = Mid(str, nI, 1)
If isYourNumber(sOneChar) Then
sNumber = sNumber & sOneChar
isNumber = True
Else
If isNumber Then
Cells(j, "B").Offset(0, nCount).Value = stringToValue(sNumber)
nCount = nCount + 1
sNumber = ""
isNumber = False
End If
End If
Next nI
Next j
End Sub

Function isYourNumber(ByVal ch As String) As Boolean
isYourNumber = False
If ch = "0" Or ch = "1" Or ch = "2" Or ch = "3" Or ch = "4" Or ch = "5" Or ch = "6" Or ch = "7" Or ch = "8" Or ch = "9" Or ch = "," Or ch = "." Then
isYourNumber = True
End If
End Function

Function stringToValue(ByVal str As String) As Long
Dim s As String
s = Replace(str, ",", "")
stringToValue = Val(s)
End Function

nCount=0搬個位置即可

Dim j As Integer
nCount = 0
isNumber = False
sNumber = ""
For j = 1 To 7
str = Cells(j, "B").Offset(0, -1)
For nI = 1 To Len(str)
sOneChar = Mid(str, nI, 1)

改成

Dim j As Integer
isNumber = False
sNumber = ""
For j = 1 To 7
nCount = 0
str = Cells(j, "B").Offset(0, -1)
For nI = 1 To Len(str)
sOneChar = Mid(str, nI, 1)

試試看

1
japhenchen
iT邦超人 1 級 ‧ 2020-04-30 16:02:53

說真的我不喜歡用VBA來跑,特別是那種上千上萬列的檔案,還要搭巨集真的會讓人受不了

如果你的EXCEL是2007版之後的XLSX檔,建議你改用VisualStudio VB.NET或C#,配EPPLUS庫(免費)來對EXCEL做建立/直接讀寫,效能真的高很多很多很多很多很多很多很多很多......

(以下為私人專案,分大家看無所謂)
https://ithelp.ithome.com.tw/upload/images/20200430/20117954nO4yzz7J96.jpg
https://ithelp.ithome.com.tw/upload/images/20200430/20117954I5WXHdmJlG.jpg
https://ithelp.ithome.com.tw/upload/images/20200430/20117954FaXBPNRDUS.jpg

看更多先前的回應...收起先前的回應...

我的目的只是把這個EXCEL檔(GOOGLE試算表匯出的)做加總統計而且製表,如果用人工跑,會要好幾個工作天,有程式跑,幾十秒
https://ithelp.ithome.com.tw/upload/images/20200430/20117954zdJBclcW39.jpg
https://ithelp.ithome.com.tw/upload/images/20200430/20117954J4smvjMaZB.jpg

謝謝 您的幫忙 因為手邊沒有VS 所以可能要想其他的辦法

ccutmis iT邦高手 2 級 ‧ 2020-04-30 19:12:37 檢舉

我是建議用Python...
自從改用Python存取excel後我都覺得用VBA存取excel是在找罪受

VS有免費的版本可用啊,一樣可以使用NUGET裡的EPPLUS,不用擔心授權的事,放心用下去
Visual Studio Community

至於PYTHON也有很不錯的EXCEL解決方案,看你自己的喜好決定
openpyxl
總之,用VBA真的是找罪受,另加巨集的安全性顧慮

我要發表回答

立即登入回答