Assigning the value of a worksheet cell to a constant

后端 未结 3 1600
情歌与酒
情歌与酒 2020-12-10 09:24

I am trying to assign the value of a worksheet cell to a constant variable in a VBA macro. The logic behind that action is that the end user is supposed to enter the current

相关标签:
3条回答
  • 2020-12-10 09:52

    From help: You can't use variables, user-defined functions, or intrinsic Visual Basic functions (such as Chr) in expressions assigned to constants.

    In your case you have to use a variable.

    0 讨论(0)
  • 2020-12-10 09:59

    No it is not possible. As the word suggest it should be Constant.

    Workaround:

    Public Const weekRange As String = "$B$1"
    

    Then in your code:

    Sub Something()
        Dim thisWeek As Integer: thisWeek = Range(weekRange).Value
    '~~> some codes here
    End Sub
    
    0 讨论(0)
  • 2020-12-10 09:59

    I know this is old but I was looking to do this myself and came up with this. I use a function:

    Function insertPNA(strSource As Long) As String
    
    Dim strResult As String
    
    strResult = Replace(strSource, strSource, ThisWorkbook.Sheets("Audits & Actuals").Range("pnacode").Value)
    
    insertPNA = strResult
    
    End Function
    

    Whenever I want the pna code I just type insertpna(1). Hope someone finds this useful. (It doesn't have to be "1" obvs, I just used it to minimize typing.)

    0 讨论(0)
提交回复
热议问题