I have data arranged in Excel like so:
Year|Trade Flow|Commodity Code|Commodity|Qty Unit|Qty|Net Weight|Trade Value
I have an IF formula:
=IF(D2=D1,0,IF(D3=D2,ABS(I3-I2),I2))
which I need to run for all the worksheets in the file, using VBA.
I am assuming you know how to find the last cell (plenty of examples on this site), or have a fixed size.
I used column H just for an example.
for each sh in sheets
sh.range("H2:H20").Formula="=IF(D2=D1,0,IF(D3=D2,ABS(I3-I2),I2))"
next
alter this to suit your needs
Related
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.
I'm try to write a macro that will auto update stock by taking the ammount used from one cell, subtracting it from a total in a different sheet.
Sheet view:
So in this instance - I want to subtract "kgs" which is cell range 'I17:I24' from what is "kgs available" which is held in the sheet "Malt" and updated from a VLOOKUP defined from the "Batch Code" in the Brew Sheet.
I'm guessing the code would probably have to VLOOKUP that batch code in the Malt sheet and subtract the "kgs" in the "Brew Sheet" sheet from the "kgs available" in the "Malt" sheet (Column L)
What also might contribute complications is if the cells in range E17:24, which contain the Batch Codes for the VLOOKUP have no values. Would that throw up errors, if though I may not need to use all of the cells in that range? (If that makes sense - sometimes, I may only need to use 1/2 of the 8 cells in the range)
Apologises in advance if my question is vague. My coding knowledge is very limited.
Thanks for you help.
you need to get the sum of KGs in range E17:24 where the batch code matched the batch code on the malt sheet
=sumif(Malt!$E$17:$E$24,L17,Malt!$I$17:$I$24)
Then all you need to do is subtract that from the available value in column I
=I17-sumif(Malt!$E$17:$E$24,L17,Malt!$I$17:$I$24)
Not sure if I have the columns correct.
I have multiple sheets of data and I want to make it in one sheet (All of them are in the same workbook). Link to the excel file.
I tried to use Hlookup function in excel file, something like below:
=HLOOKUP("University",Sheet1!$A$1:$G$2, 2, FALSE).
But, since I have more than 100 sheets of data, I want to find a way to drag the function and auto generate the function below the 2nd row. I have tried to use indirect function by setting a reference column in front as below but cannot deal with it.
=HLOOKUP("University", 'INDIRECT(A3)'!$A$1:$G$2, 2, FALSE)
My next option is VB code. But, I am new to VB. Anybody can help on it?
Place your individual sheet names in column H of the Summary sheet and the row number in column I (as helper columns) and write this formula in cell A2 of the summary sheet.
=IFERROR(HLOOKUP(A$1,INDIRECT($H2&"!A1:G"&$I2),$I2,0),)
and drag to column F and down for as many sheet rows combos you have. I used 10 rows but you can obviously make it longer or shorter as neeed.
When you are done you can filter on 0 in column A and remove any lines with no data.
If your sheet names have spaces in them, you'll need to adjust the INDIRECT formula to this:
INDIRECT("'"&$H2&"'!A1:G"&$I2)
best way would be "defined names" + INDIRECT + HLOOKUP (or LOOKUP) like:
defined names
name: SList
formula: =MID(TRANSPOSE(GET.WORKBOOK(1))&T(NOW()),FIND("]",TRANSPOSE(GET.WORKBOOK(1))&T(NOW()))+1,255)
formula in cells: (this in A2 then simply autofill to G2 and thenn everything down) (you'll get a row with 0's between the sheets, which can be filtered out or deleted later (copy/paste values))
=IFERROR(HLOOKUP(A$1,INDIRECT("'"&INDEX(SList,COUNTIF($A$1:$A1,0)+2)&"'!$A:$G"),$H2,0),"")
Set H2 to 2 and for H3: (autofill down from H3)
=MAX(($H2+1)*($A2>0),2)
works perfectly for me LINK
No manual typing of sheetnames or something like that (only Column H:H as helper). Youll get rows's with 0's every time a new sheet is selected which can be filtered out. (or if you copy/paste values also can be deleted)
the +2 at ...st,COUNTIF($A$1:$A1,0)+2)&... simply tells to start with sheet 2 (if summary is the first). You may change it to +1 if you want to lookup starting with the first sheet.
Assuming you already have all 100+ sheet names typed out in column A, this will work whether or not you have spaces in the sheet names:
=HLOOKUP("University", OFFSET(INDIRECT(ADDRESS(1,1,1,1,A2)),0,0,2,7),2,FALSE)
I wish to create a dynamic Print_Area in Excel 2010 which will consist of two cell ranges.
For example the first cell range is A1:J50 and the second range is A100:J150. These should print out on two pages, ignoring the cells that come in between these two ranges.
The four cells shown in the above example ranges should be dynamic, and not hard coded as simple Print_Area ranges. Therefore in my worksheet I used cells AA1, AB1, AC1 and AD1 to store values "A1", "J50", "A100" and "J150" respectively.
(The cells AA1, AB1, AC1 and AD1 actually use formulas to determine what cell address will be used, but for this question lets just assume the values are set as above).
I then used the Name Manager and entered the following formula under Print_Area:
=INDIRECT(Sheet1!$AA$1):INDIRECT(Sheet1!$AB$1);INDIRECT(Sheet1!$AC$1):INDIRECT(Sheet1!$AD$1)
The result of this formula is exactly what I need, and it actually works the first time I print the ranges. However once I did that, Excel automatically substitutes the formula with the actual cell range that was calculated. So when I check the Print_Area in the Name Manager after printing once, it contains something like:
=Sheet1!$A$1:$J$50,Sheet1!$A$100:$J$150
Is there a way to prevent the Print_Area from converting my formula to calculated values, and instead using the formula every time I print? I would like to not use macros if at all possible (if not, I'll try macros too)
I tested this and it seemed to work.
Create a new name called Test and set its value to (Note that I used a comma rather than the semicolon you had. I have US language set)
=INDIRECT(Sheet1!$AA$1):INDIRECT(Sheet1!$AA$2),INDIRECT(Sheet1!$AA$3):INDIRECT(Sheet1!$AA$4)
Set your Print_Area name to
=TEST
Good luck!
EDIT
The above works for me, but it appears unnecessary. I just tried to replicate the problem, and was unable. When I have the Print_Area set to the formula with INDIRECT it does not replace after printing.
See this linked file. https://www.dropbox.com/s/pgm0iv19u6igdm5/Book1.xlsx
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