• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 90
  • Last Modified:

Excel VBA Modification Dynamic

Hello,

i need help with the attached file and its code.  

the below code, does create dynamic range, but with defects, where if the data has empty cells then it does not work.

i need help with modification of below code, so that it takes care of the empty cells as well. so that in each column it the dynamic range encompasses the last row that has data in it.

thanks.

Sub Create_RangeNames()
    'Creates range names based on header row information
    Dim wbk As Workbook
    Dim sht As Worksheet
    Dim rng, rng2 As Range
    Dim cl As Object
    Dim c As Long
    Dim strAddr As Variant
    Dim strShName, strHdrName, strCol As String
    
    Set wbk = ActiveWorkbook
    For Each sht In wbk.Sheets
        c = sht.Cells.SpecialCells(xlLastCell).Column
        Set rng = sht.Range("A1", sht.Range("A1").Offset(0, c))
        For Each cl In rng
            If cl.Value <> "" Then
                strShName = Replace(sht.Name, " ", "_", 1)
                strHdrName = Replace(cl.Value, " ", "_", 1)
                strAddr = Split(cl.Address, "$")
                strCol = "$" & strAddr(1) & ":$" & strAddr(1)
                Set rng2 = sht.Range(cl, cl.End(xlDown))
                ActiveWorkbook.Names.Add Name:=strShName & strHdrName, _
                    RefersTo:="=OFFSET('" & sht.Name & "'!" & cl.Address & ",0,0,COUNTA('" & _
                    sht.Name & "'!" & strCol & "),1)"
            End If
        Next cl
    Next sht
End Sub

Open in new window

EE.xlsb
0
Flora
Asked:
Flora
  • 8
  • 4
  • 2
  • +1
3 Solutions
 
gowflowCommented:
In your code replace

this line
Set rng2 = sht.Range(cl, cl.End(xlDown))


by this line
Set rng2 = sht.Range(cl, Cells(Rows.Count, cl.Column).End(xlUp))

gowflow
0
 
Rory ArchibaldCommented:
Can't you use a Table? It would make this all unnecessary.
0
 
FloraAuthor Commented:
thanks gowflow.  but it did not work.  becuase my dynamic range has same problem as before.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
FloraAuthor Commented:
Rory,
i wish i could use table, but i cannot.  i need it without table. becuase my boss, gets annoyed with table @references of table.
0
 
gowflowCommented:
You want the name range to have all the first name and then all the Last name ?
its not clear to me what you want.
gowlfow
0
 
James ElliottManaging DirectorCommented:
Sub Create_RangeNames()
    'Creates range names based on header row information
    Dim wbk As Workbook
    Dim sht As Worksheet
    Dim rng, rng2 As Range
    Dim cl As Object
    Dim c As Long
    Dim strAddr As Variant
    Dim strShName, strHdrName, strCol As String
    
    Set wbk = ActiveWorkbook
    For Each sht In wbk.Sheets
        c = sht.Cells.SpecialCells(xlLastCell).Column
        Set rng = sht.Range("A1", sht.Range("A1").Offset(0, c))
        For Each cl In rng
            If cl.Value <> "" Then
                strShName = Replace(sht.Name, " ", "_", 1)
                strHdrName = Replace(cl.Value, " ", "_", 1)
                strAddr = Split(cl.Address, "$")
                strCol = "$" & strAddr(1) & ":$" & strAddr(1)
                Set rng2 = sht.Range(cl, Cells(Rows.Count, cl.Column).End(xlUp))
                ActiveWorkbook.Names.Add Name:=strShName & strHdrName, _
                    RefersTo:="=OFFSET('" & sht.Name & "'!" & cl.Address & ",0,0," & rng2.rows.count & ",1)"
            End If
        Next cl
    Next sht
End Sub

Open in new window

0
 
Rory ArchibaldCommented:
You know you can turn the structured referencing off?
0
 
FloraAuthor Commented:
Thanks James, but your code does not generate dynamic range. it generates fixed.  for example if i add more data the dynamic range does not pick it up.
0
 
gowflowCommented:
pls can you just explain what do you need ?
gowflow
0
 
FloraAuthor Commented:
Hi Rory et all,

basically, what i am looking for is exactly like the previous post that you helped me with. see http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28599260.html

dynamic ranges created by that macro is good and works. but that only looks at the first colum A to consider the last row. and that only create dynamic range for active sheet.

here what i need with this file is basically, to create dynamic ranges, regardless if it is with offset or with Index like the privous post.


what exactly i am looking for is the following

A) when i select the columns on active sheet, it should create dynamic range and the name should reflect from the Header cells. B) if there is a empty cells between the rows for each column it should not cause problem for dynamic range. right now with the code i have, it creates dynamic range but it has problem please see attached file. it only takes up to yellow cells, though the data is more than that.

attached is the example file.
EE.xlsb
0
 
FloraAuthor Commented:
The Macro to put the following name ranges based on the attachment

  BigStr      =REPT("z",255)
DataFirstName      =Data!$A$2:INDEX(Data!$A:$A,DataFirstNameLrow)
DataFirstNameLrow      =MATCH(BigStr,Data!$A:$A)
DataLastName      =Data!$B$2:INDEX(Data!$B:$B,DataLastNameLrow)
DataLastNameLrow      =MATCH(BigStr,Data!$B:$B)



but even in the attached file, when i put the named ranges manually. somehow it does not work. i mean the lastrow do not work. i am confused why.
EE.xlsm
0
 
FloraAuthor Commented:
ok, i found the issue.

all i want my initial VBA code to generate Dynamic range like the below

the BigStr is always has same name

the rest it should follow the Sheetname then the header of column. liek example below.

BigStr      =REPT("z",255)
DataFirstName      =Data!$A$2:INDEX(Data!$A:$A,DataFirstNameLrow)
DataFirstNameLrow      =MATCH(BigStr,Data!$A:$A)
DataLastName      =Data!$B$2:INDEX(Data!$B:$B,DataLastNameLrow)
DataLastNameLrow      =MATCH(BigStr,Data!$B:$B)
EE.xlsm
0
 
gowflowCommented:
if you want my help then you got to bear with me.

I am not familiar with names and setting them but I think I can get you to shore. But before that you need to explain to me the functioning behind creating the names.

When you set say
DataLastName      =Data!$B$2:INDEX(Data!$B:$B,DataLastNameLrow)

what do you expect to have and how would you use it ? in the example of the last workbook you posted.

if you help me understand maybe I can see a way thru.
gowflow
0
 
James ElliottManaging DirectorCommented:
Apologies Flora, I mistook the issue. Whilst not perfect, you could simply put the name of the routine in the worksheet change event, and so my previous code would regenerate the formula correctly every time the worksheet had data added.

Rgds
0
 
FloraAuthor Commented:
Hi gowflow,

thanks alot. I sincerely appreciate your help.

please see attached example workbook.  now that the dynamic named ranges are set. i can use them in Lookup formula and instead of selecting the ranges manually in lookup formula. i can use the named range.

good things about dynamic named ranges are that everything there is more data added to source data, the the named ranges do not have to be updated and that match Lrow takes care of the new data added.

in this workbook, i have added the named dynamic ranges manually, all i wanted is how set up these named ranges using VBA to save time, becuase i have lots of columns and for each of them manually creating named range is time consuming.  

please let me know, if i need to elaborate more.
EE.xlsm
0
 
FloraAuthor Commented:
thank you guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now