Excel MIN and MAX function not consistent across worksheets - excel-formula

This Excel formula works OK when entered on the same worksheet as the target range of data but returns 0 when inserted in any other sheet in the same workbook.
=MIN(INDIRECT((ADDRESS(ROW(INDIRECT(EventID_2_Start)),COLUMN(TopTemp), 1, 1) & ":" & ADDRESS(ROW(INDIRECT(EventID_2_End)),COLUMN(BtmTemp), 1, 1))))
I have an equivalent MAX function which gives the same result.
If I strip the MIN/MAX function of the formula and place anywhere in the workbook I get the correct range reference i.e. $D$1442:$F$1541
The range names all have workbook scope and refer to other sheets than the one with the data. I can't see what else I can qualify to make the MAX/MIN function work correctly on other sheets, i.e I have the data on sheet Raw Data and I want to display the MAX & MIN on sheet Results.
Any suggestions please?

If your problem is that you can't use this formula to work when data is in other sheet this is because you can't use $D$1442:$F$1541 in your range, you need to point the sheet also, so you want something like that: 'Raw Data'$D$1442:$F$1541.
This said, you need to modify your formula in the ADDRESS section for something like this:
=MIN(INDIRECT((ADDRESS(ROW(INDIRECT(EventID_2_Start)),COLUMN(TopTemp), 1, 1,"Raw Data") & ":" & ADDRESS(ROW(INDIRECT(EventID_2_End)),COLUMN(BtmTemp), 1, 1))))

Related

How to select all values in a column by column number in excel

I am trying to find an elegant way to select all values in a given column on another sheet, based on it's number. I also need to exclude the first row.
I figured that this would be simple but am surprised how much of a headache it has been.
One method has been this:
=INDEX(OtherSheet!$A$2:$H$15, 0, <colNum>)
but this relies on hoping that $H is the last column and $15 is the last row.
Second attempt:
=OFFSET(INDIRECT("OtherSheet!R1C"&<colNum>, FALSE), 1, 0, 15)
But I have to hardcode the number of rows here as well.
This seems like this is such a simple thing to do, am I just missing something?
Thanks!
To return the last row containing data of a given column, using a formula and given that
wSheet is a named range containing the worksheet name
colNum is a named range containing the desired column number
=LOOKUP(2,1/(LEN(INDIRECT(wSheet & "!C" & colNum,FALSE))>0),ROW(INDIRECT(wSheet & "!C" & colNum,FALSE)))
To return a range which refers to that column from Row 2 to the last row, you can combine INDIRECT with the ADDRESS function:
=INDIRECT(ADDRESS(2,colNum,1,1,wSheet)&":"&ADDRESS(LOOKUP(2,1/(LEN(INDIRECT(wSheet & "!C" & colNum,FALSE))>0),ROW(INDIRECT(wSheet & "!C" & colNum,FALSE))),colNum,1,1))
One issue with this method is that it will return 0's for empty or blank cells

How to compare two different sheets and place values in their respective positions?

I would like to compare two different cells within two different spreadsheets (One sheet is a base sheet) via VBA and if they are the same, take the values beside what they're being compared to and the place values in their respective positions.
So in the picture, A is a match in both sheets so the value of 30 is copied over to the base sheet. The spreadsheets that I'm dealing with are not perfectly lined up like the image.
Please let me know if you need more clarification. Thank you!
As I've commented, you can use VLOOKUP in this case.
Assuming both your values are in Column A to B of both sheet and A1 and B1 are headers, you can try below formula in Cell B2:
=VLOOKUP(A2,Sheet2!A:B,2,0)
which will give you the result above. It is also assumed that your sheet names are Sheet1 and Sheet2.
Edit: VBA Code
Using Formula Property
With Sheets("Sheet1").Range("A2")
.Formula = "=VLOOKUP(A2,Sheet2!A:B,2,0)"
.Value = .Value
End With
Using Evaluate Method
With Sheets("Sheet1")
' 3 ways to use Evaluate method
.Range("A2").Value = [VLOOKUP(A2,Sheet2!A:B,2,0)] ' Evaluate shortcut
.Range("A2").Value = .Evaluate("VLOOKUP(A2,Sheet2!A:B,2,0)") ' explicitly
.Range("A2").Value = Evaluate("VLOOKUP(A2,Sheet2!A:B,2,0)")
End With
Using WorkSheetFunction Method - Already provided by Tom
You can use native sheet functions in VBA. Just prepend the worksheet function with 'Application.WorksheetFunction'.
Application.WorksheetFunction.VLookup(Sheets("Sheet2").Range("A2"), Sheets("Sheet2").Columns("A:B"), 2, False)
Hope that helps.

Excel macro for averages

