Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在VBA中修剪很长的字符串?_String_Vba_Excel - Fatal编程技术网

String 如何在VBA中修剪很长的字符串?

String 如何在VBA中修剪很长的字符串?,string,vba,excel,String,Vba,Excel,我知道问这个问题很奇怪。但我所面对的并不是那么奇怪 我有一些长字符串(大约1000个字符或更多——更新:对不起,不是1000个,而是39000个左右,我的错)。它们包含我要修剪的空间 根据常识,我使用了工作表function.Trim来完成这项工作。它使用了一些短字符串(大约500个字符)。但是,随着字符串变大(超过39000个字符),它不断返回错误'1004'-无法获取worksheetfunction类的trim属性 毫无疑问,我在工作表中使用长字符串执行了一些测试。我在一个单元格中输入了一

我知道问这个问题很奇怪。但我所面对的并不是那么奇怪

我有一些长字符串(大约1000个字符或更多——更新:对不起,不是1000个,而是39000个左右,我的错)。它们包含我要修剪的空间

根据常识,我使用了
工作表function.Trim
来完成这项工作。它使用了一些短字符串(大约500个字符)。但是,随着字符串变大(超过39000个字符),它不断返回错误
'1004'-无法获取worksheetfunction类的trim属性

毫无疑问,我在工作表中使用长字符串执行了一些测试。我在一个单元格中输入了一个伪字符串,如“aaaaaa bbbbbb cccc…”,并在另一个单元格中输入了它。它起作用了。 它如何在工作表中工作,而不是在VBA中。我有点困惑

为了完成这项工作,我制作了自己的修剪功能作为解决方法。但是我仍然想知道
worksheetfunction.Trim
发生了什么。
工作表功能.Trim的限制是什么

感谢您的帮助。:)

这是我的密码:

我使用以下功能:
get_address(wks作为工作表)作为字符串:以常量和公式的形式获取包含数据的所有范围的地址

EXNUM(TextIn为字符串,可选分隔符为string=”“)为字符串
:从字符串中删除所有非数字字符

首先,我将使用
get\u address
获取范围地址,然后使用
EXNUM
获取地址。 然后我将对EXNUM的结果运行
worksheetfunction.trim

Function get_address(wks As Worksheet) As String

    '***Find the range***
    Dim rs1 As range, rs2 As range

    On Error Resume Next
    Set rs1 = wks.Cells.SpecialCells(xlCellTypeConstants)

        If Err.Number <> 0 Then
           Set rs1 = Nothing
        End If

    Set rs2 = wks.Cells.SpecialCells(xlCellTypeFormulas)

        If Err.Number <> 0 Then
            Set rs2 = Nothing
        End If

    '***Extract range address***
    Dim ad1 As String, ad2 As String
    Dim result As String

    ad1 = area_address(rs1)
    ad2 = area_address(rs2)

    result = ad1 & "," & ad2

    If Right(result, 1) = "," Then
        result = Left(result, Len(result) - 1)
    End If

    get_address = result
End Function



Function EXNUM(TextIn As String, _
                Optional separator As String = " ") As String

Dim x As Double
Dim result As String

    For x = 1 To Len(TextIn)
            If Not IsNumeric(Mid(TextIn, x, 1)) Then
                result = result + separator
            Else
                result = result + Mid(TextIn, x, 1)
            End If
    Next x

    If Len(result) >= 1 And Right(result, 1) = separator Then
        result = Left(result, Len(result) - 1)
    End If

EXNUM = result
End Function

'**********Supporting function only************

Public Function area_address(r As range) As String

    Dim x As Double
    Dim result As String

    For x = 1 To r.Areas.count

    result = result + r.Areas.Item(x).address(rowabsolute:=False, columnabsolute:=False) + ","
    Next x

    If Right(result, 1) = "," Then
        result = Left(result, Len(result) - 1)
    End If
    'Debug.Print r.Areas.count
    area_address = result
