且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Vb代码将单元格拆分为多行

更新时间:2023-02-04 19:58:50

以下是帮助您入门的示例.

Here is an example to get you started.

此代码将获取您突出显示的活动单元格,并用空格将字符串分解,将其扩展到第1行的各列.

This code will take the active cell you have highlighted and break up the string by spaces, expanding it across columns in row 1.

示例:

  1. 单元格A5包含"Hello World测试"
  2. 突出显示单元格A5
  3. 给子打电话
  4. 代码将执行,您现在在A1中具有"Hello",在A2中具有"World",在A3中具有"Test"

  1. Cell A5 contains "Hello World Test"
  2. Highlight cell A5
  3. Call the sub
  4. The code will execute and you will now have "Hello" in A1, "World" in A2, and "Test" in A3

Sub Example()
    Dim txt As String
    Dim i As Integer
    Dim fullname As Variant

    txt = ActiveCell.Value
    fullname = Split(txt, " ")
    For i = 0 To UBound(fullname)
        Cells(1, i + 1).Value = fullname(i)
        MsgBox fullname(i)
    Next i
End Sub