I have a problem with a VBA script. I guess the solution is simple, but I just wasnt able to figure it out....
So basically, I have a workbook that contains many worksheets. Each worksheet contains the exact same format of a table (same number of rows and columns). What I wanted to do was to create a new worksheet and in that worksheet, have averages of all those values.
So for example in cell B2 I want to have average of cells B2 from all the other worksheets. I therefore created a macro that does this, this is not a problem, the problem is however, that in that macro, all sheets are referred to by their names and since I have many of these workbooks with differently named sheets, this would not work. I therefore tried to change the name of the first sheet to actual reference of sheet - i.e. Sheet(1) and the last one as Sheet(x) - where x is the number of sheets I calculated previously. Unfortunately the code doesnt work, could anyone please suggest to me how to modify this so that it works properly? I am only copying the problematic part of the code:
x = Sheets.Count
Sheets.Add After:=Sheets(x)
Range("B2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(Sheets(1):Sheets(x)!RC)"
Try this. You are not offseting your sheet names (in fact, you're not even using the sheet names. In the context of a formula, Sheets(1) is meaningless. You need to use Sheets(1).Name and offset it appropriately:
"=AVERAGE('" & Sheets(1).Name & ":" & Sheets(x).Name & "'!RC)"

Copy raw data into specific cells of a target sheet

I have two worksheets within the same workbook, namely sheet1 ("rawdata") and sheet2 ("Overview).
I copy downloaded data into sheet1 ("rawdata"). Here the number of rows vary but heading/columns are always the same. After this I need to copy specific cells into another worksheet.
Here are the "rules" I was thinking about:
1) Always copy cells from the rawdata sheet E9, W9, X9 and Y9 into a specific cell in the target sheet. I had something like this (which worked):
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E9").Value
2) Always copy the value within column E in the lastrow. However, the last row is varying from rawdata to rawdata while the column (E) stays the same. I tried something like this: (not working)
....= Worksheets("rawdata").Range("E1").End(xlDown).Value
3) The script should be linked to the button, when I click the button again to insert the data from the sheet rawdata, the data should be inserted in the next (following) column of worksheet overview.
Assumes column E always has data. Which in this case should be true.
Sorry tried to simplify and broke it.
LastRow_WithDataInColumnE = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row
Should be
With Worksheets("rawdata")
LastRow_WithDataInColumnE = .Range("E" & .Rows.Count).End(xlUp).Row
End With
Now .Rows.Count should refer to Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row.Value
Should be
With Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = .Range("E" & .Rows.Count).End(xlUp).Row.Value
End With
There is a discussion here Error in finding last used cell in VBA. Suggests a better solution for situations where there is no data in Column E or where rows have been deleted.
You could do something like this to get the last data range in column E:
Public Function FindLastColumnECellAvailable()
FindLastColumnECellAvailable = "E" & WorksheetFunction.CountA(Range("E:E"))
End Function
Then you will have this:
At the end just read the cell value:
Range(FindLastColumnECellAvailable).Value
Greetings
Sorry
An apologize "in advance", I just read the date, hope this will help you yet or anyone else, it's my second day

Excel 2007 - Formula changes to #REF

So I've got this Workbook which contains a lot of data. And I've got this one sheet which basically copies the data based on certain conditions.
Each cell in each row looks like this (the last specified cell is the one where the formula is in):
=IF(Numbers1!E2<>0;Numbers1!A2;"")
=IF(Numbers1!E3<>0;Numbers1!A3;"")
=IF(Numbers1!E4<>0;Numbers1!A4;"")
=IF(Numbers1!E2<>0;Numbers1!B2;"")
=IF(Numbers1!E3<>0;Numbers1!B3;"")
=IF(Numbers1!E4<>0;Numbers1!B4;"")
So the formula in cell A2 is the first one, formula in A3 is the second line etc.
I want to copy the value from the same column and row from the sheet Numbers1, IF the value in the same row of column E is not 0. This seems to be working just fine.
But, when I update the data in Numbers1 sheet, the formulas are all of a sudden invalid and the formula now looks like this:
=IF(Numbers1!#REF!<>0;Numbers1!#REF!;"")
Each formula in each cells look identical to the formula above. And I can't have that, why can't Excel just keep the formula as it is without "helping" me?
Since you may be better off using a macro to rewrite your formulas, here are the basics:
Sub RewriteFormulas()
Dim row, col As Integer
row = 1 'row you want your target formulas to be on
For row = 1 To 60
For col = 1 To 13
ActiveSheet.Cells(row, col).Formula = "=IF(Numbers1!" & Cells(row,col).Address & "<>0,Numbers1!" & Cells(row+2,col).Adddress & ","""")"
Next row
Next col
End Sub
You can play around with using different sheets (or different workbooks) instead of just ActiveSheet so you can have 1 workbook that stores the macro and alters data in whatever workbooks provide your updated datasets.
Hope that helps...

Resources