在多列中排列文本

时间:2017-06-01 18:23:07

标签: excel vba

我正在尝试将资产负债表从PDF组织到Excel。我将整行粘贴在一个单元格中。然后,我设法从值结果中区分文本。我将文本粘贴在B列上,并尝试沿其他列分配值。有人可以帮助我吗?

Sub adjustingRow()

   Dim subjectCell As String
   Dim letters As String
   Dim index As Integer
   Dim text As String
   Dim values(1 To 7) As String

   letters = "qwertyuiopasdfghjklçzxcvbnmQWERTYUIOPASDFGHJKLÇZXCVBNM "
   subjectCell = Range("A6").Value

   For i = 1 To Len(subjectCell)

       If InStr(1, letters, Mid(subjectCell, i + 1, 1), vbTextCompare) = 0 Then
           index = i
           Exit For
       End If

   Next i

   text = Left(subjectCell, index)
   values() = Split(Right(subjectCell, Len(subjectCell) - index), " ", -1, vbTextCompare)

   Range("B6").Value = text
   For j = 1 To 7
       ActiveCell.Offset(0, j).Range("A1").Value = values(j)
   Next i

End Sub

1 个答案:

答案 0 :(得分:0)

评论中的家伙解决了我的问题。在这里:

Sub adjustmentRow()

Dim subjectCell As String
Dim letters As String
Dim index, spacesBeforeIndex As Integer
Dim text As String
Dim values() As String

letters = "àáéíóãõqwertyuiopasdfghjklçzxcvbnmÀÁÉÍÓÃÕQWERTYUIOPASDFGHJKLÇZXCVBNM ,.!?"
subjectCell = ActiveCell.Value

For i = 1 To Len(subjectCell)

    If InStr(1, letters, Mid(subjectCell, i + 1, 1), vbTextCompare) = 0 Then
        index = i
        Debug.Print index
        Exit For
    End If

Next i

text = Left(subjectCell, index)
values() = Split(Right(subjectCell, Len(subjectCell) - index), " ", -1, vbTextCompare)

ActiveCell.Offset(0, 1).Range("A1").Value = text
ActiveCell.Offset(0, 1).Range("A1").Select
For j = LBound(values) To UBound(values)
    values(j) = Replace(values(j), ".", ",")
    ActiveCell.Offset(0, j + 1).Range("A1").Value = values(j)
Next j

ActiveCell.Offset(1, -1).Range("A1").Select

End Sub