Enter value in cell based on first cell in named range

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
602
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

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

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
12,639
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,647
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
602
Office Version
  1. 365
Platform
  1. Windows
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!
 

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
602
Office Version
  1. 365
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,647
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,752
Messages
5,638,162
Members
417,011
Latest member
Amaden95

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
Top