- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道问这个问题很奇怪。但我所面临的情况并不那么奇怪。
我有一些长字符串(大约 1000 个字符或更多 - 更新:抱歉,不是 1000,而是大约 39000,我的错)。它们包含我想要修剪的空格。
根据常识,我使用 Worksheetfunction.Trim
来完成这项工作。它适用于一些短字符串(大约 500 个字符)。但是,随着字符串变大(超过 39000 个字符),它不断返回错误 '1004' - 无法获取工作表函数类的修剪属性
有疑问,我在工作表中使用长字符串执行了一些测试。我在一个单元格中输入了一个虚拟字符串,如“aaaaaabbbbbbcccc...”,并在另一个单元格中输入了 =TRIM(string)
。有用。它在工作表中如何工作,但在 VBA 中却不起作用?我有点困惑。
为了完成这项工作,我制作了自己的 TRIM 函数作为解决方法。但我仍然想知道 worksheetfunction.Trim
发生了什么。 Worksheetfunction.Trim
的限制是什么?
感谢任何帮助。 :)
这是我的代码:
我使用以下函数:get_address(wks as worksheet) as string
:获取包含常量和公式形式数据的所有范围的地址。
EXNUM(TextIn as string,可选分隔符as string = "") as string
:从字符串中删除所有非数字字符
首先,我将使用 get_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
这是错误和字符串长度的屏幕截图
更新: @brettdj:这就是我正在做的事情。这是一个相当简单的想法。我想创建一个名为 DetectSizeX 的函数。我输入一个工作表或一个范围,DetectSizeX 将返回较小范围的地址,其中包含较大范围/工作表中的所有数据。
例如:DetectSizeX(Activesheet)
==> 返回"A3:T3568"
我的函数的工作原理如下:
第 1 步:使用以下方法检测包含所有数据的分段范围:
Cells.SpecialCells(xlCellTypeConstants)
Cells.SpecialCells(xlCellTypeConstants)
第2步:获取上面得到的大范围内所有分片范围的地址。将所有地址连接成一个字符串。称之为r_address
。
r_address
看起来像“A1、B33:C88、T6:Z90、K7:Z100...”第3步:获取左上角和右下角单元格的地址
r_address 字符串中最大的数字代表最后一行。r_address
字符串中最小的数字代表第一行。
还有
r_address
中的“最大”列名称(如 A、B、AA、AZ)代表最后一列r_address
中的“最小”列名称代表第一列。
连接(最小的列名称,最小的数字)
和连接(最大的列名称,最大的数字)
给我两个单元格的地址,我可以用它们来确定 DetectSizeX 结果的范围
这是我的完整代码,供感兴趣的人使用,它很长:欢迎并赞赏任何建议和改进:)
'====================================
'**********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*********
'====================================
最佳答案
WorksheetFunction
受到与工作表相同的约束的限制。对于单元格中的字符串,该长度为 32767 个字符(如 user3964075 所评论)。
你最好的选择是推出你自己的修剪功能,像这样
Function MyTrim(s As String) As String
Do While InStr(s, " ") > 0
s = Replace$(s, " ", " ")
Loop
MyTrim = Trim$(s)
End Function
就性能而言,这实际上比 WorksheetFunction.Trim
稍快 (10%)(在 30,000 个字符的字符串上进行测试)
关于string - 如何在VBA中使用worksheetfunction.Trim很长的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28494005/
这个问题在这里已经有了答案: Isn't an Int64 equal to a long in C#? (2 个答案) 关闭 9 年前。 它不应该是一个整数类型吗? 这样,一些使用 int 的函数
当我遇到一些我想知道的事情时,我正忙着解析一个二进制文件。 Stream.Position属性的类型为Int64或long。为什么是这样?因为流中的位置不能为负,所以使用 UInt64 不是更有意义吗
所以第一部分是我从用户那里获得输入,在本例中,输入是“1”作为从另一个函数接收的字符值。 printf ("\nPlease enter 1, 2, 3 or q: "); option =
我正在尝试使用以下代码从 REST 服务返回 JSON: $categories = $categoriesController->listAll(); if($categories){ hea
我阅读了文档,它说 long 是 %li,但打印输出返回为 -2147024891。是什么赋予了? 最佳答案 您甚至没有提供要打印的号码,但我猜您已经无意中发现了签名打印和未签名打印之间的区别。 使用
我正在创建自定义购物车,我正在构建一个查询,该查询从检索我刚刚保存到购物车表中的 session_id 开始。我知道这个值被保存了,我在 mysql 命令行运行这个查询,它返回我需要的但我没有将值放入
我有一个包含 textView 的 scrollView。如果文本很长并且不适合屏幕,我想增加 textView 高度(我想我可以通过添加 NSLayoutConstraint outlet 并修改它
我有一个基本的数据库处理程序类,其中有一个使用 PDO::FETCH_ASSOC 参数返回结果集的公共(public)方法: public function resultSet() { $th
在后台线程中,我调用 PublishSubject.onNext(); 并在主线程中通过 subscribe(PublishSubject.filter(message -> message.getI
我想知道为什么 Amazon Web Services 控制台登录页面有这么长的 url?为什么不只发布数据而不显示其中包含大量数据的冗长 url。以这种方式实现有什么充分的理由吗? 最佳答案 我认为
这个问题在这里已经有了答案: Can I mix MySQL APIs in PHP? (4 个答案) 关闭 6 年前。 希望我犯了一个快速而明显的错误,我浏览了 previous question
我得到了答案:如果我禁用了cookie,那么使用URL重定向我可以传递JSESSIONID,但我的URL已经很长,因为我使用它有约束的GET方法。那怎么办我应该使用我的 session 吗?我希望我的
目前,当我使用 DOMDocument 对象并调用 saveHTML() 时,它会自动添加一些我不需要的 html 标签。我尝试了此处建议的解决方案 ( https://stackoverflow.c
我是一名优秀的程序员,十分优秀!