Excel VBA – 将parameter passing给属性

在Excel VBA中,我试图将一个参数值传递给属性'VerticalAlignment'。 我得到的错误是:“无法设置Range类的Horizo​​ntalAlignment属性”。 显然,问题出在'horzAlign'和'vertAlign'的价值,但是,什么?

' Merge the range & horizontal & vertical ' alignment as per arguments Sub mergeCellsWithLeftAlign(ByVal curRange As Range, _ ByVal horzAlign As String, ByVal vertAlign As String) With curRange .HorizontalAlignment = horzAlign .VerticalAlignment = vertAlign .MergeCells = True End With End Sub 

这在另一个程序中被调用,像这样:

 Call mergeCellsWithLeftAlign(Range("F10:F11"), "xlLeft", "xlBottom") 

看看VBA帮助的值不能是"xlLeft", "xlBottom"而是xlLeft, xlBottom ,即没有引号 – 它们是整数常量。

RTFM。 从帮助:

该属性的值可以设置为以下常量之一:

xlBottom xlCenter xlDistributed xlJustify xlTop

根据msdn,属性值必须是其中的一个:

水平:

  • xlCenter
  • xlDistributed
  • xlJustify
  • xlLeft
  • xlRight

垂直:

  • xlBottom
  • xlCenter
  • xlDistributed
  • xlJustify
  • xlTop