Is there a way to change a vlookup table_array in VBA? - vba

Basically, I am trying to use VBA to change the VLookup table_array from one tab to another. Currently the VLookup value is something like this
=+vLookup($A4,'02.19.21'!$A$1:$O$9000,F$1,False)
02.19.21 is the name of a tab the data is pulling from, but every week a new tab is created with the most recent data, so the next week the data will need to pull from a new tab that will get created called "02.26.21"
I need vba to change '02.19.21' to '02.26.21', which is where the new data will be pulled from
Edit, I have tried doing a macro recorder, but the issue is that the data is changing weekly, meaining, if I record and change the date to 02.26.21, then when i need to do it for the next week, march 5th, it would not return that data.
I tried the date function in VBA and then used it as a string, since the vlookup would be looking at a tab that is called 02.26.21, but got errors when I did that.

I would consider using a named range for your vlookups instead of a direct sheet+range reference.
See here for named ranges for example: https://www.contextures.com/xlnames01.html
So your vlookup goes from this:
=+vLookup($A4,'02.19.21'!$A$1:$O$9000,F$1,False)
to this:
=+vLookup($A4,LookupTable,F$1,False)
So then when your code needs to change the source sheet, you can do something like:
ThisWorkbook.Names("LookupTable").RefersTo = _
ThisWorkbook.Sheets("02.26.21").Range("A1:O9000")

Related

Referencing a sheet by index number

I've got a LibreOffice Calc spreadsheet that I use to keep track of my accounts receivable at work. Each sheet lists invoices and their status (paid, unpaid, etc) as well as info about each invoice. I'm trying to create a Summary sheet that lists certain data from each sheet. Creating the sheet manually is easy, but I'm trying to "automate" the process. I want the summary page to auto-update if I add a new sheet (or remove one) as I add and remove accounts to the file.
I know that LibreOffice assigns each sheet an index number that I could refer to in some sort of formula, but I cannot find a function that I can use to refer to that index number when getting a value from a cell within it. One would expect that a function like Sheet(2) would reference the second sheet, but, alas, that is not so!
I've tried using the indirect and address functions without success, but I'm not sure if I'm not understanding these functions or if they're not appropriate for what I'm trying to accomplish.
This has been a missing piece in Calc for a long time. The preferred solution is to write a user-defined function. Spreadsheet formulas do not access sheets by index number but Basic can.
The following function is from https://ask.libreoffice.org/en/question/16604/how-do-i-access-the-current-sheet-name-in-formula-to-use-in-indirect/.
Function SheetName(Optional nSheet)
If IsMissing(nSheet) Then
SheetName = ThisComponent.getCurrentController().getActiveSheet().getName()
Else
SheetName = ThisComponent.getSheets().getByIndex(nSheet-1).getName()
EndIf
End Function
Then get a relative address of the first sheet cell A1 like this.
=ADDRESS(1,1,4,,SHEETNAME(1))
A slightly different function is given at https://forum.openoffice.org/en/forum/viewtopic.php?f=9&t=49799.

How can I check the time a cell was edited and set the time and date in other cells? Google script

I have spreadsheet and I'm basically going to use it for inventory tracking and management.
In one sheet I'll have an app on my phone fill a list when it reads
a QR code.
In the second sheet there is also a list with the
specific QR codes of each item.
I need to know when a cell in the first sheet has been updated and put that information in the following two cells.
I'm using this trigger to check if ANY cell in the document has been updated.
function cellEditTrigger()
{
ScriptApp.newTrigger('coolFunction').forSpreadsheet('awsomeSpreadsheetID').onEdit().create();
}
It works fine and it runs the 'coolFunction' function. However I can't seem to be able to get the time and date for the cells that have been edited and put that information in the next two cells.
I know I could use onEdit(e), but I still can't get the information from e.range and I would like my script to not have to be bound to the Spreadsheet.
Is there any way to accomplish this?

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

Vlookup across different sheets

I am trying to do a vlookup across several sheets within the same workbook:
=IF(ISNA(VLOOKUP(A2,Regulares!J:L,3,0),ISNA(VLOOKUP('Temp Activos'!G:I,3,0),ISNA(VLOOKUP(A2,'Temp JA'!G:I,3,0),VLOOKUP(A2,'Temp Fit'!G:I,3,0)))))
But I keep getting the error that I have too many arguments???
I would also like to make a macro to add this vlookup to a cell in one of my sheets (PS), and bring the formula down to the last row (fill handle) upon pressing a button, but first need to figure out why it wont work before plugging it into a code...
The ISNA() function just needs one parameter, so your parentheses are out of sync. If you are using Excel 2007 or later, you can more easily use the IFERROR() function which has two parameter, the second being the value to return in case of error, which would be the next VLOOKUP in your case.

Use Absolute Naming for Worksheets when used with VBA

I have the following problem: In one of my excel files, i have the following worksheets: One in which I have a number of formulas, one with the data for the current month (which gets copied&pasted from another source into there), and a couple of worksheets that are used as some sort of archive.
I use a little VBA macro to move the data from the current month into the archive.
Sheets("Aktueller Monat").Cells.Cut
Sheets("1").Activate
Range("A1").Select
ActiveSheet.Paste
The moving part of it works fine. The problem is now that everytime i move the current month into the archive, it updates all the formula in my calc sheet so that they are now referencing the archive (which I don't want to, since the formulas should be calculating the new current month instead)
For example, this
=ZĂ„HLENWENN('Aktueller Monat'!$C:$C;"RSS*")
becomes this
=ZĂ„HLENWENN('1'!$C:$C;"RSS*")
and I don't want that.
Can anybody help me?
Instead of using Cut and Paste, use Copy and PasteSpecial, followed by Clear or ClearContents. In the example below I paste the values and number formats, but you can adjust this to your needs using different values of the XlPasteType Enumeration:
Dim oSourceRange As Range, oTargetRange As Range
Set oSourceRange = Worksheets("Aktueller Monat").UsedRange
Set oTargetRange = Worksheets("1").Range("A1")
oSourceRange.Copy
oTargetRange.PasteSpecial xlPasteValuesAndNumberFormats
oSourceRange.ClearContents