【问题标题】:VBA Combine Multiple columns of data into 1 columnVBA 将多列数据合并为 1 列
【发布时间】:2015-04-08 15:09:53
【问题描述】:

我还是 VBA 的新手,在寻求帮助之前我一直在尝试我能想到的一切来完成这项工作,但无法弄清楚。

我有一个带有多个选项卡的 excel 文件。我只关心其中的两个。我需要将基于它们的值从选项卡“路线图”中不为空白的行组合到选项卡“PPPP”上的 B 列中。我的代码将对第一组数据执行此操作,然后用第二组数据替换该数据。

Sub Move_PPPP()

Sheets("PPPP").Select
Rows("2:1000").Select
Selection.ClearContents

Dim rowCount2 As Long, shtSrc As Worksheet
Dim shtDest As Worksheet
Dim rng2 As Range
Dim currentRow As Long

    Set shtSrc = Sheets("Roadmap")
    Set shtDest = Sheets("PPPP")

    rowCount2 = shtSrc.Cells(Rows.Count, "C").End(xlUp).Row

    Set rng2 = shtSrc.Range("C6:C" & rowCount2)

    currentRow = 2

        For Each cell2 In rng2.Cells
        If cell2.Value <> "" Then

       shtDest.Range("B" & currentRow).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 10).Text
       shtDest.Range("B" & currentRow).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 11).Text
       shtDest.Range("B" & currentRow).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 12).Text
          currentRow = currentRow + 1

        ElseIf cell2.Value = "" Then

        End If
        Next cell2

End Sub

我试图为我的目标工作表添加一个范围,但这样做只会给我“路线图”选项卡中最后一行数据的 9 行

Sub Move_PPPP()

Sheets("PPPP").Select
Rows("2:1000").Select
Selection.ClearContents

Dim rowCount2 As Long, shtSrc As Worksheet
Dim columnCount As Long
Dim shtDest As Worksheet
Dim rng2 As Range
Dim rng As Range
Dim currentRow As Long

    Set shtSrc = Sheets("Roadmap")
    Set shtDest = Sheets("PPPP")

    rowCount2 = shtSrc.Cells(Rows.Count, "C").End(xlUp).Row
    columnCount = shtDest.Cells(Columns.Count, "B").End(xlUp).Row

    Set rng2 = shtSrc.Range("C6:C" & rowCount2)
    Set rng = shtDest.Range("B2:B" & columnCount & currentRow)

    currentRow = 2

        For Each cell2 In rng2.Cells
        If cell2.Value <> "" Then

        rng.Value = "           " & cell2.Text & " - " & cell2.Offset(0, 10).Text

            currentRow = currentRow + 1

        ElseIf cell2.Value = "" Then

        End If
        Next cell2


End Sub

样本数据

路线图选项卡

列:C D E F G H I J K L M 标题:项目状态打开关闭名称P1 P2 P3 P4 P5 P6

第 1 行:FISMA 新 是 否 Albert na na na na New Day Old Data 第 2 行:QRD 已关闭 否 是 Albert na na na na na 已关闭

期望的结果。当 M 空白时将 C 列与 M 列合并,循环遍历整行并将该数据放入 PPPP 选项卡的 B 列。然后在 N 空白时将 C 列与 N 合并,并将其放在 PPPP 选项卡上,B 列位于 M 列的数据下方。

PPP 选项卡

单元格 B2 FISMA - 新的一天

单元格 B4 FISMA - 旧数据 QRD - 关闭

解决方案:

Sub Move_PPPP()

Sheets("PPPP").Select
Rows("2:1000").Select
Selection.ClearContents

