【问题标题】:Moving Data with vba使用 vba 移动数据
【发布时间】:2017-05-25 19:42:30
【问题描述】:

我在 F、G、H 和 I 列中收到了数据。我需要将这些数据全部放入 E 列并取出重复项和空白单元格。到目前为止我的代码有效,但它把它们都放在同一行,并没有把它们放在适当的行上。我需要他们保持在他们当前所在的同一行,但只是转录到另一列。这是我到目前为止所拥有的:

Sub Sample()

    Dim ws As Worksheet
    Dim LastRow As Long, lastCol As Long, i As Long
    Dim Rng As Range, aCell As Range, delRange As Range '<~~ Added This
    Dim MyCol As New Collection

    ~~> Change this to the relevant sheet name
    Set ws = Sheets("Sheet1")

    With ws
        '~~> Get all the blank cells
        Set delRange = .Cells.SpecialCells(xlCellTypeBlanks)  '<~~ Added This

        '~~> Delete the blank cells
        If Not delRange Is Nothing Then delRange.Delete  '<~~ Added This

        LastRow = .Cells.Find(What:="*", After:=.Range("A1"), _
        Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, MatchCase:=False).Row

        lastCol = .Cells.Find(What:="*", After:=.Range("A1"), _
        Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious, MatchCase:=False).Column

        Set Rng = .Range("A1:" & Split(.Cells(, lastCol).Address, "$")(1) & LastRow)

        'Debug.Print Rng.Address
        For Each aCell In Rng
            If Not Len(Trim(aCell.Value)) = 0 Then
                On Error Resume Next
                MyCol.Add aCell.Value, """" & aCell.Value & """"
                On Error GoTo 0
            End If
        Next

        .Cells.ClearContents

        For i = 1 To MyCol.Count
            .Range("A" & i).Value = MyCol.Item(i)
        Next i

        '~~> OPTIONAL (In Case you want to sort the data)
        .Columns(1).Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With

End Sub

【问题讨论】:

  • 查看stackoverflow.com/questions/28958879/… 中的答案,大致了解如何循环遍历行(而不是单元格循环)。这会导致单元格正确地合并到同一行的一个单元格中。
  • 不确定您的问题。您的代码将所有内容放在一列中,每列放在单独的行中。究竟是什么。你的意思是“同一行”??
  • 他们开始时所在的同一行。希望这些图片会有所帮助。第一个是之前,第二个是之后。 tinypic.com/r/dqtc5/9tinypic.com/r/zmfvy0/9.
  • 我明白了,如果它们在同一行中,您只会删除重复项。您可以使用 E 列中的简单公式来做到这一点,只需在右侧列中选择值的第一个实例。

标签: excel vba


【解决方案1】:

试试这个。

Sub CopyThingy()

Dim wb As Workbook
Dim ws As Worksheet
Dim lCount As Long
Dim lCountMax As Long
Dim lECol As Long
Dim lsourceCol As Long

    lECol = 5 '* E column
    Set wb = ThisWorkbook
    Set ws = wb.Sheets(1) '*Your Sheet

    lCountMax = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
    lsourceCol = 6
    lCount = lCountMax

    Do While lCount > 1

        If ws.Cells(lCount, lsourceCol) <> "" Then
            ws.Cells(lCount, lECol).Value = ws.Cells(lCount, lsourceCol).Value
        End If
        lCount = lCount - 1

    Loop

    lCountMax = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
    lsourceCol = 7
    lCount = lCountMax

    Do While lCount > 1

        If ws.Cells(lCount, lsourceCol) <> "" Then
            ws.Cells(lCount, lECol).Value = ws.Cells(lCount, lsourceCol).Value
        End If
        lCount = lCount - 1

    Loop

    lCountMax = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row
    lsourceCol = 8
    lCount = lCountMax

    Do While lCount > 1

        If ws.Cells(lCount, lsourceCol) <> "" Then
            ws.Cells(lCount, lECol).Value = ws.Cells(lCount, lsourceCol).Value
        End If
        lCount = lCount - 1

    Loop

End Sub

【讨论】:

  • 随时 :) 乐于助人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多