返回列表 上一主題 發帖

VBA如何把字串中的英文字前插入空格?

回復  samwang
請問Arr要怎樣才能抓跨行的資料呢?
因為在這行就出錯了
s13030029 發表於 2022-8-25 10:29


請再測試看看,謝謝
Sub test()
Dim Arr, a, T, i&, x, j%
Arr = Range([A1], [A65536].End(3).Offset(1, 0))
For i = 1 To UBound(Arr) - 1
    a = Split(Trim(Arr(i, 1)), Chr(10))
    For x = 0 To UBound(a)
        For j = 2 To Len(a(x))
            T = UCase(Mid(a(x), j))
            If Asc(T) > 64 And Asc(T) < 123 Then
                If Cells(i, 2) = "" Then
                    Cells(i, 2) = Replace(a(x), T, " " & T)
                Else
                    Cells(i, 2) = Cells(i, 2) & Chr(10) & Replace(a(x), T, " " & T)
                End If
            End If
        Next
    Next
Next
End Sub

TOP

回復 2# Andy2483


If Mid(j, x, 1) Like "[A-z]" Then  大小通吃~~

TOP

回復 12# 准提部林
    謝謝 前輩指導

TOP

回復 2# Andy2483


    修正自己的認知
j = Trim(Mid(j, 1, x - 1)) & " " & Mid(j, x, c * 2) 改為
j = Trim(Mid(j, 1, x - 1)) & " " & Mid(j, x)即可
當初學習時還在考慮 length 要用多少??  

https://learn.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/mid-function
Mid(string, start, [ length ])
string        必要。 字串運算式的功能是會從中傳回字元。 如果 字串 包含 Null,則傳回 Null。
start        必要;Long。 string 中要擷取做為開始部分的字元位置。 如果 start 中的字元數大於 string,則 Mid 會傳回零長度字串 ("")。
length        選用;Variant (Long)。 要傳回的字元數。 如果省略此參數,或是字元數少於文字中的 length 字元 (包含 start 所在的字元),就會傳回從 start 位置開始一直到字串結尾的所有字元。
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

        靜思自在 : 願要大、志要堅、氣要柔、心要細。
返回列表 上一主題