Enter value in cell based on first cell in named range

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
I have a named range called "Test" that is 6 rows high and 6 columns wide. It can appear anywhere on a worksheet between A1:Z400.

=ADDRESS(MIN(ROW(Test)),MIN(COLUMN(Test)),4) gives me the cell reference of the first cell in the range. Let's say that cell reference is G8.

Rather than formulas, I'd like to use VBA to return two values:

1. In the cell to the left of the first cell in the Test range (F8 in the case where the first cell in the range is G8), return the number that is two rows less than the number of rows in the first cell in the range. If that first cell is G8, the number in F8 would be 6.

2. In the cell to the left of the first cell in the Test range and one row down (F9 in the case where the first cell in the range is G8), return the number that is two columns less than the number of columns in the first cell in the range. If that first cell is G8, the number in F9 would be 5.

The first cell in the range will vary and will not always be G8. Thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I had to read that a few times before I got it (or at least think I got it)
1
VBA Code:
Range("Test")(1, 1).Offset(2, -1).Value = Range("Test")(1, 1).Row - 2
2
VBA Code:
Range("Test")(1, 1).Offset(1, -1).Value = Range("Test")(1, 1).Column - 2
 
Upvote 0
How about
VBA Code:
Sub ChristineJ()
   With Range("Test")
      .Offset(, -1).Resize(1, 1).Value = .Row - 2
      .Offset(1, -1).Resize(1, 1).Value = .Column - 2
   End With
End Sub
This assumes that Test is workbook scope, or it's on the active sheet
 
Upvote 0
Solution
I had to read that a few times before I got it (or at least think I got it)
1
VBA Code:
Range("Test")(1, 1).Offset(2, -1).Value = Range("Test")(1, 1).Row - 2
2
VBA Code:
Range("Test")(1, 1).Offset(1, -1).Value = Range("Test")(1, 1).Column - 2
Thanks for deciphering it! It just about works - results are fine, one appears on the 3rd row of the range rather than the first. Thanks for the help!
 
Upvote 0
How about
VBA Code:
Sub ChristineJ()
   With Range("Test")
      .Offset(, -1).Resize(1, 1).Value = .Row - 2
      .Offset(1, -1).Resize(1, 1).Value = .Column - 2
   End With
End Sub
This assumes that Test is workbook scope, or it's on the active sheet
That does it! Thanks so much.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,972
Members
448,933
Latest member
Bluedbw

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