Copy MessageBox returned value to a cell

julhs

Active Member
Joined
Dec 3, 2018
Messages
471
Office Version
  1. 2010
Platform
  1. Windows
How can I copy the returned range address that’s displayed in a MessageBox to a cell on the same sheet.
Having established what the used range is with the code below, I want to put that returned range address into a cell.
eg-: so W10 will = the rng.Address
Have tried numerous bits found on line, to no avail.
Have tried; copy to ClipBoard using Ctrl-C, then use Ctrl-V to paste in cell, but its pasting ALL the Msg box properties but I ONLY want the Range Address.
VBA Code:
Sub MeUsedRangetest()
Dim lastRow As Long, firstrow As Long
Dim rng As Range
Dim sht As Worksheet
Dim sMsg As String

Set sht = ThisWorkbook.ActiveSheet
      firstrow = sht.Range("s:s").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
      lastRow = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
Set rng = sht.Range("T" & firstrow & ":T" & lastRow)

MsgBox "Range is " & rng.Address

End Sub
 
Sorry for any minor inconsistencies you might come across, but I DON’T believe they impact on the ultimate question?
So:-
firstrow = .Range("s:s").Find(what:="CF rules"….. = S7
lastRow = .Range("T:T").Find(what:="Cash Paid"….. = T24
ACTUAL used range IN ColS is = S10:S20
It’s the ACTUAL used range S10:S20 that I am looking to return the address of (to a cell on the sheet)

I have to use “Cash Paid” row as lastrow reference; because BETWEEN Row 10 and Row “Cash Paid” it is a dynamic range( rows are constantly being added)
There are rows BELOW “Cash paid” in Col T and they have values, but they are NOT relevant so to be ignored. So hence the need to first find “Cash Paid” row and then count UP ColS to find the ACTUAL last used cell/row (so we are ignoring ever thing below “Cash Paid” row in Col T)
I sincerely hope I’ve not rushed my reply and in the proses given you duff/incorrect info ?
Afraid had to resort to screen shot as Xl2bb was having a huge hissy fit

1674256613230.png
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
sXl2bb has gotten over the hissy fit!

Mumps
Code below is what I ran on the Xl2bb, it contains your new suggestion, but I’ve left lines in from your first version so I could compare the Debug.Print results
Excel Formula:
Sub MeUsedRangetest()
Dim lastrow As Long, firstrow As Long
With ActiveSheet
firstrow = .Range("s:s").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
lastrow = .Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
.Range("s30") = .Range("S" & firstrow).CurrentRegion.Address
.Range("s32") = .Range("s" & firstrow + 3 & ":s" & lastrow - 1).Address
End With

Debug.Print firstrow
Debug.Print lastrow
Debug.Print Range("s" & firstrow + 3 & ":s" & lastrow - 1).Address
End Sub
Accounts Code TestingBook1.xlsm
NOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAK
5
6DateInv #Payment MethodPayment Details ListBank & CashDrawingsPurchases of Stock / MaterialsTool,Weather/Safety equipRepairs & RenewalsMotor ExpensesHire ChargesLiability InsuranceN.I cont / TGWUGen Insur Office Postage/ StationaryMiscAcquisition of AssetsLet PropertyBank ChargesUtilities / HouseIncome Tax
7sLeave these 2 row emptyCF rules
8sn
9sn
10sApr-07St Order Rent694.50694.50
11sApr-07D. DebitAmazon Prime5.995.99
12sApr-07gs005My SolHeating Oil 540.22540.22
13sApr-08D. DebitEE Mobile(1)28.2128.21
14sApr-08D. DebitAA membership39.6139.61
15sApr-11gs010My SolFuel Other127.00127.00
16sApr-12gs015My SolTesco29.4429.44
17sApr-12gs020My SolTesco113.55113.55
18sApr-13My SolAmazon Prime7.997.99
19sApr-19D. DebitEDF energy16.2516.25
20sApr-19gs025My SolTesco15.0015.00
21s
22s
23s
24s1617.76Cash Paid171.97   127.00    67.82   1250.97  
25s 
26sIrrelevant data in thisarea
27svvhh
28svckk
29scvmm
30CurrentRegion.Address$A$6:$AJ$30
31
32.Range("s" & firstrow + 2 & ":s" & lastrow - 1).Address$S$10:$S$23
33
April 22 - 23 (3)
Cell Formulas
RangeFormula
S24,U24:AI24S24=SUM(S$7:OFFSET(S24,-1,0))
AJ24AJ24=SUM(AJ$7:OFFSET(AI24,-1,0))
T25T25=SUMIF(Q$7:$Q23,$T24,$S$7:$S26)
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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