0

別のプログラムから取得している文字列セットが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を初めて使用するという事実をお許しください。

4

3 に答える 3

0

Splitを使用して、配列をセルに処理できます。SelectionオブジェクトにはTextToColumns関数もあります。

于 2012-12-19T15:13:47.313 に答える
0

これは、複数の区切り文字 splitを使用した投稿です。

そこからヒントが得られるかもしれません。

  • 行頭が数字で始まることを確認する
  • スペース、タブ、または区切り文字として使用している特定の文字で分割
  • 複数の区切り文字がある場合は、上記の方法を利用できます

あなたが試したことをコメントしてください。そこから喜んでお手伝いします。

于 2012-12-20T07:44:00.823 に答える
0

正規表現を使用します。Microsoft VBScript Regular Expressions 5.5fromへの参照を追加しますTools -> References。次に、次のようなコードを記述できます。

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
于 2012-12-20T09:07:26.270 に答える