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
373 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
[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
  • 6
  • 4
12 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
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.

 

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 500 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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.

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