我有一个Range对象,用于显式调出Range(通常是单个单元格)并使用该单元格。我正在尝试将其设置的单元格合并到右侧两列的单元格中。因此,总共将合并3个单元。我一直在研究Address,Range和Offset对象/函数,但似乎无法找出一种干净地完成此操作的方法。在下面的代码中,我不确定是否应该将merge_rng
作为字符串来构建字符串,还是将merge_rng
作为范围来构建。
这是我尝试过的一些代码:
Dim site_curr_position As Range
Dim merge_rng As String
Set curr_ws = Worksheets(cal_name)
merge_rng = site_curr_position.Address(0, 0) & ":" & site_curr_position.Address(, 0) + 2 'how do I offset by 2 columns?
curr_ws.Range(merge_rng).Merge
也尝试过
Dim merge_rng As Range
merge_rng = curr_ws.Range(site_curr_position.Address(0, 0), site_curr_position.Address(0,0).Offset(0, 2)) 'assuming variable is at A1, this would return "A1:A1" still
curr_ws.Range(merge_rng).Merge
答案 0 :(得分:1)
您可以通过构建字符串来做到这一点,但这似乎不必要地复杂。我建议改用Resize
。这两个都合并了A1:C1。
Sub x()
Dim r As Range, s As String
Set r = Range("A1")
'Resize
r.Resize(, 3).Merge
'or build a string
s = r.Address
s = s & ":" & r.Offset(, 2).Address
Range(s).Merge
End Sub