End Function
步骤2:获取从上面获取的大范围内所有碎片范围的地址。将所有地址合并为一个字符串。叫它
r\u address

r_地址
看起来像“A1,B33:C88,T6:Z90,K7:Z100…” 步骤3:获取左上角和右上角单元格的地址

r\u address
字符串中的最大数字表示最后一行。
r\u address
字符串中的最小数字表示第一行

r\u address
中的“最大”列名称(如A、B、AA、AZ)表示最后一列
r\u address
中的“最小”列名称表示第一列

Concatenate(最小列名称,最小数字)
Concatenate(最大列名称,最大数字)

给我两个单元格的地址,我可以用它来确定DetectSizeX结果的范围

以下是我的完整代码,供感兴趣的人使用,非常长: 欢迎并感谢任何建议和改进:)

'====================================
“*********检测大小V6*************
'====================================
公共函数将Sizex_v6(工作表作为工作表,可选r_ad作为字符串=vbNullString)检测为字符串
'**注意:如果DetectSizeX_v5返回字符串“0”,则表示错误,应跳过该工作表
作为字符串的Dim地址
调暗左上角为字符串
Dim bot_右键为字符串
将最大行尺寸调整为双精度
Dim min_num为双精度
作为字符串的Dim max_col
作为字符串的Dim min_col
如果r_ad=vbNullString,则
地址=获取地址(工作表)
其他的
地址=获取地址范围(工作表,r\U ad)
如果结束
如果Len(地址)>0,则
最大行=获取行(地址,真)
min\u num=获取行(地址,False)
max\u col=get\u col\u name(地址,True)
min\u col=get\u col\u name(地址,False)
左上=最小列和最小数
bot\u right=最大列和最大行
DetectSizeX_v6=左上&“:”右下
其他的
DetectSizeX_v6=“0”
如果结束
端函数
“************在这里获取您的地址*********************
公共函数get_address(作为工作表工作)作为字符串
“***查找范围***
调暗rs1作为范围,rs2作为范围
出错时继续下一步
设置rs1=wks.Cells.SpecialCells(xlCellTypeConstants)
如果错误号为0,则
设置rs1=无
如果结束
设置rs2=wks.Cells.SpecialCells(xlCellTypeFormulas)
如果错误号为0,则
设置rs2=无
如果结束
'***提取范围地址***
尺寸ad1为字符串,ad2为字符串
将结果变暗为字符串
ad1=区域地址(rs1)
ad2=区域地址(rs2)
结果=ad1&“,”和ad2
如果Right(result,1)=“,”则
结果=左(结果,透镜(结果)-1)
如果结束
获取地址=结果
端函数
公共功能区\地址(r为范围)为字符串
将x调为双精度
将结果变暗为字符串
对于x=1到r.Areas.count
结果=结果+r.Areas.Item(x).地址(行绝对值=False,列绝对值=False)+“,”
下一个x
如果Right(result,1)=“,”则
结果=左(结果,透镜(结果)-1)
如果结束
区域\地址=结果
端函数
公共函数get_address_range(wks作为工作表,r_ad作为字符串)作为字符串
“***查找范围***
调暗rs1作为范围,rs2作为范围
出错时继续下一步
设置rs1=wks.range(r_ad).SpecialCells(xlCellTypeConstants)
如果错误号为0,则
设置rs1=无
如果结束
设置rs2=wks.range(r_ad).SpecialCells(xlCellTypeFormulas)
如果错误号为0,则
设置rs2=无
如果结束
'***提取范围地址***
尺寸ad1为字符串,ad2为字符串
将结果变暗为字符串
ad1=rs1.address(rowabsolute:=False,columnabsolute:=False)
ad2=rs2.address(rowabsolute:=False,columnabsolute:=False)
结果=ad1+“,”+ad2
如果Right(result,1)=“,”则
结果=左(结果,透镜(结果)-1)
如果结束
获取地址范围=结果
端函数
'*******支持功能*******
'*******用于DetectSizeX_v6*****
公共函数get_col_name(ByVal地址作为字符串,max_min作为布尔值)
“****从地址+清理地址中提取列名****
'address=“D2:D7,G8,B2:B9,F7:F9,C2:C10,E2:E13,B13:D13”
'注意:如果get_col_name返回字符串“0”,则表示错误
地址=extextext(地址)
地址=
Cells.SpecialCells(xlCellTypeConstants)
Cells.SpecialCells(xlCellTypeConstants)
'====================================
'**********Detectsize V6*************
'====================================

