根据列标题粘贴特定行中的单元格

时间:2014-12-01 23:33:01

标签: excel vba excel-vba

我不是程序员,但我会感激一些帮助!

我正在尝试获取一系列单元格,并将它们粘贴到电子表格的另一部分,但是将其粘贴到我想要的正确列中(该列将在稍后更改,这就是为什么我希望它来识别列将细胞粘贴到右侧)

例如,取细胞(A2:A10)并将它们粘贴到" TTM" D4列:D12 ......我将文本TTM放入D1 ......之后,TTM可能变为E1,在这种情况下,A2:A10单元格需要移动到E4:E12 ......

非常感谢!

1 个答案:

答案 0 :(得分:1)

以下功能可用于执行您想要的操作。您需要解释触发此代码的内容,以及是否要搜索“活动表格”以外的其他内容。

Function Move_Cells()
Dim iCols   As Integer
Dim i       As Integer
Dim strKey  As String
Dim iNewCol As Integer

    strKey = "TTM"      ' Set this to whatever label you want to search row 1 for.
    iCols = ActiveSheet.UsedRange.Columns.Count         ' Get count of used columns

    For i = 1 To iCols              ' Find column containing 'TTM'
        If LCase(Cells(1, i).text) = LCase(strKey) Then     ' ignore case inCASE SoMeBody....
            iNewCol = i             ' Save col # containing search keyword
            Exit For
        End If
    Next i

    'ActiveSheet.Range("A2:A10").Copy        ' Where to copy from
    'ActiveSheet.Cells(2, iNewCol).PasteSpecial xlPasteValues        ' Paste into new location
    'Application.CutCopyMode = False

    ' Try the following instead of the previous copy/paste
    Range("A2:A10").Select
    Selection.Copy
    Cells(2, iNewCol).Select
    ActiveSheet.Paste

End Function
相关问题