VBA Finding the next column based on an input value

后端 未结 5 1424
隐瞒了意图╮
隐瞒了意图╮ 2021-01-27 21:56

In a program that I\'m trying to write now I take two columns of numbers and perform calculations on them. I don\'t know where these two columns are located until the user tell

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 22:29

    In light of the comments of others (and they all raised valid points), here is a much better solution to the problem, using Offset and Address:

    Dim first_Column As String
    Dim second_Column As String
    Dim p As Integer
    
    first_Column = Range("B2").Text
    
    second_Column = _
        Range(first_Column + ":" + first_Column).Offset(0, 1).Address(0, 0, xlA1)
    p = InStr(second_Column, ":")
    second_Column = Left(second_Column, p - 1)
    

    The above should work for any valid column name, "Z" and "AA" etc. included.

提交回复
热议问题