Tables interfere with VBA range variables depending on scope

前端 未结 3 1507
Happy的楠姐
Happy的楠姐 2020-12-11 04:13

An Excel file includes VBA-coded user-defined functions (UDFs) that are deployed in tables (VBA listobjects). Now, for reasons that escape me, if the UDF module contains

3条回答
  •  青春惊慌失措
    2020-12-11 04:24

    OK. This workaround should work.

    If When it does, there are a few issues and caveats to address.

    I'll also post explanations.

    Install the code in the ThisWorkbook module.

    Code:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
      Dim rngCell As Range
    
      For Each rngCell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas)
        With rngCell
          If .FormulaR1C1 Like "*ITEM_NAME*" _
          And Left$(.FormulaR1C1, 4) <> "=T(""" _
          Then
            .Value = "=T(""" & .FormulaR1C1 & """)"
          End If
        End With
      Next rngCell
    
    End Sub
    
    Private Sub Workbook_Open()
    
      Dim rngCell As Range
    
      For Each rngCell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas)
        With rngCell
          If .FormulaR1C1 Like "*ITEM_NAME*" _
          And Left$(.FormulaR1C1, 4) = "=T(""" _
          Then
            .FormulaR1C1 = .Value
          End If
        End With
      Next rngCell
    
    End Sub
    

提交回复
热议问题