define a range in VBA [duplicate] - excel

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?

Related

Using an Excel Range Name in VBA [duplicate]

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

Prevent # signs in formulas [duplicate]

This question already has an answer here:
Excel VBA - How to add dynamic array formula
(1 answer)
Closed 2 years ago.
I was playing around with Excel to try and help out a friend with a problem, and wrote the following macro
Sub test()
Worksheets("Sheet3").Cells(1, 4) = "=SUM(A1:A2)"
End Sub
The idea was to try and figure out if I could create a macro that writes formulas in a specific cell (which we know we could) but also be able to change the range of the formula.
For example, if instead of just 2 values to sum up, I had 4 values, we want the macro to the able to count all the values and then set a range for all the formulas.
The problem here is that when I write the piece of code shown above, for some reason in the cell it appears " =#SUM(A1:A2) ". I have no idea what the "#" symbol is supposed to do nor why it is showing up. As a result though, I get a "name?" error and the function doesn't work. However, if I manually delete the "#" symbol, it works perfectly.
Can anyone explain why the "#" symbol is showing up and how not make it show up ?
This should work :)
As the .Formula is made for excel to recognize that you want to print a formula.
Worksheets("Sheet3").Cells(1, 4).Formula = "=SUM(A1:A2)"

How do I enter a formula in VBA? [duplicate]

This question already has an answer here:
Excel VBA Formula German/French/Italian/Russian/Dutch/Foreign Function
(1 answer)
Closed 3 years ago.
I'm making a bot that erases certain columns and fills them in with new data. By erasing the data, the formula becomes
=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!#REF!,'AP query'!L:L,0)),"Non PO")
So I tried making a macro and filling the cell with the formula but it says there's an error. Is there a fix or another way to enter the formula into the the cell using macros?
Range("B3").Formula = "=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),"Non PO")"
srcWorkbook.Worksheets("Findings").Range("B3").Copy
srcWorkbook.Worksheets("Findings").Range("B3:B" & LR).PasteSpecial xlPasteFormulas
When you wish to use a string in a formula =IF(A1="Hi",... in VBA, just "double up" the quotes:
Range("B3").Formula = "=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),""Non PO"")"
Because, as VBA compiles that line, it'll hit the first quote at "Non Po" and think that's the end of the line. So (...trying to visualize it), it'd be reading that line like your formula is:
=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),
Tangiental tip - sometimes when I hit similar issues, even just pasting the code here on SO, and formatting it as code, helps because as you can see in your post, the Non PO is a different color, but all formulas (strings maybe?) should be the same (all red, as in the answer I posted above).

VBA_Using UsedRange.Count but failed [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 6 years ago.
I met a little problem when I use:
Count_line = ActiveSheet.UsedRange.Rows.Count
to count how many lines do I have in a worksheet. It can not give me the correct number. Can it be influenced by the format of the cells? Cause I've different coulour to highlight some important columns.
If you've got sime idea, please leave a comment. Thank you!
UsedRange.Rows.Count is not a reliable way to pull the last row, as it doesn't account for empty rows at the start of your sheet. Assuming you are finding the last row with text in it, use the End method to find the row.

Excel - How to leave cell blank instead of 0? [duplicate]

This question already has answers here:
Pulling data from big excel datatable with incremental column in Vlookup or IndexMatch without zeros
(2 answers)
Closed 6 years ago.
I'm not using a numerical formula. All I have is a simple reference to another cell.
=(Sheet1!D8)
If that cell is blank, I want to have the cell on this page blank too, not a 0. How do I do that? I want it to appear empty to anyone, not just me, so I don't think simply editing excel settings would work? None of my google search results, including MS office site, have been helpful.
My suggestion:
=IF(ISBLANK(Sheet1!D8),"",Sheet1!D8)
You can force a formula to evaluate as text instead of as a number by adding '& ""' to the end like below, but if you are dealing with numbers this isn't great.
=(Sheet1!D8)&""
See this answer for other ideas.

Resources