将开头的所有单元格插入另一行但已压缩的行

时间:2019-07-02 23:14:50

标签: excel vba

我有包含大量列的原始数据。我想提取第一行中以Sum_Pop开头的单元格,例如Sum_Pop2_3,N,。然后,我想将这些字符串输入第4行的第5列的摘要表中。有人可以为我提供一些VBA代码来解决此问题吗?

原始数据大约有160列,我只想提取大约10-20个单元格。然后,我希望它们按顺序输入到分析表中,因此单元格之间没有任何空隙,因此它将为每行的人口每年增加创建标题。

我尝试使用此代码:

Private Sub ()
    Dim qq As Integer
    Dim I As Integer

    For qq = 5 To 25
        For I = 1 To 200
            If Sheets("raw").Range("A" & I) Like "Sum_Pop*" Then
                Sheets("raw").Range("A" & I) = Sheets("analysis").Range("R4C" & qq).Value
            Else:
                Next I
            End If
        Next qq
    Next I
End Sub

1 个答案:

答案 0 :(得分:0)

Sub Macro1()
Dim ws as Worksheet, LastCol as Long, i as Long, MySel as Range

Set ws = ThisWorkbook.Sheets("raw")

With ws
    LastCol= .Cells(1, .Columns.Count).End(xlToLeft).Column
    For i = 1 to LastCol
        If .Cells(1, i) Like "Sum_Pop*" Then
            If MySel Is Nothing Then
                Set MySel = .Cells(1, i)
            Else
                Set MySel = Union(MySel, .Cells(1, i))
            End If
        End If
    Next i
End With

If Not MySel Is Nothing Then MySel.Copy _
        Destination:=ThisWorkbook.Sheets.Add.Range("E4")

End Sub