How to avoid using select in VBA for variable cell ranges?

前端 未结 4 712
有刺的猬
有刺的猬 2021-01-27 05:59

I have heard of the dislike for using .select in VBA for excel macros, but I am wondering how my particular goal can be achieved without its use? For example, say there is a cel

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-27 06:32

    I want to simplify the answer a bit. For example

    Set r = ActiveCell
    MsgBox r.Address    ' $A$1
    Columns("A").Insert ' insert column before the first column
    MsgBox r.Address    ' $B$1
    

    so you can change your code to

    Dim cell As Range   ' optional
    Set cell = ActiveCell
    While cell = "Commodity"
        Set cell = cell(, 2)    ' similar to Set cell = cell.Resize(1,1).Offset(, 1)
    Wend
    While cell = ""
        Set cell = cell(, 2)
    Wend
    

提交回复
热议问题