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

前端 未结 4 711
有刺的猬
有刺的猬 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:34

    First find the column that your Sting is located, then count the rows beside it, set your range and enter the formula.

    Sub FindColumn()
        Dim f As Range, c As Integer
        Dim LstRw As Long, rng As Range
    
        Set f = Rows(1).Find(what:="Commodity", lookat:=xlWhole)
    
        If Not f Is Nothing Then
            c = f.Column
        Else: MsgBox "Not Found"
              Exit sub
    
        End If
    
        LstRw = Cells(Rows.Count, c - 1).End(xlUp).Row
        Set rng = Range(Cells(2, c), Cells(LstRw, c))
        rng = "My Formula"
    
    End Sub
    

提交回复
热议问题