Dynamic Names Pointing to Blank-Delimited Blocks of Data

Swamp Thing

Active Member
Joined
Aug 16, 2002
Messages
313
Hello,
I have a column of data that is blank-delimited into five blocks. The size of each block will vary, since the data comes from a web query. A blank (empty) cell tells me where a block ends and the next one begins.

I would like to set up five dynamic names (e.g. Block_1, Block_2 and so on), each of which will reference the corresponding segment of data.


What would be an elegant way of defining the dynamic names?

Thanks
Swamp Thing
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Code:
Sub SetNames()
    Dim i As Long, sNameCol As String
    sNameCol = "A"
    ActiveSheet.Columns(sNameCol).SpecialCells(xlCellTypeConstants, 23).Select
    For i = 1 To Selection.Areas.Count
        ActiveWorkbook.Names.Add Name:="Block_" & i, RefersTo:=Selection.Areas(i)
    Next i
End Sub
 
Upvote 0
A non-VBA approach:
If your first block starts in A1 and each block has one blank cell between it and the next:

Define these names
Name: lastCell RefersTo: =INDEX(Sheet1!$A:$A,MATCH("zzzz",Sheet1!$A:$A),1)

Name: firstBlank
RefersTo: =MATCH("", (INDEX(Sheet1!$A:$A,1,1):lastCell)&"", 0)
Name: secondBlank
RefersTo: =MATCH("", (INDEX(Sheet1!$A:$A,firstBlank+1,1):lastCell)&"",0)+firstBlank
Name: thirdBlank
RefersTo: =MATCH("", (INDEX(Sheet1!$A:$A,secondBlank+1,1):lastCell)&"",0)+secondBlank
Name: fourthBlank
RefersTo: =MATCH("", (INDEX(Sheet1!$A:$A,thirdBlank+1,1):lastCell)&"",0)+thirdBlank

Name: Block1 RefersTo: =INDEX(Sheet1!$A:$A,1,1):INDEX(Sheet1!$A:$A,firstBlank-1,1)
Name: Block2 RefersTo: =INDEX(Sheet1!$A:$A,firstBlank+1,1):INDEX(Sheet1!$A:$A,secondBlank-1,1)
Name: Block3 RefersTo: =INDEX(Sheet1!$A:$A,secondBlank+1,1):INDEX(Sheet1!$A:$A,thirdBlank-1,1)
Name: Block4 RefersTo: =INDEX(Sheet1!$A:$A,thirdBlank+1,1):INDEX(Sheet1!$A:$A,fourthBlank-1,1)
Name: Block5 RefersTo: =INDEX(Sheet1!$A:$A,fourthBlank+1,1):lastCell
 
Upvote 0
Code:
Sub SetNames()
    Dim i As Long, sNameCol As String
    sNameCol = "A"
    ActiveSheet.Columns(sNameCol).SpecialCells(xlCellTypeConstants, 23).Select
    For i = 1 To Selection.Areas.Count
        ActiveWorkbook.Names.Add Name:="Block_" & i, RefersTo:=Selection.Areas(i)
    Next i
End Sub

Nice solution. I am working on project which I can utilise your code. Thank you mate.
 
Upvote 0

Forum statistics

Threads
1,223,719
Messages
6,174,089
Members
452,542
Latest member
Bricklin

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