Solved

Excel VBA Modification Dynamic

Posted on 2015-01-19
16
76 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms office troubleshooting for users 8 36
sql server query from excel 3 57
Auto populate in Cascade dropdown 3 27
Access Excel export not behaving 2 25
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

896 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

11 Experts available now in Live!

Get 1:1 Help Now