当我在VBA中使用命名范围时,我收到错误“该命令不能用于多个选择”。
正在使用VBA派生一个很好的公式但我只想粘贴这些值。
Set rngCopy = rngCopy.Offset(0, 10).SpecialCells(xlCellTypeVisible)
rngCopy.Activate
rngCopy.Value = _
"=IF(RC[-10]="""","""",IF(WEEKDAY(RC[-10])=2,RC[-10]-3,IF(WEEKDAY(RC[-10])<>2,RC[-10]-1)))"
rngCopy.Copy
rngCopy.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
答案 0 :(得分:1)
它对我有用。此外,如果您的意图是复制粘贴公式作为值,那么您不需要采取那么长的路线。您只需使用Application.Evaluate
Sub Sample()
Dim rngCopy As Range
Set rngCopy = Range("A1")
Set rngCopy = rngCopy.Offset(0, 10).SpecialCells(xlCellTypeVisible)
rngCopy.Value = Application.Evaluate( _
"=IF(RC[-10]="""","""",IF(WEEKDAY(RC[-10])=2,RC[-10]-3,IF(WEEKDAY(RC[-10])<>2,RC[-10]-1)))" _
)
End Sub
使用非连续范围进行测试也有效。
Sub Sample()
Dim rngCopy As Range
Set rngCopy = Union(Range("A1"), Range("D1"), Range("F1"))
rngCopy.Activate
rngCopy.Value = Application.Evaluate( _
"=IF(RC[-10]="""","""",IF(WEEKDAY(RC[-10])=2,RC[-10]-3,IF(WEEKDAY(RC[-10])<>2,RC[-10]-1)))" _
)
End Sub