convert a wide column (containing multiple words/strings) to narrow one (just one word) in Excel using VBA

huangapple go评论52阅读模式
英文:

convert a wide column (containing multiple words/strings) to narrow one (just one word) in Excel using VBA

问题

我有一个Excel表格,其中包含两列:部门和员工,如下所示:

部门 员工
IT Adam
财务 Mary John Doe
人力资源 Alex Sara

在第二列中,我有多个字符串,它们之间以空格分隔。我试图重新格式化这些数据,使第二列只包含一个员工,并且这是VBA代码,用于将员工列中的每个单词分开,但格式不符合要求:

Sub ExtractEmployee()

    Dim i As Long, j As Long
    Dim arr() As String
    Dim LastRow As Long
    
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    For i = 1 To LastRow
        arr = Split(Cells(i, 2), " ")
        For j = LBound(arr) To UBound(arr)
            Cells(i, 3 + j) = arr(j)
        Next j
    Next i
    
End Sub

上述代码基于列B中字符串的数量添加了额外的列,这并不实用。我需要像这样的格式:

部门 员工
IT Adam
财务 Mary
财务 John
财务 Doe
人力资源 Alex
人力资源 Sara

任何帮助将不胜感激。

英文:

I have an Excel sheet with two columns: department and employees, like below:

department employees
IT Adam
Finance Mary John Doe
HR Alex Sara

In the second column I have multiple strings with a space delimiter.
I am trying to reformat this data into a narrow one that has one employee in the second column, and this is the VBA code that separates each word in the employees column but not with desired format:

Sub ExtractEmployee()

    Dim i As Long, j As Long
    Dim arr() As String
    Dim LastRow As Long
    
    LastRow = Cells(rows.count, "B").End(xlUp).row
    
    For i = 1 To LastRow
        arr = Split(Cells(i, 2), " ")
        For j = LBound(arr) To UBound(arr)
            Cells(i, 3 + j) = arr(j)
        Next j
    Next i
    
End Sub

The above code adds extra columns based on the number of strings in column B, which is not useful.
I need something like this:

department employee
IT Adam
Finance Mary
Finance John
Finance Doe
HR Alex
HR Sara

Any help would be appreciated.

答案1

得分: 2

将单元格拆分到新行中在Power Query中更容易:

选择数据后,点击功能区的数据 > 从表/范围获取

在Power Query打开后,拆分列 > 按分隔符,高级选项 > 行

英文:

Splitting cells to new rows is easier with power query:

With the data selected, click on the ribbon Data > From Table / Range

After power query opens, Split Column > by Delimiter, Advanced options > Rows

convert a wide column (containing multiple words/strings) to narrow one (just one word) in Excel using VBA

convert a wide column (containing multiple words/strings) to narrow one (just one word) in Excel using VBA

答案2

得分: 1

尝试这个。

Sub ExtractEmployee2()

    Dim i As Long, j As Long
    Dim arr() As String
    Dim LastRow As Long
    Dim endRow As Long
    
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    ' 计算员工总数
    For i = 1 To LastRow
        arr = Split(Cells(i, 2), " ")
        endRow = endRow + (UBound(arr) - LBound(arr) + 1)
    Next i

    ' 从最后一行开始写入单元格
    For i = LastRow To 1 Step -1
        arr = Split(Cells(i, 2), " ")
        For j = LBound(arr) To UBound(arr)
            Cells(endRow, 1) = Cells(i, 1)
            Cells(endRow, 2) = arr(j)
            endRow = endRow - 1
        Next j
    Next i
    
End Sub
英文:

Try this.

Sub ExtractEmployee2()

    Dim i As Long, j As Long
    Dim arr() As String
    Dim LastRow As Long
    Dim endRow As Long
    
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
' count total number of employees
    For i = 1 To LastRow
        arr = Split(Cells(i, 2), " ")
        endRow = endRow + (UBound(arr) - LBound(arr) + 1)
    Next i

' write cells, begining from last    
    For i = LastRow To 1 Step -1
        arr = Split(Cells(i, 2), " ")
        For j = LBound(arr) To UBound(arr)
            Cells(endRow, 1) = Cells(i, 1)
            Cells(endRow, 2) = arr(j)
            endRow = endRow - 1
        Next j
    Next i
    
End Sub

huangapple
  • 本文由 发表于 2023年7月10日 12:33:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定