Dim rowCount2 As Long, shtSrc As Worksheet
Dim shtDest As Worksheet
Dim rng2 As Range
Dim currentRow As Long

    Set shtSrc = Sheets("Roadmap")
    Set shtDest = Sheets("PPPP")

    rowCount2 = shtSrc.Cells(Rows.Count, "C").End(xlUp).Row

    Set rng2 = shtSrc.Range("C6:C" & rowCount2)

    currentRow = shtDest.Range("A" & Rows.Count).End(xlUp).Row

        For Each cell2 In rng2.Cells
        If cell2.Value2 <> "" Then
        shtDest.Range("A" & currentRow).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 9).Text
        currentRow = currentRow + 1

        ElseIf cell2.Value = "" Then

        End If
        Next cell2

    Set rng2 = shtSrc.Range("C6:C" & rowCount2)

    currentRow = shtDest.Range("A" & Rows.Count).End(xlUp).Row + 1

       For Each cell2 In rng2.Cells
       If cell2.Value2 <> ""  Then
       shtDest.Range("A" & currentRow + 1).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 10).Text
       currentRow = currentRow + 1

        ElseIf cell2.Value = "" Then

        End If
        Next cell2

    Set rng2 = shtSrc.Range("C6:C" & rowCount2)

    currentRow = shtDest.Range("A" & Rows.Count).End(xlUp).Row + 1

       For Each cell2 In rng2.Cells
       If cell2.Value2 <> ""  Then
       shtDest.Range("A" & currentRow + 1).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 11).Text
       currentRow = currentRow + 1

        ElseIf cell2.Value = "" Then

        End If
        Next cell2

            Set rng2 = shtSrc.Range("C6:C" & rowCount2)

    currentRow = shtDest.Range("A" & Rows.Count).End(xlUp).Row + 1

       For Each cell2 In rng2.Cells
       If cell2.Value2 <> ""  Then
       shtDest.Range("A" & currentRow + 1).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 12).Text
       currentRow = currentRow + 1

        ElseIf cell2.Value = "" Then

        End If
        Next cell2

结束子

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    在第一个版本上,试试这个:

     Sub Move_PPPP()
    
    Sheets("PPPP").Select
    Rows("2:1000").Select
    Selection.ClearContents
    
    Dim rowCount2 As Long, shtSrc As Worksheet
    Dim shtDest As Worksheet
    Dim rng2 As Range
    Dim currentRow As Long
    
        Set shtSrc = Sheets("Roadmap")
        Set shtDest = Sheets("PPPP")
    
        rowCount2 = shtSrc.Cells(Rows.Count, "C").End(xlUp).Row
    
        Set rng2 = shtSrc.Range("C6:C" & rowCount2)
    
        currentRow = shtDest.Range("B" & Rows.Count).End(xlUp).Row
    
            For Each cell2 In rng2.Cells
            If cell2.Value <> "" Then
    
           shtDest.Range("B" & currentRow).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 10).Text
           shtDest.Range("B" & currentRow + 1).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 11).Text
           shtDest.Range("B" & currentRow + 2).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 12).Text
              currentRow = currentRow + 1
    
            ElseIf cell2.Value = "" Then
    
            End If
            Next cell2
    
     Set rng2 = shtSrc.Range("D6:D" & rowCount2)
    
        currentRow = shtDest.Range("B" & Rows.Count).End(xlUp).Row + 1
    
            For Each cell2 In rng2.Cells
            If cell2.Value <> "" Then
    
           shtDest.Range("B" & currentRow).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 10).Text
           shtDest.Range("B" & currentRow + 1).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 11).Text
           shtDest.Range("B" & currentRow + 2).Value2 = "           " & cell2.Text & " - " & cell2.Offset(0, 12).Text
              currentRow = currentRow + 1
    
            ElseIf cell2.Value = "" Then
    
            End If
            Next cell2
    
    End Sub
    

    【讨论】:

    • 谢谢,这给了我和以前一样的输出。它用每个偏移量替换数据。我需要它跳过一行并将下一组数据放在它下面。 @R3uK
    • 好的,但是在第一个中,您在同一个单元格中写了 3 次 shtDest.Range("B" &amp; currentRow).Value2 = ,所以这是主要问题!您希望它们水平堆叠还是垂直堆叠?
    • 我希望它们在 PPPP 选项卡的 B 列中垂直,而不是重叠。我希望来自选项卡路线图列 L 的信息填充在选项卡 PPPP 列 B 行 2 上 - 不管有多少行数据,然后将路线图列 M 填充到 L 列数据刚刚移动的位置下的 PPPP 1 单元格上。 @R3uK
    • 好的,它更清晰,我在手机上,但我会尝试编辑。只是您的 3 个不同(如上一条评论中所述)信息仍然正常吗?
    • 谢谢!我认为我不能继续添加 For Each / If 语句。我能够使用您提供的内容并将其放入我的代码中来获得我需要的内容。我真的很感谢你的帮助!! @R3uK
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多