Copy and paste the Range address displayed in the Msg Box below subroutine to any selected cell

shoun2502

New Member
Joined
Nov 14, 2018
Messages
45
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a following code which displays the Range address in the msg box after executing the code

I am looking to copy and paste Range address to any of the Selected cell.

VBA Code:
Sub GetRangeAddress()
Dim r As Range
Dim s As String

  Select Case Selection.Areas.Count
  Case 0:
    MsgBox "Nothing selected."
  Case 1:
    MsgBox "Selected range: " & Selection.Areas(1).Address(False, False)
  Case Else
    s = ""
    For Each r In Selection.Areas
      s = s + vbNewLine + r.Address(False, False)
    Next r
    MsgBox "Selected several areas:" & s
  End Select
End Sub

I would appreciate if any one could help to customize the code and get the desired result.

Thanks in anticipation
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You don't specify which cells need to contain the address

This puts it in the first cell of the selection:

VBA Code:
Sub GetRangeAddress()
Dim r As Range
Dim s As String

  Select Case Selection.Areas.Count
  Case 0:
    MsgBox "Nothing selected."
  Case 1:
    MsgBox "Selected range: " & Selection.Areas(1).Address(False, False)
    Selection.Cells(1, 1) = Selection.Areas(1).Address(False, False)
  Case Else
    s = ""
    For Each r In Selection.Areas
      s = s + vbNewLine + r.Address(False, False)
    Next r
    MsgBox "Selected several areas:" & s
    Selection.Cells(1, 1) = s
  End Select
End Sub
 
Upvote 0
Hi gallen ,

It will not suffice my purpose. As it replaces my value of the selection with the Range address and subsequently will change my data.

If its possible to change it A1 would be fine with me.

Thanks
 
Upvote 0
So just replace both instances of:

VBA Code:
Selection.Cells(1, 1)

with

VBA Code:
Range("A1")
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top