如何添加variable.value作为range()的参数?

时间:2017-12-04 18:56:22

标签: excel excel-vba vba

我正在尝试合并前三个空行并在三个合并单元格中写入Activity#。我甚至无法弄清楚如何选择3个自定义单元格来准备合并。我在网上到处检查但是范围(A1:B2)总是给出一个确定的范围。我如何编写例如:range(variable_A1:variable_B2)?

到目前为止,这是我的代码:

Private Sub OKButton_Click()

'Make Sheet1 active
Sheet1.Activate

Dim beginning
Dim ending
Dim selection

beginning = Cells(empty_row.Value, 2)
ending = Cells(empty_row.Value + 2, 2)


'this is supposed to select 3 cells, but it doesn't work
selection = Range("beginning:ending").Select
'figure out how to merge cells below


Cells(empty_row.Value, 2).Value = "Activity" & Activity_number.Value


Dim i As Integer
For i = 1 To nb_subs.Value
    Cells(empty_row.Value + i + 2, 2).Value = "Sub-Activity" & i
Next i

1 个答案:

答案 0 :(得分:0)

Private Sub OKButton_Click()

Dim beginning As Range
Dim ending As Range
Dim selection As Range

With Sheet1

    Set beginning = .Cells(empty_row.Value, 2)
    Set ending = .Cells(empty_row.Value + 2, 2)


    'this is supposed to select 3 cells, but it doesn't work
    Set selection = .Range(beginning, ending)
    selection.Merge


    selection.Value = "Activity" & Activity_number.Value


    Dim i As Integer
    For i = 1 To nb_subs.Value
        .Cells(empty_row.Value + i + 2, 2).Value = "Sub-Activity" & i
    Next i
End With