VBA to identify top left cell of a selected range

exceluser2007

Active Member
Joined
Nov 21, 2007
Messages
365
Hi All,

I'm curious to know, that if a user has selected a range, how do you, in VBA, identify the:

1. Top left cell
2. Bottom left cell
3. Top right cell
4. Bottom right cell


For example if user has selected the range B5:M30, then we would want to identify in the macro:

1. Top left cell = B5
2. Bottom left cell = B30
3. Top right cell = M5
4. Bottom right cell = M30

Any help appreciated.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Just a quick question to revive an old (but useful) thread - in Tom's original code, he declares variables with $, & and % modifiers in the dim statement. What is this used for?

Thanks!

Code:
Sub Test2()
Dim cell As Range, MergeVal$
Dim TR&, BR&, LC%, RC%
With Selection
TR = .row
BR = .Rows.Count + .row - 1
LC = .Column
RC = .Columns.Count + .Column - 1
MsgBox _
"Top row: " & TR & vbCrLf & _
"Bottom row: " & BR & vbCrLf & _
"Left Column: " & LC & vbCrLf & _
"Right column: " & RC
End With
End Sub
 
Upvote 0
The $ is an abbreviated was of saying "as String".

Those abbreviation characters have been around for awhile, here is a general overview of variable types, their abbreviation characters, and their description. The intervening dots are my attempt to enhance readability for the fields on this html page.

Type...............Memory....Character...........Description
Byte...............1 byte....none................Positive whole number ranging from 0 through 255 that can be represented as a binary value.
Boolean............2 bytes...none................True or False value
Integer............2 bytes...%...................Whole numbers ranging from -32,768 through 32,767.
Long...............4 bytes...&...................Whole numbers ranging from -2,147,483,648 through 2,147,483,647.
Single.............4 bytes...!...................Single-precision floating-point number (with decimal points) ranging from -3.402823E38 to 3.402823E38.
Double.............8 bytes...#...................Double-precision floating-point number ranging from -1.79769313486232E308 to 1.79769313486232E308.
Currency...........8 bytes...@...................Large numbers between -922,337,203,685,477.5808 and 922,337,203,685,477.5807.
Date...............8 bytes...none................Represents dates from January 1, 100 through December 31, 9999.
Object.............4 bytes...none................An instance of a class or object reference.
String.............10 bytes + 1 byte per char...$...Series of any ASCII characters.
String (fix len)...length of string..............none...Series of any ASCII characters, of a pre-defined length.
Variant............min 16 bytes..................none...Any kind of data except fixed-length String data and user-defined types.
<!-- / message --><!-- sig -->
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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