Using a Defined Name (Dynamic List) in Code

vikrampnz

Board Regular
Joined
Jun 20, 2006
Messages
111
Office Version
  1. 2007
Platform
  1. Windows
Hello

I have a piece of code which works well for its intended purpose and has part where certain worksheet names are specified which are to be excluded for the list that is made by using that code. (See the sample below)

VBA Code:
Dim xSheet As Worksheet
Dim xRow As Integer
Dim calcState As Long
Dim scrUpdateState As Long

For Each xSheet In Application.Worksheets
If IsError(Application.Match(xSheet.Name, _
Array(Me.Name, "Reports", "Trial List", "Original Template"), 0)) Then
xRow = xRow + 1

In the above code, "Reports", "Trial List", "Original Template" are the three worksheets that will be excluded from the displayed list. I have created a table ("Table2") and am adding the names of worksheets to be excluded in that table in the column named "Ignore_L", as its much easier for me to add 3-4 more names in the table than to add in those sheetnames in the code directly every time. Purpose of inserting table was that the range automatically extends everytime a new name is added to the list. I have defined a name for this column in the table as "Ignore_List".

I have tried referencing the Defined Name "Ignore_List" in my code with no luck. It either gives an error or doesn't work.

Would appreciate any help from the group !

Cheers
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
If you have made a formal table (which it sounds like you have) then you don't also need a named range. You should be able to use something like this (checking the table name of course)

VBA Code:
If IsError(Application.Match(xSheet.Name, Range("Table1[Ignore_List]"), 0)) Then
  xRow = xRow + 1
End If


BTW, if you want to format part of your posted code yourself, you need to use the 'rich' code tags, not 'vba' code tags.

1588163754325.png
 
Upvote 0
Hi Peter_SSs
Thanks for your response.

Unfortunately, I tried to reference it the way you suggested but its clearing the entire list and not just the worksheets listed in the Ignore_List.

Not sure whats going wrong !
 
Upvote 0
its clearing the entire list and not just the worksheets listed in the Ignore_List
There is nothing in the code you posted about clearing anything so there is no way for me to see if I can replicate what you are describing.

Perhaps you could post your actual code?

.. and a small sample of two worksheets (one on the ignore list and one not) using XL2BB so that we can easily copy to test with?

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Hello...I am using Excel 2007....have updated the same in my profile ......I have a sample sheet....but I am struggling to see where I can upload it from....please guide on how to upload the sample sheet...thanks
 
Upvote 0
please guide on how to upload the sample sheet...thanks
See the XL2BB link in my previous post.
If you want to share an actual workbook, you need to upload to DropBox/OneDrive/GoogleDrive etc and provide a shared link here.
 
Upvote 0
Heres the link
Thanks, but as far as I can see that has your original method ..
VBA Code:
If IsError(Application.Match(xSheet.Name, _
Array(Me.Name, "Reports", "Trial List", "Original Template"), 0)) Then
You said you had tried my method, but I cannot see that attempt. What was your actual replacement code for the above line that you tried?


Thanks for updating your profile. (y)
 
Upvote 0
Heres the link for the workbook with the change in code you suggested.
Thanks.
What you have (just combined onto a single code line) is
Rich (BB code):
If IsError(Application.Match(xSheet.Name, Array(Me.Name, Range("Ignore_List[IGNORE]")), 0)) Then

There are a couple of issues.
  1. There is no Array() in what I suggested.

  2. You hadn't told us that this was part of a worksheet 'event' code (Worksheet_Activate) in the 'Summary' worksheet module and that the table you had created was on a different worksheet.
    The problem here is that in your code Range("Ignore_List[IGNORE]") will be looking on the Summary sheet for that table/range and naturally cannot find it.

If you do not want that part of your code to act on the Summary sheet, then add that name to the Ignore_List table.
Then modify my code only to point to the sheet where the Ignore_List table is

Rich (BB code):
If IsError(Application.Match(xSheet.Name, Sheets("DataMaster").Range("Ignore_List[IGNORE]"), 0)) Then
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top