Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Excel VBA: Write to an external xls file name ranges and apply to cells in a specified worksheet

Posted on 2011-03-13
12
Medium Priority
?
405 Views
Last Modified: 2012-05-11
Hi, I have a VBA code that takes data from file (A) and writes it to an another xls file (B).
I want to create in file B, through VBA, name ranges referring to cell values defined in B- worksheet "Indx" and have them applied to specified ranges in B - worksheet "Data".
The objective is to have the file prepared for a user to enter their updates using just the options specified in the names ranges.

I need somebody to build for me code to be used in file A to accomplish just that.
I have attached a flle B.

Thank you in advance for our kind assistance.

Cheers, N2V Status-File---Example.xls
0
Comment
Question by:NewToVBA
  • 6
  • 4
10 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35122822
To add references see this sample code. Please amend as per realistic situations

oBook.Names.Add Name:="BookMarkName", RefersTo:="=Indx!G1:G6"

oBook is the workbook object where you want to create the named range.
BookMarkName is the name of the bookmark that you want to create
=Indx!G1:G6 is the sheetname and the relevant range.

Sid
0
 

Author Comment

by:NewToVBA
ID: 35123876
HI Sid,

Sorry, I need more guidance, I am too green with this stuff!
I would appreciate if you could drop me a sample file which creates in B sheet "Indx", lists there the ranges of values as shown in my file, defineds them as names, and then creates validations for cells in Data sheet of the provided file, under Category and Status....

Thank you

NV
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35125143
>>>Sorry, I need more guidance, I am too green with this stuff!

Sure not a problem. :)

I see you mentioned about File B in your original question. Can you break point wise what you exactly want and then we will convert that into VBA code. :)

Sid
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NewToVBA
ID: 35129428
Excellent, thank you Sid!

I attached this time two files, File A = master inventory file, File B = report file built from data subest of File A.
Objectives:
File B is a report per Supplier, and its purpose is to allow user modify records only within valid ranges. Selected ranges are copied from File A - worksheet "Indx" upon creating File B.
So, this is how it supposed to work.

1. Administrator of File A  selects a Supplier, e,g, "AAA" , and a macro picks up all belonging records and write to file B - worksheet "Data", using the same headers and other content.

2. A macro creates in File B a worksheet "Indx" with "Category" and "Status" lists copied from File B - worksheet "Indx". Both ranges define validation references in File B - worksheet "Data", Column B and E ("Category" and "Status"). Close File B.

Hope this makes sense :-)
Cheers,

N2V File-A.xls
File-B.xls
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35130137
Ok I got it. :)

So do you have the code to write from File A to File B per Supplier or do you want me to write that as well?

Sid
0
 

Author Comment

by:NewToVBA
ID: 35131483
Ha! I got a code, but to be perfectly honest with you, I would love to learn from you how an efficient one is to look like.... So, please do me this great favor :-)

Cheers,
N2V

0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35134480
>>>> Ha! I got a code, but to be perfectly honest with you, I would love to learn from you how an efficient one is to look like.... So, please do me this great favor :-)

LOL :)

Ok let me write it from scratch but it might not be perfect ;)

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35175138
N2V: Sorry I was busy. Working on it now.

BTW, if you are around and if you want to save time, can you upload your version and maybe I can amend it. Otherwise, I will write it from the scratch.

Sid
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 2000 total points
ID: 35175171
Ok. This was fast :)

Please find attached a sample file. When you open the file you will see that there is no validation in Category and Status in sheet "Data". When you run the Sub Sample() in module 1 you will see that the named range is created and the data validation is applied. I have also commented the code so that it is easier to understand.

Let me know if you have any queries. :)

Sid

Code Used

Sub Sample()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim ws1LastColARow As Long, ws1LastColBRow, ws2LastRow As Long
    Dim ws1Rng1 As Range, ws1Rng2 As Range
    
    '~~> Set the worksheets
    Set ws1 = Sheets("Indx")
    Set ws2 = Sheets("Data")
    
    '~~> Get the last Row of Category and Status
    ws1LastColARow = ws1.Range("A" & Rows.Count).End(xlUp).Row
    ws1LastColBRow = ws1.Range("B" & Rows.Count).End(xlUp).Row
    
    '~~> Set the range in the indx sheet on which the names will depend
    Set ws1Rng1 = ws1.Range("A2:A" & ws1LastColARow)
    Set ws1Rng2 = ws1.Range("B2:B" & ws1LastColARow)
    
    '~~> Delete the names if they exist
    On Error Resume Next
    ActiveWorkbook.Names("Category").Delete
    ActiveWorkbook.Names("Status").Delete
    On Error GoTo 0
    
    '~~> Create Names from scratch
    ActiveWorkbook.Names.Add Name:="Category", RefersTo:="=" & ws1.Name & "!" & ws1Rng1.Address
    ActiveWorkbook.Names.Add Name:="Status", RefersTo:="=" & ws2.Name & "!" & ws1Rng2.Address
    
    '~~> Get the last row in Sheet Data
    ws2LastRow = ws2.Range("A" & Rows.Count).End(xlUp).Row
    
    '~~> Add the data validation for Category
    With ws2.Range("B2:B" & ws2LastRow).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=Category"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
    
    '~~> Add the data validation for Status
    With ws2.Range("E2:E" & ws2LastRow).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=Status"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub

Open in new window

File-B.xls
0
 

Author Closing Comment

by:NewToVBA
ID: 35243724
As always, Syd rocks!

Many thanks, Syd, you are a genius!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

581 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