我目前正在使用VBA工具,该工具会自动在Excel文档中为其中的所有样式设置标题和底线,打开文档后即会打开,用户可以从其他文档中导入标题和底线,并将其写在gui窗口中的字段。为了避免用户可能只需要进行很小的更改就不必键入所有内容,该窗口已包含了当前文档中的所有值。
我的问题是,现在我不知道如何将输入的字符串拆分为格式部分和实际显示的文本部分(不必自己将格式部分拆分为格式)。
到现在为止,我一直使用Replace()来简单擦除已知格式的部分,并在末尾手动添加它们,但这并不能将其用于各种标题。
使用For循环在&符号处剪切传入字符串的选项失败,因为用户曾经可以使用带有&符号的Titles,并且最后一种格式确实直接以实际文本继续(&K03 + 039附录,将颜色变量引到标题部分)附录)
我也不能使用表本身的任何部分,因为这对于所有类型的表也是通用的。
所以我想知道是否有一种方法可以将标题字符串拆分为实际的文本部分和格式部分。
我可以想到的每种格式的输入字符串都是:&“ Algerian,Fett”&14&S&X&K03 + 039附录C-Opera,而附录C-Opera是标题。
谢谢您的提前帮助
好的,我现在为我的问题开发了一种解决方案,该解决方案对我有用,虽然不是很漂亮但是可以起作用。
它可以将给定的字符串拆分为格式化部分和文本部分,但是在标题中不能与&&一起使用,因此在使用此方法之前需要加以注意。
它也不知道页码的格式,因为我不需要它,但是可以很容易地解决。
Public FullLine As SeperatedLineParts
私有函数InformationHandler(文本作为字符串)Set FullLine = New SeperatedColumnParts Dim param as String Dim textPart As String Dim co As Integer
'Stops the function when "text" is empty
If text = "" Then
FullLine.text = text
FullLine.format = ""
Exit Function
End If
'Splits the incoming "text" in single Symbols
Dim singleSymbols() As String
ReDim singleSymbols(Len(text) - 1)
For co = 1 To Len(text)
singleSymbols(co - 1) = Mid$(text, co, 1)
Next
'switch case variables, helper for seperating the "text"
Dim isParam As String ' Boolean Value as a String, to use it for the switch cases
Dim wasText As Boolean ' Remembers if the last part was a textpart or a format part, if it was a text and now a format part is following, they will be sepperated
isParam = "False" ' Remembers if the current Symbol is a textpart or a format part
wasText = False
FullLine.parts = 1 ' remembers the number of parts with different formats
' This loop splits Format and Textparts and writes a Chr(3)||Chr(2) Marker between the different formatted parts
For co = 0 To UBound(singleSymbols)
Select Case singleSymbols(co) + isParam
Case Chr(38) + "False" ' If a & symbol appears it will start a new parameter except if another & follows (&&)
If singleSymbols(co + 1) = Chr(38) Then
textPart = textPart + "&&"
co = co + 1
isParam = "False"
Else
If wasText = True Then ' If a & follows after a textsegment a new part will be created except when another & follows (&&)
textPart = textPart + Chr(3) + "||" + Chr(2) ' Chr(3) and Chr (2) arte the permitter and that split a following formatpart from a leading textpart
param = param + Chr(3) + "||" + Chr(2)
FullLine.parts = FullLine.parts + 1
wasText = False
End If
isParam = "True"
param = param + "&"
End If
' Identifies all single letter commands
Case "L" + "True", "C" + "True", "R" + "True", "I" + "True", "E" + "True", "X" + "True", "Y" + "True", "B" + "True", "U" + "True", "S" + "True", "D" + "True", "T" + "True", "F" + "True", "A" + "True", "N" + "True", "Z" + "True", "G" + "True"
param = param + singleSymbols(co)
isParam = "False"
' Identifies all size commands
Case "1" + "True", "2" + "True", "3" + "True", "4" + "True", "5" + "True", "6" + "True", "7" + "True", "8" + "True", "9" + "True"
param = param + singleSymbols(co)
If IsNumeric(singleSymbols(co + 1)) Then
co = co + 1
param = param + singleSymbols(co)
End If
isParam = "False"
Case Chr(34) + "True" ' If the " symbol appears at the beginning of a command it will be a longer command till another " symbol ends it
Dim coun As Integer
param = param + singleSymbols(co)
For coun = co + 1 To UBound(singleSymbols)
param = param + singleSymbols(coun)
If singleSymbols(coun) = Chr(34) Then
co = coun
coun = UBound(singleSymbols)
End If
Next
isParam = "False"
Case "K" + "True" ' Identifies all color commands
param = param + singleSymbols(co)
For coun = 1 To 6
param = param + singleSymbols(coun + co)
Next
co = co + 6
isParam = "False"
Case Else ' Identifies all normal text symbols
textPart = textPart + singleSymbols(co)
wasText = True
End Select
Next
' Writes the format and the text parts into the "FullLine" object, wich now contains these parameters as well as the number of parts
FullLine.format = param
FullLine.text = text
End Function
'Function for making a string out of an array while shorten it at the beginning for the given amount
Private Function ArrToString(inpu() As String, shortenBy As Integer) As String
Dim i As Integer
For i = shortenBy + 1 To UBound(inpu)
ArrToString = ArrToString + inpu(i)
Next
End Function
我知道这是一个庞然大物,可能会伤害每个经验丰富的程序员,但这是迄今为止我能想到的最好的方法。
编辑:仍然存在一个问题,即如果文本中包含格式符号,则仅将最后一个文本部分作为文本,并且格式部分之间的所有内容也将被视为“格式”。
有没有更有效的方法来完成此任务。
编辑:重新设计了整个功能,现在可以处理文本和&&中的格式更改,并且注释也得到了改进,以使其对其他用户可更改。
还要编写一个具有以下内容的类模块:
Option Explicit
Public parts As Integer
Public format As String
Public text As String
使该功能正常工作。
编辑:Chr(3)和Chr(4)符号在这一侧不起作用,并且您在输出中应看到的示例注释没有显示它们,因此您的结果将与此处显示的不同。不管怎样,这些符号都是excel中任何标题都不太可能使用的符号,可以在不对其余代码进行任何进一步更改的情况下对其进行更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句