Need help converting offset formula for dynamic range in vba

michaelsmith559

Well-known Member
Joined
Oct 6, 2013
Messages
881
Office Version
  1. 2013
  2. 2007
Here is the current macro:
Code:
Sub Insert_Dynamic_Range()
Dim myText As String, myRange As Range, lastRow As Long

myText = Application.InputBox("Enter a Name for Range", Type:=2)
Set myRange = Application.InputBox("Select Where To Put Name", Type:=8)
myRange = myText

With ActiveSheet
lastRow = Cells(Rows.Count, myRange.Column).End(xlUp).Row
    ActiveSheet.Names.Add Name:=myText, _
    RefersTo:="OFFSET(myRange.Offset(1,0).Address,0,0,MATCH(1E+306,myRange.Column:myRange.Column),1)"
End With

End Sub

Here is sheet 1:

Excel 2012
AB
1
25
36
47
53
62
71
88
99
Sheet1


Here is sheet 1 after I run macro:

Excel 2012
AB
1Prices#VALUE!
25
36
47
53
62
71
88
99
Sheet1
Cell Formulas
RangeFormula
B1=SUM(Prices)
Named Ranges
NameRefers ToCells
Sheet1!Prices="OFFSET(myRange.Offset(1,0).Address,0,0,MATCH(1E+306,myRange.Column:myRange.Column),1)"


I am trying to replace this formula:
Offset($A$2,0,0,Match(1E+306,$A:$A),1)

with something like this:
OFFSET(myRange.Offset(1,0).Address,0,0,MATCH(1E+306,myRange.Column:myRange.Column),1)
 
I see, I forgot the = signs. The code works perfect now. It allows a user to create a dynamic named range for numbers with blank cells just by entering a name and selecting where to put the name. I just have a few more things I may add to it. Appreciate the help.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I'm going to post this because I worked on it for a half hour. Take what you need. With this code, you select the range including the header. It makes the range out of the values below.

Code:
Sub Insert_Dynamic_Range2()
  Dim myText As String, myRange As Range, i As String, j As String
  Dim Cel As Range
  Dim A As String
  Dim P As String
  
  myText = Application.InputBox("Enter a Name for Range", Type:=2)
  Set myRange = Application.InputBox("Select Where To Put Name", Type:=8)
  If myRange Is Nothing Then Exit Sub
  For Each Cel In myRange
    i = Cel.Address(0, 0)
    A = "'" & Cel.Parent.Name & "'!" & Cel.Address
    Exit For
  Next Cel
  j = Left(i, 1)
  i = A
  
  A = "=Offset(" & i & ", 1, 0, Match(1E+306, " & "$" & j & ":" & "$" & j & ")-1, 1)"
  ActiveSheet.Names.Add Name:=myText, RefersTo:=A
  
End Sub
 
Upvote 0
Here is what the macro does (although I am not finished with it yet). Suppose I want to create a dynamic named range. I want to call the named range "Prices". For example, setup sheet 1 like I have here:

Excel 2012
ABC
1
25#NAME?
36
44
57
63
77
82
98
101
119
12
Sheet1
Cell Formulas
RangeFormula
B2=SUM(Prices)


Sheet after macro runs:

Excel 2012
ABC
1Prices
2552
36
44
57
63
77
82
98
101
119
12
Sheet1
Cell Formulas
RangeFormula
B2=SUM(Prices)
Named Ranges
NameRefers ToCells
Sheet1!Prices=OFFSET(Sheet1!$A$2, 0, 0, MATCH(1E+306,Sheet1!$A:$A), 1)


Check the Formulas; Name Manager

You should see the named range and the appropriate formula. All I had to enter was the name I wanted to call the range and select the cell I wanted the name placed in.

Code:
Sub Insert_Dynamic_Range_Numbers()
Dim myText As String, myRange As Range, i As Variant, j As Variant

With ActiveSheet

myText = Application.InputBox("Enter a Name for Dynamic Range and Column Heading", Type:=2)
Set myRange = Application.InputBox("Select First Row for Column of Dynamic Range", Type:=8)
myRange = myText

i = myRange.Offset(1, 0).Address
j = Split(i, "$")(1)

ActiveSheet.Names.Add Name:=myText, _
    RefersTo:="=Offset(" & i & ", 0, 0, Match(1E+306, " & "$" & j & ":" & "$" & j & "), 1)"

End With

End Sub
 
Upvote 0
Sorry, the above code line: myText = Application.InputBox("Enter a Name for Dynamic Range and Column Heading", Type:=2) can be change to this:

myText = InputBox("Enter a Name for Dynamic Range and Column Heading")
 
Upvote 0
Here is my finished macro. It easily creates a dynamic named range for a range of numbers, including blanks. If your starting cell is blank it puts the name in that cell and if the starting cell contains a number it moves the numbers down one row and inputs the name in the first cell. The refers to formula is automatically filled in. Here is the macro:

Code:
Sub Insert_Dynamic_Range_Numbers()
Dim myText As String, myRange As Range, i As Variant, j As Variant
Dim k As Range, l As Variant

With ActiveSheet

myText = InputBox("Enter a Name for Dynamic Range and Column Heading")
Set k = Application.InputBox("Select First Row for Column of Dynamic Range", Type:=8)
l = k.Address

If Application.WorksheetFunction.IsNumber(Range(l)) <> True Then 'Uses this if name cell is blank
    Set myRange = Range(l)
        myRange = myText

    i = myRange.Offset(1, 0).Address
    j = Split(i, "$")(1)

ActiveSheet.Names.Add Name:=myText, _
    RefersTo:="=Offset(" & i & ", 0, 0, Match(1E+306, " & "$" & j & ":" & "$" & j & "), 1)"

ElseIf Application.WorksheetFunction.IsText(Range(l)) <> True Then 'Uses this if name cell is number
Range(Cells(k.Row, k.Column), Cells(Rows.Count, k.Column).End(xlUp)).Cut Destination:=k.Offset(1, 0)
    Set myRange = Range(l)
        myRange = myText

    i = myRange.Offset(1, 0).Address
    j = Split(i, "$")(1)

ActiveSheet.Names.Add Name:=myText, _
    RefersTo:="=Offset(" & i & ", 0, 0, Match(1E+306, " & "$" & j & ":" & "$" & j & "), 1)"


End If

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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