0

I am looking for some code that can search cell by cell in the 2nd column of a table for numbers and decimal points, cut them and paste them in the cell to the left whilst leaving the text behind.

For example:

    • 1(tab space)Test
    • 1.1(tab space)Test
    • 1.1.1(tab space)Test
    • 1.1.1.1(tab space)Test

Where the bullet points represent separate cells in different columns.

In all instances the numbers are separated from the text by a tab space "Chr9" (as indicated in the example)

Any help or useful snippets of code would much appreciated!

EDIT: I have some code that scans each cell in a column but I dont know the code to tell it to only cut numbers and decimal points up to the first tab space.

4

1 回答 1

0

Split功能提供您所追求的。示例代码:

Dim inputString As String
Dim splitArray() As String
Dim result As String

inputString = "1 Test"
splitArray = Split(inputString, " ")

If(UBound(splitArray) >= 1) Then 'Making sure that it found something before using it
    result = splitArray(1) 'Text
End If

inputString = "1.1 Test"
splitArray = Split(inputString, " ")

If(UBound(splitArray) >= 1) Then
    result = splitArray(1) 'Text
End If
'etc.

更新

提供您想要的功能的代码:

  Dim splitArray() As String
  Dim curTable As Table
  Set curTable = ActiveDocument.Tables(1)
  For Row = 1 To curTable.Rows.Count
     With curTable
        splitArray = Split(.Cell(Row, 2).Range.Text, " ")

        If (UBound(splitArray) >= 1) Then 
           .Cell(Row, 2).Range.Text = splitArray(1)
           .Cell(Row, 1).Range.Text = splitArray(0)
        End If
     End With
  Next Row
于 2013-08-29T13:47:05.493 回答