繁体   English   中英

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

[英]Paste cells in specific row based on column header

我不是程序员,但希望获得帮助!

我正在尝试提取一系列单元格,然后将其粘贴到电子表格的另一部分中,但要粘贴到我想要的正确列中(此列稍后会更改,这就是为什么我希望它标识出将该单元格粘贴到右侧的列的原因行)

例如,取单元格(A2:A10)并将其粘贴到“ TTM”列D4:D12 ...中,然后将文本TTM放入D1 ...之后,TTM可能会变成E1,在这种情况下,A2:A10单元格需要移动到E4:E12 ...

非常感谢!

以下功能可用于执行您想要的操作。 您需要说明什么会触发此代码,以及是否要搜索“有效工作表”以外的内容

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM