How to get the opposite cell details from range in VBA?

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi

If my selection range is a1: e5 then with below, we can have the first cell as fCell is a1, and last cell as lCell is e5.
But what if, i want to get the first column last cell as fColLastCell is a5, and last column first cell as lColFirstCell is e1?

Range can be vary time to time. Its not fixed.
How can I grab this in VBA?

VBA Code:
  Set Rng = Selection
  fCell = Rng.Cells(1).Value
  lCell = Rng(Rng.count).Value
  ACellAdd = ActiveCell.Address(0, 0)
  lCellAdd = Rng(Rng.count).Address(0, 0)
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
how about
Rich (BB code):
    Dim TLeft As String, TRight As String, BLeft As String, BRight As String
    
    With Selection
        TLeft = .Cells(1, 1)
        TRight = .Cells(1, .Columns.Count)
        BLeft = .Cells(.Rows.Count, 1)
        BRight = .Cells(.Rows.Count, .Columns.Count)
    End With
 
Upvote 0
Maybe:
VBA Code:
Sub FirstAndLast()
Dim R As Range, Ar As Range
Set R = Selection
If R.Areas.Count = 1 Then  'R is a range of contiguous cells
    'first cell of R
    MsgBox R(1).Address
    'last cell of R
    MsgBox R(R.Count).Address
    'first cell of 2nd column
    MsgBox R.Columns(2).Cells(1).Address
    'last cell of 2nd column
    MsgBox R.Columns(2).Cells(R.Columns(2).Cells.Count).Address
Else  'R is a range of non-contiguous cells
    'first cell of R
     MsgBox R(1).Address
     'last cell of R
     MsgBox Cells(R.Areas(R.Areas.Count).Row, _
         R.Areas(R.Areas.Count).Column)(R.Areas(R.Areas.Count).Count).Address
End If
End Sub
 
Upvote 0
how about
Rich (BB code):
    Dim TLeft As String, TRight As String, BLeft As String, BRight As String
   
    With Selection
        TLeft = .Cells(1, 1)
        TRight = .Cells(1, .Columns.Count)
        BLeft = .Cells(.Rows.Count, 1)
        BRight = .Cells(.Rows.Count, .Columns.Count)
    End With
Thanks.
 
Upvote 0
Maybe:
VBA Code:
Sub FirstAndLast()
Dim R As Range, Ar As Range
Set R = Selection
If R.Areas.Count = 1 Then  'R is a range of contiguous cells
    'first cell of R
    MsgBox R(1).Address
    'last cell of R
    MsgBox R(R.Count).Address
    'first cell of 2nd column
    MsgBox R.Columns(2).Cells(1).Address
    'last cell of 2nd column
    MsgBox R.Columns(2).Cells(R.Columns(2).Cells.Count).Address
Else  'R is a range of non-contiguous cells
    'first cell of R
     MsgBox R(1).Address
     'last cell of R
     MsgBox Cells(R.Areas(R.Areas.Count).Row, _
         R.Areas(R.Areas.Count).Column)(R.Areas(R.Areas.Count).Count).Address
End If
End Sub
Sorry, it doesn't give me the desired result.
 
Upvote 0
You could also leave them as ranges, then you could work with them in the same way:

VBA Code:
Type RangeParts
    EntireRange As Range
    TLeft As Range
    TRight As Range
    BLeft As Range
    BRight As Range
End Type

Sub Test()
Dim myRng As RangeParts
Set myRng.EntireRange = Selection

  With myRng.EntireRange
        Set myRng.TLeft = .Cells(1, 1)
        Set myRng.TRight = .Cells(1, .Columns.Count)
        Set myRng.BLeft = .Cells(.Rows.Count, 1)
        Set myRng.BRight = .Cells(.Rows.Count, .Columns.Count)
    End With
    
myRng.BRight.Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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