Public Function DetectSizeX_v6(WorkSheetIn As Worksheet, Optional r_ad As String = vbNullString) As String
'**Note: if DetectSizeX_v5 return a string "0", it means an error, should skip that worksheet
    Dim address As String
    Dim top_left As String
    Dim bot_right As String

    Dim max_row As Double
    Dim min_num As Double
    Dim max_col As String
    Dim min_col As String

    If r_ad = vbNullString Then
        address = get_address(WorkSheetIn)
    Else
        address = get_address_range(WorkSheetIn, r_ad)
    End If

    If Len(address) > 0 Then
        max_row = get_row(address, True)
        min_num = get_row(address, False)

        max_col = get_col_name(address, True)
        min_col = get_col_name(address, False)

        top_left = min_col & min_num
        bot_right = max_col & max_row

        DetectSizeX_v6 = top_left & ":" & bot_right
    Else
        DetectSizeX_v6 = "0"
    End If
End Function

'*************GET_ADDRESS HERE*********************

Public Function get_address(wks As Worksheet) As String

    '***Find the range***
    Dim rs1 As range, rs2 As range

    On Error Resume Next
    Set rs1 = wks.Cells.SpecialCells(xlCellTypeConstants)

        If Err.Number <> 0 Then
           Set rs1 = Nothing
        End If

    Set rs2 = wks.Cells.SpecialCells(xlCellTypeFormulas)

        If Err.Number <> 0 Then
            Set rs2 = Nothing
        End If

    '***Extract range address***
    Dim ad1 As String, ad2 As String
    Dim result As String

    ad1 = area_address(rs1)
    ad2 = area_address(rs2)

    result = ad1 & "," & ad2

    If Right(result, 1) = "," Then
        result = Left(result, Len(result) - 1)
    End If

    get_address = result

End Function

Public Function area_address(r As range) As String

    Dim x As Double
    Dim result As String

    For x = 1 To r.Areas.count
        result = result + r.Areas.Item(x).address(rowabsolute:=False, columnabsolute:=False) + ","
    Next x

    If Right(result, 1) = "," Then
        result = Left(result, Len(result) - 1)
    End If
    area_address = result
End Function

Public Function get_address_range(wks As Worksheet, r_ad As String) As String

'***Find the range***
Dim rs1 As range, rs2 As range

On Error Resume Next
Set rs1 = wks.range(r_ad).SpecialCells(xlCellTypeConstants)

    If Err.Number <> 0 Then
       Set rs1 = Nothing
    End If

Set rs2 = wks.range(r_ad).SpecialCells(xlCellTypeFormulas)

    If Err.Number <> 0 Then
        Set rs2 = Nothing
    End If

'***Extract range address***
Dim ad1 As String, ad2 As String
Dim result As String

ad1 = rs1.address(rowabsolute:=False, columnabsolute:=False)
ad2 = rs2.address(rowabsolute:=False, columnabsolute:=False)

result = ad1 + "," + ad2

If Right(result, 1) = "," Then
    result = Left(result, Len(result) - 1)
End If

get_address_range = result

End Function

'******SUPPORTING FUNCTION*******
'*********For DetectSizeX_v6*****
Public Function get_col_name(ByVal address As String, max_min As Boolean)

'****Extract column name from address + cleaning address****
'address = "D2: D7 , G8, B2: B9 , F7: F9 , C2: C10 , E2: E13 , B13: D13"
'Note: if get_col_name return string "0", it means an error
address = EXTEXT(address)
address = Replace(address, ",", " ")
address = Replace(address, ":", " ")
address = EXNONBLANK(address)

