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

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

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 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

12 Experts available now in Live!

Get 1:1 Help Now