Excel 宏中的可变列字母

Variable column letter in Excel macro

我想制作一个相对条件格式宏,根据我在其中播放宏的列来更改列字母。我可以这样做吗?

=IF( OR($B6="WEEKEND", $B6="NYTÅR", $B6="JULEFERIE", $B6="PINSEFERIE", $B6="KR. HIMMEL", $B6="ST. BEDEDAG", $B6="PÅSKEFERIE", $B6="NYTÅR"),TRUE,FALSE)

所以不应该说$B6,而是$(column letter)6。 我的宏看起来像这样(对不起丹麦语)

Sub LØ_SØ_HELLIGDAGS_FORMATERING()
'
' LØ_SØ_HELLIGDAGS_FORMATERING Makro
'

'
    ActiveCell.Range("A1:A420").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=HVIS( ELLER($B6=""WEEKEND""; $B6=""NYTÅR""; $B6=""JULEFERIE""; $B6=""PINSEFERIE""; $B6=""KR. HIMMEL""; $B6=""ST. BEDEDAG""; $B6=""PÅSKEFERIE""; $B6=""NYTÅR"");SAND;FALSK)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

我相信这可能会达到您的目的:

Sub LØ_SØ_HELLIGDAGS_FORMATERING()
'
' LØ_SØ_HELLIGDAGS_FORMATERING Makro
'

'
Dim myFormula As String
Dim cellAddress As String
    
    cellAddress = [SUBSTITUTE(ADDRESS(1,COLUMN(),3),"1","")] & 6
    ActiveCell.Range("A1:A420").Select
    myFormula = "=HVIS( ELLER($B6=""WEEKEND""; $B6=""NYTÅR""; $B6=""JULEFERIE""; $B6=""PINSEFERIE""; $B6=""KR. HIMMEL""; $B6=""ST. BEDEDAG""; $B6=""PÅSKEFERIE""; $B6=""NYTÅR"");SAND;FALSK)"
    myFormula = Replace(myFormula, "$B6", cellAddress)
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= myFormula
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
  1. [SUBSTITUTE(ADDRESS(1,COLUMN(),3),"1","")] 捕获活动单元格的列,& 6 附加 6 并将结果(以您想要的格式:$(column letter)6)写入 cellAddress .
  2. 之后,我留下了您更改选择的原始代码部分。
  3. 我把你原来的公式移到了变量myFormula
  4. 然后我简单地将 $B6 替换为在步骤 1 中写入 cellAddress 的任何值。