'***Split address into individual string***
    Dim arr() As String
    arr = Split(address, " ")

'***Convert column names into index***
    Dim x As Double
    Dim arr_size As Double
    Dim arr_num() As Double

    arr_size = UBound(arr)
    ReDim arr_num(0 To arr_size)

    For x = 0 To arr_size
        arr_num(x) = col_num(arr(x))
    Next x

'***Extract the max and the min col name/char***
    Dim temp_num As Double
    Dim max_char As String
    Dim min_char As String

        '***Max:
        temp_num = Application.WorksheetFunction.Max(arr_num)
        For x = 0 To arr_size
            If arr_num(x) = temp_num Then
                Exit For
            End If
        Next x
        max_char = arr(x)

        '***Min:
        temp_num = Application.WorksheetFunction.Min(arr_num)
        For x = 0 To arr_size
            If arr_num(x) = temp_num Then
                Exit For
            End If
        Next x
        min_char = arr(x)

'***Return value***
If max_min Then
    get_col_name = max_char
Else
    get_col_name = min_char
End If

End Function

Public Function get_row(ByRef address As String, max_min As Boolean)

Dim x As Double
Dim max_ad As String, min_ad As String
Dim max_row As Double, min_row As Double

For x = Len(address) To 1 Step -1
    If Mid(address, x, 1) = "," Then
        max_ad = Right(address, Len(address) - x)
        Exit For
    End If
Next x

For x = 1 To Len(address)
    If Mid(address, x, 1) = "," Then
        min_ad = Left(address, x - 1)
        Exit For
    End If
Next x

max_ad = EXNONBLANK(EXNUM(max_ad))
min_ad = EXNONBLANK(EXNUM(min_ad))

'***get_max_min

Dim arr() As String
Dim arr_val() As Double
Dim arr_size As Double

arr = Split(max_ad + " " + min_ad, " ")
arr_size = UBound(arr, 1)
ReDim arr_val(0 To arr_size)

For x = 0 To UBound(arr, 1)
    arr_val(x) = Val(arr(x))
Next x

max_row = Application.WorksheetFunction.Max(arr_val)
min_row = Application.WorksheetFunction.Min(arr_val)

If max_min Then
    get_row = max_row
Else
    get_row = min_row
End If

End Function
Public Function EXTEXT(TextIn As String, _
                Optional separator As String = " ") As String

Dim x As Double 'for long text
Dim result As String

    For x = 1 To Len(TextIn)
            If IsNumeric(Mid(TextIn, x, 1)) Then
                result = result + separator
            Else
                result = result + Mid(TextIn, x, 1) + separator
            End If
    Next x

If Len(result) >= 1 And Right(result, 1) = separator Then
    result = Left(result, Len(result) - 1)
End If

EXTEXT = result
End Function
Public Function EXNUM(TextIn As String, _
                Optional separator As String = " ") As String

Dim x As Double
Dim result As String

    For x = 1 To Len(TextIn)
            If Not IsNumeric(Mid(TextIn, x, 1)) Then
                result = result + separator
            Else
                result = result + Mid(TextIn, x, 1)
            End If
    Next x

    If Len(result) >= 1 And Right(result, 1) = separator Then
        result = Left(result, Len(result) - 1)
    End If

EXNUM = result
End Function

'***Convert col_name to col_number
Public Function col_num(col_name As String)
    col_num = range(col_name & 1).Column
End Function
'***End Convert col_name to col_number


Function EXNONBLANK(str As String) As String
Do While InStr(str, "  ") > 0
    str = Replace$(str, "  ", " ")
Loop
EXNONBLANK = trim$(str)
End Function
'====================================
'**********End Detectsize V6*********
'====================================
Function MyTrim(s As String) As String
    Do While InStr(s, "  ") > 0
        s = Replace$(s, "  ", " ")
    Loop
    MyTrim = Trim$(s)
End Function