這不是你是否需要工作表名稱以及單元格地址清楚。工作表名稱將生成完整的單元格區域地址,但如果您的目的是僅將單元格部分用於其他內容,那麼工作表名稱就會妨礙您的工作。這裏有三種可能性。
'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
你能告訴我們到目前爲止還沒有工作嗎? – xidgel
我在這篇文章中的代碼工作正常,但我只寫它來確定是否給了我活動單元格地址。我被困在從那裏去的地方。 我沒有進一步的編碼從那裏輸入單元格地址到列中,如我所述。 – Tombies