Solved

Excel VBA Modification Dynamic

Posted on 2015-01-19
16
80 Views
Last Modified: 2015-01-19
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
Comment
Question by:Flora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
  • +1
16 Comments
 
LVL 29

Assisted Solution

by:gowflow
gowflow earned 166 total points
ID: 40557940
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 40557945
Can't you use a Table? It would make this all unnecessary.
0
 
LVL 6

Author Comment

by:Flora
ID: 40557949
thanks gowflow.  but it did not work.  becuase my dynamic range has same problem as before.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Author Comment

by:Flora
ID: 40557951
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
 
LVL 29

Expert Comment

by:gowflow
ID: 40557961
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
 
LVL 12

Assisted Solution

by:James Elliott
James Elliott earned 166 total points
ID: 40557973
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
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 168 total points
ID: 40557975
You know you can turn the structured referencing off?
0
 
LVL 6

Author Comment

by:Flora
ID: 40558003
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
 
LVL 29

Expert Comment

by:gowflow
ID: 40558008
pls can you just explain what do you need ?
gowflow
0
 
LVL 6

Author Comment

by:Flora
ID: 40558019
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
 
LVL 6

Author Comment

by:Flora
ID: 40558095
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
 
LVL 6

Author Comment

by:Flora
ID: 40558108
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
 
LVL 29

Expert Comment

by:gowflow
ID: 40558149
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
 
LVL 12

Expert Comment

by:James Elliott
ID: 40558277
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
 
LVL 6

Author Comment

by:Flora
ID: 40558341
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
 
LVL 6

Author Closing Comment

by:Flora
ID: 40558807
thank you guys
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question