我有一张表,其中包含 A 列中的“5670&&2”、“1281&&-3&-5&&7”等值范围。
请帮助我以下列方式提取 VBA 中的输出:
例如 5670&&2 我要求 A1 单元格包含 5670,B1 单元格包含 &&,C1 单元格包含 2。
对于例如 1281&&-3&-5&&7,我要求 A1 单元格包含 1281,B1 单元格包含 &&-,C1 单元格包含 3,D1 单元格包含 &-,E1 单元格包含 5,F1 单元格包含 &&,G1 单元格包含 7。
请帮忙。
谢谢。,
我有一张表,其中包含 A 列中的“5670&&2”、“1281&&-3&-5&&7”等值范围。
请帮助我以下列方式提取 VBA 中的输出:
例如 5670&&2 我要求 A1 单元格包含 5670,B1 单元格包含 &&,C1 单元格包含 2。
对于例如 1281&&-3&-5&&7,我要求 A1 单元格包含 1281,B1 单元格包含 &&-,C1 单元格包含 3,D1 单元格包含 &-,E1 单元格包含 5,F1 单元格包含 &&,G1 单元格包含 7。
请帮忙。
谢谢。,
在这里,我尝试编写代码来将数字与非数字分开。数字和非数字被复制到不同的列,例如 Excel 文本到列。代码有点疯狂,如果你需要我会提供评论。使用 ActiveSheet.UsedRange.Columns(1).Cells 作为输入。
Option Explicit
Sub SeparateNumbers()
Dim targetRange As Range
Dim cellRange As Range
Dim charIndex As Integer
Dim oneChar As String
Dim nextChar As String
Dim start As Integer
Dim copiedCharsCount As Integer
Dim cellValue As String
Dim columnIndex As Integer
Set targetRange = ActiveSheet.UsedRange.Columns(1).Cells
For Each cellRange In targetRange
columnIndex = cellRange.Column
start = 1
copiedCharsCount = 0
cellValue = cellRange.Value
If (VBA.Strings.Len(cellValue) <= 1) Then GoTo nextCell
For charIndex = 2 To Len(cellValue)
oneChar = VBA.Strings.Mid(cellValue, charIndex - 1, 1)
nextChar = VBA.Strings.Mid(cellValue, charIndex, 1)
If VBA.IsNumeric(oneChar) And VBA.IsNumeric(nextChar) Then GoTo nextCharLabel
If Not VBA.IsNumeric(oneChar) And Not VBA.IsNumeric(nextChar) Then GoTo nextCharLabel
cellRange.Offset(0, columnIndex).Value = VBA.Strings.Mid(cellValue, start, charIndex - start)
columnIndex = columnIndex + 1
copiedCharsCount = copiedCharsCount + (charIndex - start)
start = charIndex
nextCharLabel:
If charIndex = Len(cellValue) Then
cellRange.Offset(0, columnIndex).Value = VBA.Strings.Right(cellValue, charIndex - copiedCharsCount)
End If
Next charIndex
nextCell:
Next cellRange
End Sub
Here is one more code. As a side product, function TextSplitToNumbersAndOther can be used independently as a formula to achieve the same effect.
To prevent accidental firing of the macro in a wrong sheet or a wrong column and overwriting neighbouring columns with scrap, named range "Start_point" should be defined by a user. Below this range in the same column, all data will be processed till the first blank row.
Spreadsheet example: http://www.bumpclub.ee/~jyri_r/Excel/Extracting_symbols_into_columns.xls
Option Explicit
Sub ExtractSymbolsIntoColumns()
Dim rng As Range
Dim row_processed As Integer
Dim string_to_split As String
Dim columns_needed As Long
Dim counter As Long
row_processed = 1
counter = 0
Set rng = Range("Start_point")
While rng.Offset(row_processed, 0).Value <> ""
string_to_split = rng.Offset(row_processed, 0).Value
columns_needed = TextSplitToNumbersAndOther(string_to_split)
For counter = 1 To columns_needed
rng.Offset(row_processed, counter).Value = _
TextSplitToNumbersAndOther(string_to_split, counter)
Next
row_processed = row_processed + 1
Wend
End Sub
Function TextSplitToNumbersAndOther(InputText As String, _
Optional SplitPieceNumber As Long) As Variant
Dim piece_from_split(100) As Variant
Dim char_from_input As String
Dim word_count As Long
Dim counter As Long
Dim char_type(100) As Variant
InputText = Trim(InputText)
If Not IsNull(InputText) Then
word_count = 1
piece_from_split(word_count) = ""
For counter = 1 To Len(InputText)
char_from_input = CharFromTextPosition(InputText, counter)
char_type(counter) = CharTypeAsNumber(char_from_input)
If counter = 1 Then
piece_from_split(word_count) = char_from_input
Else
If (char_type(counter - 1) = char_type(counter)) Then
piece_from_split(word_count) = piece_from_split(word_count) & char_from_input
'Merge for the same type
Else
word_count = word_count + 1
piece_from_split(word_count) = char_from_input
End If
End If
Next
End If
If SplitPieceNumber = 0 Then
TextSplitToNumbersAndOther = word_count
Else
If SplitPieceNumber > word_count Then
TextSplitToNumbersAndOther = ""
Else
TextSplitToNumbersAndOther = piece_from_split(SplitPieceNumber)
End If
End If
End Function
Function CharTypeAsNumber(InputChar As String, Optional PositionInString As Long) As Long
If PositionInString = 0 Then PositionInString = 1
If Not IsNull(InputChar) Then
InputChar = Mid(InputChar, PositionInString, 1)
Select Case InputChar
Case 0 To 9
CharTypeAsNumber = 1
Case "a" To "z"
CharTypeAsNumber = 2
Case "A" To "Z"
CharTypeAsNumber = 3
Case Else
CharTypeAsNumber = 4
End Select
Else
CharTypeAsNumber = 0
End If
End Function
Function CharFromTextPosition(InputString As String, TextPosition As Long) As String
CharFromTextPosition = Mid(InputString, TextPosition, 1)
End Function
您可以编写一个 UDF(用户定义函数)来实现目标。您的两个示例按顺序(升序)过滤到 Excel 中的相邻列(A、B、C、D ...)
那么从逻辑上假设,您永远不会遇到必须将字符串分成不相邻列的情况是否正确?例如,1234 到 A,&& 到 C,3 到 D……导致 A、C、D。
假设 2:您的拆分字符串不需要超过 Excel 所能提供的列。
您可以尝试的步骤: 1. 检查您的字符串是否为空 2. 将其拆分为数字以外的字符 3. 在每个非数字字符的开头和结尾处,您可以继续到下一个相邻列。
搜索帮助:在 Excel 中将字符串拆分为多列 - VBA