按钮单击

时间:2015-08-25 02:16:40

标签: excel excel-vba vba

我想要一个按钮点击

  • 记录我在工作表1中选择的活动单元格的地址,并将其放入工作表2中“B”列中的下一个空行。
  • 同样点击按钮,我希望MsgBox显示相应的“A”列,其中包含参考编号。

到目前为止,我有一个工作按钮,但我的编码能力有限,这就是我所拥有的:

Private Sub CellReferenceBtn_Click()
MsgBox "The Active Cell Row and Column is " & ActiveCell(Row + 1, Column + 1).Address
End Sub

感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

你走了:

Private Sub CellReferenceBtn_Click()
    With ActiveCell
        Sheet2.[offset(b1,counta(b:b),)] = .Address
        MsgBox Sheet1.Cells(.Row, 1)
    End With
End Sub

答案 1 :(得分:0)

目前尚不清楚您是否需要工作表名称以及单元格地址。工作表名称将生成一个完整的单元格范围地址,但如果您的目的是仅使用单元格部分用于其他内容,则工作表名称将会妨碍。这有三种可能性。

'just the cell address without the worksheet name
Private Sub CommandButton1_Click()
    With Worksheets("Sheet2")
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = _
            Selection(1).Address
        'alternate for a larger selection of cells
        '.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = _
            Selection.Address
        'pass focus back to the worksheet cells
        Selection.Select
    End With
End Sub

'cell range and the worksheet name
Private Sub CommandButton1_Click()
    Dim addr As String
    With Worksheets("Sheet2")
        addr = Selection(1).Address(external:=True)
        'alternate for a larger selection of cells
        addr = Selection.Address(external:=True)
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = Chr(39) & _
            Replace(addr, _
              Mid(addr, InStr(1, addr, Chr(91)), _
                InStr(1, addr, Chr(93)) - InStr(1, addr, Chr(91)) + 1), _
                vbNullString)
        'pass focus back to the worksheet
        Selection.Select
    End With
End Sub

'full external path, worksheet name and cell range
'you can use this locally and it will abbreviate itself to what is necessary
Private Sub CommandButton1_Click()
    With Worksheets("Sheet2")
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = _
            Selection(1).Address(external:=True)
        'alternate for a larger selection of cells
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = _
            Selection.Address(external:=True)
        Selection.Select
    End With
End Sub