excel/vba中用数字和冒号分隔的字符串分析

excel/vba中用数字和冒号分隔的字符串分析,vba,excel,Vba,Excel,我在VBA中设置了一个字符串,我正在从另一个程序中提取该字符串。当我将此数据拉入Excel时,其格式如下: EXAMPLE EXAMPLE EXAMPLE EXAMPLE EXAMPLE EXAMPLE EXAMPLE EXAMPLE 001: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE 002: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE 003: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE 使用我当前的V

我在VBA中设置了一个字符串,我正在从另一个程序中提取该字符串。当我将此数据拉入Excel时,其格式如下:

EXAMPLE EXAMPLE EXAMPLE EXAMPLE 
EXAMPLE EXAMPLE EXAMPLE EXAMPLE 

001: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE 

002: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE

003: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE 
使用我当前的VBA代码,单击一个表单控件,它会在我键入数据时将其放入单元格中。我想将其分开,因此当我单击控件时,它会将数据放入由数字分隔的单独单元格中。就是

EXAMPLE EXAMPLE EXAMPLE EXAMPLE 
EXAMPLE EXAMPLE EXAMPLE EXAMPLE 

001: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE 
进入第一个牢房

002: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE
进入相邻单元,并且

003: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE

进入下一个相邻的单元格,依此类推,不管我有多少个数字。我希望我已经充分地解释了我的处境,有人可以帮助我。请原谅我对VBA很陌生

您可以使用数组并将其处理到单元格中。Selection对象上还有一个TextToColumns函数。

这是我们使用的一篇文章

你可以从中得到一个想法

  • 检查以数字开头的行的开头
  • 按空格、制表符或任何特定字符分割,而不是按分隔符分隔
  • 如果有多个分隔符,可以使用上述方法
请用您尝试过的内容进行评论。很高兴从那里得到帮助。

使用。从
Tools->References
添加对
MicrosoftVBScript正则表达式5.5
的引用。然后您可以编写如下代码:

Public Function PasteValues()
Dim s As String, re As New RegExp
Dim matches As MatchCollection, m As Match

Dim rng As Range
'Destination workbook, worksheet within workbook, and starting cell
Set rng = ActiveWorkbook.Worksheets(1).Range("A1")

s = "EXAMPLE EXAMPLE EXAMPLE EXAMPLE " & Chr(13) & _
    "EXAMPLE EXAMPLE EXAMPLE EXAMPLE " & Chr(13) & _
    Chr(13) & _
    "001: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE " & Chr(13) & _
    Chr(13) & _
    "002: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE " & Chr(13) & _
    Chr(13) & _
    "003: EXAMPLE EXAMPLE EXAMPLE - EXAMPLE "

'Finds a sequence of non-digits (\D) followed by either 
    '1) a sequence of digits followed by a colon -- (\d*:)
    '2) the end of the string -- $
'The either/or is defined by the pipe -- |
re.Pattern = "(\D*)((\d*:)|$)"

'We want to match all instances, not just the first
re.Global = True

Set matches = re.Execute(s)
For Each m In matches
    'Each item in the SubMatches collection corresponds to a pair of parentheses.
    'e.g. m.SubMatches(0) returns the matched string corresponding to (\D*)
    'In this case, we aren't interested (I'm assuming) in the actual numbers, just that
    'they are there, but we could see them using SubMatches(1) or SubMatches(2)
    rng.Value = m.SubMatches(0)

    'Advance the range to the next column
    Set rng = rng.Offset(, 1)
Next
End Function

你能告诉我们你已经试过什么了吗?