Solved

Excel VBA Modification Dynamic

Posted on 2015-01-19
16
75 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
  • 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 5

Author Comment

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

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 5

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 29

Expert Comment

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

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 5

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 5

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 5

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 5

Author Closing Comment

by:Flora
ID: 40558807
thank you guys
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now