Using an Excel Range Name in VBA [duplicate] - excel

This question already has answers here:
How do I use RefersToRange?
(3 answers)
Selecting a Named Range Cell Location EXCEL VBA
(1 answer)
Closed 3 months ago.
I'm trying to use an existing workbook Range Name "value" in VBA.
I use cell E1 on a Config Tab that has been named "GridStartDate".
Looking at the Name Manager in Excel I see that the Value is 9/1/2022 and the "Refers to" is =Config!$E$1
I want to simply refer to the Sheet Name Range instead of the explicit cell
ShiftCells = DateDiff("d", Sheets("Config").Range("E1").Value, StartingDate)
With something like this:
ShiftCells = DateDiff("d", ActiveWorkbook.Names("GridStartDate").Value, StartingDate)
I'm concerned that if someone should add a column or row in the Config sheet it may move my GridStartField and therefor cause the above formula to fail.
I get a type mismatch when I use the revised codes because the Value is actually returning the refersto value it seems. I find all sorts of information on adding Ranges with VBA but I don't understand how to use the value after the fact.
Thanks
Rob

Related

VBA - user input of row value to use in a cell range [duplicate]

This question already has answers here:
How can I insert variable into formula in VBA
(2 answers)
Closed 14 days ago.
I'm trying to create a macro to copy and paste a certain section of a row to another sheet. I'm getting the user to input the row they want the copy/paste to work from.
I've created a variable (Dim c) for the user to input the row value, and want to use that variable in a range, but don't know how.
E.g.
Range("C5:I5").Copy Range("C45:I45")
In the first step of this I want the '5' to be replaced by variable c, but how do I call that variable in this statement?
Tried Range("C(Val(c)):I(Val(c))"), but that hasn't worked.
Apologies for rookie errors, but I'm fairly new to VBA.
Got it eventually using this -
Range("C" & c & ":I" & c)

Vlookup function showing error N/A inspite of the value [duplicate]

This question already has answers here:
vlookup error in excel [closed]
(3 answers)
Excel VLOOKUP where the key is not in the first column
(1 answer)
Vlookup limitations- want to transfer backwards
(1 answer)
VLOOKUP N/A Error
(2 answers)
Closed 6 months ago.
I have two worksheet named booklist and author.I am trying to get the Id of author from the worksheet author and use it in the boolist worksheet in the writer code column. I have written the formula like below-
=VLOOKUP(C2,author!A2:D419,1,0)
But it is not working kindly help me aht is wrong in this
It is showing error kindly help me to do that.
VLOOKUP() will not work in this case because vlookup always look for value in first column and return data from another column of corresponding row. You author sheet first column is ID and you are looking for writer name. So, it will never match. Try below formula instead.
=INDEX(author!$A$2:$A$500,MATCH(C2,author!$C$2:$C$500,0))
Or XLOOKUP() like-
=XLOOKUP(C2,author!$C$2:$C$500,author!$A$2:$A$500)

Index function to pull value from a closed workbook, but use cell value for sheet name

I have 2 workbooks and within each book, each sheet are the months of the year. The source book and target book have identical sheet names, eg. Aug 2019, Sep 2019..... I have created the following formula in the target book, cell B9, to pull a value from the source workbook, cell AJ46:
=INDEX('C:\FTPDownloads\Villa Stuff[Occupancy Chart Rev 1d.xlsx]Aug 2019'!$AJ$46,1)
It works fine, even with the source book closed.
In the target book I have loaded cell Q3 with the following formula :
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)
This loads cell Q3 in the target book with the current active sheet. So cell Q3's value is Aug 2019.
There are 20 copies of the INDEX formula in the target book, and the only thing that changes is the source cell at the end, which in the formula above is AJ46.
To prevent constant changing of the INDEX formula when I extend the sheets to future months, I would like to use the value in cell Q3 in place of entering the actual sheet name.
I have tried :
=INDEX('C:\FTPDownloads\Villa Stuff[Occupancy Chart Rev 1d.xlsx]"&Q3&"'!$AJ$46,1)
But it produces an error.
I am using Excel 2007.
Firstly, if you only want to return a specific cell, say AJ46, why won't you just reference the cell address directly rather than using an INDEX function? I am sure the following will also return the desired value regardless if the other workbook is open or not.
='C:\FTPDownloads\Villa Stuff[Occupancy Chart Rev 1d.xlsx]Aug 2019'!$AJ$46
Secondly, there have been ample discussions on this topic online and even in this community as in this post. The simple conclusion is:
there is no way to make a dynamic workbook/worksheet/range/cell name using excel formula and return the desired value from a closed workbook.
Thirdly, it can be done using VBA. If it is an option for you, you may want to add #vba to your tags so other contributors may be able to help you as #vba it not my expertise. Someone in the post I mentioned before provided a vba code that works for direct cell reference but not incorporating INDEX. If that's an option for you you may want to give it a go.
Fourthly, for your information it can also be done using PowerQuery but it is only available in Excel 2010 and later versions. Besides, PowerQuery worked best with large raw data-set rather than structured report anyway.

MATCH function suddenly stopped working

I wrote this VBA macro for Excel code many years ago and it has suddenly stopped working. I'm trying to find the row index in one sheet that exactly matches an entry in the current sheet. The line of code that has stopped working is:
Cells(rr, cc + 1).Value = "=MATCH(RC[-1], Num, 0)"
I'm not sure what Num is, since it isn't referenced anywhere in the macro earlier. In particular, I don't see how it references the worksheet i'm interested in.
If you were trying to put a formula into the cell defined by row:=rr, column:=cc+1 then the code really should be,
Cells(rr, cc + 1).FORMULAR1C1 = "=MATCH(RC[-1], Num, 0)"
(sorry - all caps for emphasis) That formula would make use of a Named Range called Num. Check Formulas ► Defined Names ► Name Manager for its existence. It will refer to a single row or column of cells where the lookup for the value of one cell to the left is performed.
If you try and put an xlR1C1 style formula in as the .Value, it may be correctly interpreted by the worksheet overhead as a formula. However, without a defined Num range, this would produce the #NAME? error on the worksheet.
The problem turned out to be a bad data entry in the worksheet I was trying to MATCH to. I don't know why it didn't just return a FALSE value. However, I have solved my problem thanks to the help from this site.
Thanks.

define a range in VBA [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Last not empty cell (column) in the given row; Excel VBA
(2 answers)
Closed 8 years ago.
I have a range of data that will be between columns B and D inclusive.
I can get the top left most range as this will be constant, the only thing that varies is the bottom right cell.
I tried obtaining the bottom right cell via using xltoright and then xltobottom. However I am having difficulties obtaining the cell address of this bottom right cell.
Has anyone come across this problem and if so how did you over come the issue?
change xltoright and then xltobottom, to : xltoleft and then xlUp.
Check the answers you get from it in the immédiate window of VB editor (Ctrl+G)
if still doesn't work maybe use:
Range(Range_Adddress).cells.(Range(RangeAddress).cells.count).address
to get to the last cell's address of your range (I named it Range_Address, which has to be a string in the example).
Does this help you?

Resources