Macro Referencing Named Range In Worksheets Private Module
Sep 12, 2006
I'm having trouble calling a defined range within a VLOOKUP function in VBA. If the named range is located on the same sheet within which you are running the macro, everything runs fine and all is well in the world.
However, after I relocated the range to a separate sheet (a 'SourceData' sheet to tidy up the user interface sheet), I was getting the following error message:
Method 'Range' of object '_Worksheet' failed
The name is correctly defined - Range("DaysInYear").Select still picks up the correct selection - it's just the VLOOKUP will no longer function correctly.
Here is part of the macro's
For I = 1 To NumberOfDays
Range("A1").Value = DateAdd("d", -(I - 1), EndDate)
If Application.VLookup(Range("A1"), Range("DaysInYear"), 3, False) = 1 Then
If Application.VLookup(Range("A1"), Range("DaysInYear"), 4, False) = 0 Then
ActualNumber = ActualNumber + 1
I've got a problem referring to a named range in another worksheet.
Private Sub test() Dim var_StartWeek As String ' 0740 This is the value I make a lookup for Dim var StartWeekNr As Integer ' 40 and get this as a result
'*** Here is the named range in another worksheet (It doesn't work) Set rng = ActiveWorkbook.Names("WeekData2").RefersToRange var_StartWeekNr = WorksheetFunction.HLookup(var_StartWeek, Range("rng.Value"), 4) ' I'm doing something wrong here...
'*** If I have the named range in the same worksheeet it works fine 'var_StartWeekNr = WorksheetFunction.HLookup(var_StartWeek, ActiveSheet.Range("WeekData"), 4)
When in a ThisWorkbook macro, I want to call a sub/macro in a Module. Usually you can just do a CALL MACRO1, and it will find it. But my MACRO1 is Private as I don't the users to be able to do a Tools, Macro, Macros and see it. So I don't want to make it a Public macro. So how do I call the private sub/macro from within ThisWorkbook?
Can I proceed the sub/macro's name with the name of the module, kind of like CALL MODULE1.MACRO1? Or do I have to make it public?
I have a sheet that has a macro on it, I would like to run that macro programaticaly but Im not able to call it using Call mymacro type of code. The code that calls the macro resides in a module and the macro is in a sheet. Obviously they are not talking, is there a way to run it remotely without moving the macro into the module?
I would like the users to be able to exit the whole module via double clicking the form. When the form is double clicked, the double click event appears and I am wondering how to put code into this procedure which exits the whole module.
I want to export data from a set of named ranges in workbook x to another set of ranges in workbook y. The names of the ranges to be exported from and to are spesified in a table "Table_Export". Problem is I do not want to spesify in what sheets these respective ranges are located, seeing that I do not know the actual sheet names. But since the ranges are global it should somehow be possible to reference them in vba without referencing the respective worksheet?
The relevant code can be found below where either of the two 'commented lines within the if statement are what I want to accomplish.
Dim x As Workbook Dim y As Workbook Set x = ThisWorkbook Set y = Workbooks.Open(Range("Export_to").Value) Dim export_control As ListObject
I have a challenge I'm trying to surmount. I have an excel sheet in which I want to be able to select Zone codes from a drop down, click on a link "search" which takes me to a place in the document that has been named according to the Zone code i pick in the drop-down. I want to achieve this using only formulas as i'm distributing the file to a wide audience who are not versed enough with computers to know how to enable macros.
I some code that reduces a long list of dates from one source of data (in its own worksheet) down to a unique list of dates (on another worksheet). I need to create a new worksheet for each date in the range (named for the date), and then allocate each line item for that date in the source data into its repsective worksheet.
After all the awesome macros I've obtained with the help of all of you, I now have over 30 macros, each in its own module. I have tried without success to re-name the modules with no luck. How is everyone organizing these?
Attached is a master metrics deck, where data needs to be refreshed every week, from two different workbooks.
The catch is, the two parent workbooks would be new versions every week (data would be same format, and style within), but file name would change each week, for eg: for 1st workbook, 'XXX_Weekly_Week24.xlsx' to 'XXX_Weekly_Week25.xlsx' and so on.
for 2nd workbook, 'YYY_Weekly_Week24.xlsx' to 'YYY_Weekly_Week25.xlsx' and so on.
In the master metrics deck, a consolidated vew of some of data from workbook 1 and workbook 2 is presented for each week.
This master file would remain same, and only be refreshed each week (preferably with a button on its sheet "REFRESH!!")
Next, need to manually change cells C2:G2 each week to reflect week titles correctly.
For e.g.: week6 would show week 1 through week 5, but during week7, it needs to show week 2 to week 6. Only last five weeks of data any given time.
The first datarows of each set are conditionally formatted (they are compared with other rows in the set and need to reflect danger values, good values etc) (I have done that already).
Ok I have been trying to figure this out for way to long. Say I have a list of values in A1:A4. I have named this range MyRange. If I wanted to refer to this range somewhere else in the workbook and enter =MyRange it will only work provided the "=MyRange" is in the same rows as the original data.
For example if I enter MyRange in cells C1 through C4 it would mirror the data in column A. But if I enter MyRange in C5:C8 I would get a #Value error. Does anyone have any ideas to make this more of an absolute reference.
Basically anywhere I enter MyRange into a cell I want to see A1. If i drag that formula down I want to see A2, A3, A4, etc....
I have tried using PULL from Harlan Grove's posts to workaround this but am coming up with #VALUE errors.
Here's what I have:
Column B contains the acct # being referenced e.g. 5230 Column E="_"&Br where r is the row # Column F=MATCH($B$3,INDIRECT(Er&"Rows")) where _acct#Rows is a named range referring to an external workbook e.g. _5230Rows Columns G-R=INDEX(INDIRECT($Er),$Fr,COLUMN(G$6)) where_acct# is a named range referencing an external workbook e.g. _5230
I thought that INDEX($Er, $Fr, COLUMN(G$6)) should work in Columns G-R however it returns #REF!
I need a solution to replace INDIRECT so I do not have to have both workbooks open together. The named ranges are static, but reference external workbooks.
I have a named range that is being updated via a macro. After the update, I want the list to be sorted. The named range is "Projects". This name is not a variable...it is a named range within Excel itself.
Any code to sort this at the end of my existing macro.
In my sheet I have a range on the go called 'cells_replen', I then need to search through the sheet and if a cell matching a given criteria is found I want to add it to the named range. The idea is that when I've done all the adding I can just select the range and then go back through the cells later on without searching for them all over again. The first code that declares the first cell I need in the range is:
I have 3 different sheets with a private sub on each all labelled
Private Sub CommandButton1_Click()
The macro's runs fine on each page.
I want to put a macro on a separate sheet that i can run each of those macro's
I did initially copy the original private macro and change the
Private Sub CommandButton1_Click()
But I couldn't do all 3 in the same manner. i did change each name to something different... the other 2 subs did run, but they didn't do anything except put the massage box on the end saying that "the macro has finished".