button单击时在新工作表中logging活动单元格地址

我想要一个button点击

  • logging我在表1中select的活动单元格的地址,并将其放入表2中“B”列中的下一个空行。
  • 在button点击,我希望一个MsgBox显示列“A”,其中包含的参考号码对应的行。

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

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

任何帮助表示赞赏!

干得好:

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

您是否需要工作表名称以及单元格地址并不清楚。 工作表名称将会生成一个完整的单元格区域地址,但是如果您的意图是仅将单元格部分用于其他内容,那么工作表名称将会妨碍您的工作。 这里有三种可能性。

 '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 

就像是

 Sub AddForm() Dim ws1 As Worksheet Dim ws2 As Worksheet Set ws1 = Sheets(1) Set ws2 = Sheets(2) 'check active sheet is sheet 1 If ActiveCell.Parent.Name = ws1.Name Then ws2.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = ActiveCell.Address MsgBox ActiveCell.Address End If End Sub