首页 > 解决方案 > 如何在特定范围内插入单元格值?

问题描述

我一直在尝试从单元格中复制特定值,然后插入一个新列,其中所有记录都必须具有我在开头复制的值,(该值是文件中的日期)

我可以插入新列,其所有记录中复制的值,

问题是我需要从动态的特定范围复制所有行中的单元格值,但是使用我的实际代码我填写了所有行,直到最后一个 excel 行,而不仅仅是特定范围

细节

inicio = 范围变量的开始 fin = 范围变量的结束

我的代码是

    Dim inicio As String
    Dim fin As String

    Cells.Find(What:="TOTAL GENERAL").Select
    Selection.End(xlDown).Select
    inicio = ActiveCell.row
    Selection.End(xlDown).Select
    fin = ActiveCell.row

    Range("A1").Select
    Selection.End(xlToRight).Select
    Selection.Copy

我试图在 mi 代码中使用类似的东西,但它没有用

    ActiveCell.EntireColumn.Insert.Range(ActiveCell, fin - 1).EntireColumn.Insert

我希望你能帮助我,非常感谢

标签: excelvba

解决方案


这是我的代码:

Sub NewColumn()

    'Declarations.
    Dim RngInicio As Range
    Dim RngFin As Range
    Dim DatDate As Date

    'Setting variables.
    Set RngInicio = Cells.Find(What:="TOTAL GENERAL").End(xlDown)
    Set RngFin = RngInicio.End(xlDown)
    DatDate = Range("A1").End(xlToRight).Value

    'Inserting column.
    RngInicio.EntireColumn.Insert

    'Filling desired cells of the new column.
    Range(RngInicio, RngFin).Offset(0, -1).Value = DatDate

End Sub

告诉我它是否适合您,如果您需要任何其他解释或改进。顺便说一句:尽量避免在代码中选择您需要的范围(或对象)。;)


推荐阅读