Question : Microsoft Excel - Change a Named Range With Code

Currently, I have a Named Range on a Worksheet (in a workbook) titled "BizCode".

Upon opening the Workbook, the Named Range is already initially set to =ActivityCodes!$B$20:$B$33

This Named Range is used as a drop-down list on the Invoice worksheet.

We currently have 4-5 workbooks like this and the only difference between each, is the actual data within the BizCode named range.  What we would like to do is combine all of these workbooks into just one, but would then need a way to change the cell reference of this named range.

In our minds, it would work like this...The user opens with Workbook (which opens to the Invoice worksheet) and selects the type of invoice they filling (selected from a popup form with the list of Invoice types).  Upon clicking 'OK", the BizCode named range cell reference is changed.

How do we change the cell references of a Named Range through code?

FYI - the workbooks must remain in Excel 97-2003 format.

Answer : Microsoft Excel - Change a Named Range With Code

Alright...so after many hours of research, I found the answer to my question as it was originally stated...

"In our minds, it would work like this...The user opens with Workbook (which opens to the Invoice worksheet) and selects the type of invoice they filling (selected from a popup form with the list of Invoice types).  Upon clicking 'OK", the BizCode named range cell reference is changed."

Assume the main worksheet is called "Invoice" and the business codes worksheet is called "BusinessCodes".  Also assume there are 3 named ranges (you could have however many you wanted), titled "BusCodes", "TravelCodes" and "ActivityCodes".  Each of the named ranges (just mentioned) refer to a single table of codes.  There are (as mentioned in the first post), several variations of each table as each department within the company uses different codes for the purpose of accounting clarity and segregation.  Thus the need to be able to change the named range referenced on demand depending on the type of Invoice being filled out.  Again, the invoice layout for each department is exactly the same, only the table codes change.

Thus, the simplest solution which does not require a redesign of the workbook would be as follows:

1) Create a Form which includes a 'check box' (or listbox), that contains the different Invoices available for use.

2) Use an 'on click' event to initiate the Named Range reference change.  Using a 'check box', the VB code would appear as follows....


Private Sub InvoiceType1_CheckBox_Click()

If Me.InvoiceType1_CheckBox.Value = true Then

   Sheets("BusinessCodes").Visible = True
   Sheets("BusinessCodes").Select
   Range("A1:A100").Name = "BusCodes"
   Range("B1:B100").Name = "TravelCodes"
   Range("C1:C100").Name = "ActivityCodes"
   Sheets("BusinessCodes").Visible = False
    Sheets("Invoice").Select
   Application.ScreenUpdating = True
   Exit Sub
   Unload Me

Else

'replicate above code for each Invoice Type option.

End If
End Sub

Of course, this code could be used regardless of where the tables are located or how they are laid out because the code explicitly names each range and worksheet.  

With the above code, non-technical individuals could continue to add/delete/change information in the ranged tables and as long as the table data did not exceed the range.

No doubt, this code could be modified to dynamically locate  the end of each table, which would then remove any need for an IT person to ever modify the coding again except to add or remove available Invoice Type options...but the beauty of this coding is that it's  simple.  

Random Solutions  
 
programming4us programming4us