首页 文章

选择录制的宏中的数据

提问于
浏览
-1

我正在尝试录制宏以自动化格式化使用情况报告 . 报告并非都具有相同数量的行或列 .

我想要做的是选择包含数据的所有列和行 . 录制时如果单击Ctrl Shift Down Right,它将选择该报告上的数据 . 但是,如果我在一组包含更多行或列的数据上运行它,则不会在选择中包含它 .

有没有办法从起始单元格到可用数据的行或列的末尾进行选择?

1 回答

  • 0

    使用相对参考肯定会得到正确的编码 . 或者您可以将此代码 Range("A1").CurrentRegion.Select 放入宏中的适当位置(将最左上角的单元格放在"A1"的位置) .

    如果要进行更高级的工作,可以将当前区域保存为变量,并使用不同的属性来执行除简单选择区域之外的其他操作 .

    例如:

    sub test()
    dim rng as range
    dim rw as integer
    dim col as integer
    
    set rng = Range("A1").CurrentRegion 'choose the top leftmost cell in the range
    rw = rng.rows.count 'counts the number of rows in the selection
    col = rng.columns.count 'counts the number of columns
    rng.select 'selects the range in an efficient manner
    rng.columns(2).select 'selects the second column in the range
    rng.rows(2).select 'selects the second row in the range
    rng.cells(1,2).select 'selects the cell on the first row and second column in the range
    
    end sub
    

    希望这很有帮助 . 我只是想扩展一些你可以做的事情来处理你拥有的范围 .

相关问题