I have this recorded macro that I want to convert to plain VBA macro code and copy the formula from AE2 to lastrow.
"=VLOOKUP(RC[-22],'[test.xlsx]Sheet3'!C5:C6,2,0)" ' this is from another workbook
Trying to convert to VBA.
dim wbSLW as workbook
dim wbSLWDir as String
wbSLWDir = "C\Documents\test.xlsx" 'this is not the constant directory or file name
set wbSLW = workbooks.open(wbSLWDir)
ThisWorkbook.Activate
With Thisworkbook.Sheets(1)
.Range("AE2") = "=VLOOKUP(I2," & wbSLW & "!E:F,2,0)" ' error line
end with
When I've converted it, it returns Object does not support this property or method.
Change your settings so that it does not use R1C1 format by going to (Excel 2010) Settings / Formulas and then unchecking the R1C1 reference style. Rerecord your macro and then edit it. You should not have any "RC" references, but if you do then change all references that have "RC" in them to the the same style as in "I2".
Cleaned recorded code :
With ActiveSheet.Range("AE2")
.FormulaR1C1 = "=VLOOKUP(RC[-22],Temp!C[-30]:C[-29],2,0)"
.AutoFill Destination:=Range("AE2:AE182"), Type:=xlFillDefault
End With 'ActiveSheet
RC[-22] refers to a cell that is 22 columns before the cell in which you have the formula here AE2 is the initial cell, so RC[-22]=I2
C[-30] and C[-29] refers respectively to columns that are 30 and 29 columns before the cell in which you have the formula
here AE2, so C[-30]=A and C[-29]=B
Changed formula :
Sheets(1).Range(perNum & 2).Formula = "=VLOOKUP(I2,Temp!A:B,2,0)"
or without converting the formula :
Sheets(1).Range(perNum & 2).FormulaR1C1 = "=VLOOKUP(RC[-22],Temp!C[-30]:C[-29],2,0)"
Assuming that Active Sheet name is Sheet1
ActiveCell.Value = Application.WorksheetFunction.VLookup(Sheets("Sheet1").Range("I2"), Sheets("Temp").Range("A:B"), 2, 0)
If i understood correctly, you want to replace name of the workbook in below line in vba code :
=VLOOKUP(RC[-22],'[**test.xlsx**]Sheet3'!C5:C6,2,0)
Simplest way to do that without changing any other code is :
=VLOOKUP(RC[-22],'[**" & wbSLW.Name & "**]Sheet3'!C5:C6,2,0)
I have this excel formula which works perfectly to lookup a value in A1 from multiple sheets provided those sheets are on the same spreadsheet. I would like to extend it so it searches from an external spreadsheet where sheet_list is a list of sheet names on the external spreadsheet. Please help.
=LOOKUP(9.99999999999999E+307,1/COUNTIF(INDIRECT(" ' " &sheets_list&" '!A:A"),$A1),sheets_list)
I do exactly this :
IFERROR(INDIRECT("'" & $A$1 & $B$1 & $C$1 & "'!"&B$3 & ($A4+6) ),"file closed")
the cells a1, b1, c1 contain the path and file name detail, so all you need is to add your path / filename before the first ampersand in your expression.
Got it to work in this way:
=LOOKUP(9.99999999999999E+307,1/COUNTIF(INDIRECT("'[external_workbook_filename]" &sheet_names_array&"'!A:A"),$A1),sheet_names_array)
I want to check Column A (A1:A365) in Sheet2, Sheet3, Sheet4 and Sheet5 and find Maximum value for each cell. Compare A1 in Sheet2, Sheet3, Sheet4 and Sheet5, find maximum of it and report it in A1 in result page. Also in cell B1 report corresponding sheet for this maximum. This goes on to Column A (A1:A365)
the following code i used:
Worksheets("sheet2").Range("A1").Value = a
Worksheets("sheet3").Range("A1").Value = b
Worksheets("sheet4").Range("A1").Value = c
Worksheets("sheet5").Range("A1").Value = d
MaxValue = Application.Max(a, b, c, d)
Range("A1").Value = MaxValue
yes i have just 4 sheets – Mohsen 11 mins ago
Non VBA Solution
In Sheet1, Cell A1, put this formula
=MAX(Sheet2!A1,Sheet3!A1,Sheet4!A1,Sheet5!A1)
In Sheet1, Cell B1, put this horrifying formula. I am sure there is a better way to find the sheet name though.
=IF(Sheet1!A1=Sheet2!A1,RIGHT(CELL("filename",Sheet2!A1),LEN(CELL("filename",Sheet2!A1))- FIND("]",CELL("filename",Sheet2!A1),1)),IF(Sheet1!A1=Sheet3!A1,RIGHT(CELL("filename",Sheet3!A1),LEN(CELL("filename",Sheet3!A1))- FIND("]",CELL("filename",Sheet3!A1),1)),IF(Sheet1!A1=Sheet4!A1,RIGHT(CELL("filename",Sheet4!A1),LEN(CELL("filename",Sheet4!A1))- FIND("]",CELL("filename",Sheet4!A1),1)),IF(Sheet1!A1=Sheet5!A1,RIGHT(CELL("filename",Sheet5!A1),LEN(CELL("filename",Sheet5!A1))- FIND("]",CELL("filename",Sheet5!A1),1)),""))))
A word of caution though. To use the RIGHT(CELL("filename",Sheet2!A1),LEN(CELL("filename",Sheet2!A1))- FIND("]",CELL("filename",Sheet2!A1),1)), you need to have the workbook saved.
My Sheet2, A1 has 1, Sheet3, A1 has 2, Sheet4, A1 has 2.5,Sheet5, A1 has 3
VBA Solution
Sub Sample()
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Range("A1:A365").Formula = "=MAX(Sheet2!A1,Sheet3!A1,Sheet4!A1,Sheet5!A1)"
.Range("A1:A365").Value = .Range("A1:A365").Value
For i = 1 To 365
Select Case .Range("A" & i)
Case ThisWorkbook.Sheets("Sheet2").Range("A" & i).Value: .Range("B" & i).Value = "Sheet2"
Case ThisWorkbook.Sheets("Sheet3").Range("A" & i).Value: .Range("B" & i).Value = "Sheet3"
Case ThisWorkbook.Sheets("Sheet4").Range("A" & i).Value: .Range("B" & i).Value = "Sheet4"
Case ThisWorkbook.Sheets("Sheet5").Range("A" & i).Value: .Range("B" & i).Value = "Sheet5"
End Select
Next i
End With
End Sub
Quick solution
Based on Sidd's answer, here's my non-VBA solution without the horrifying formula:
Place this formula in Sheet1!A1: =MAX(Sheet2:Sheet5!A1)
As the sheet name does not have to be flexible (I'd assume you don't change it that often), you can use this formula in B1:
=IF(Sheet2!A1=A1,"Sheet2",
IF(Sheet3!A1=A1,"Sheet3",
IF(Sheet4!A1=A1,"Sheet5",
"Sheet5")))
More structural solution (better suited for many worksheets):
If you have many worksheets, you could consider this alternative.
Have a list of the relevant worksheets stored somewhere in your worksheet. (in the example, I place the list in E3:E7). Name this range Sheets. (Similar to Sidd's horrifying formula, I used the CELL formula to dynamically get each sheet name. However, this is not necessary in a static model)
(same as step 1 above): Place this formula in Sheet1!A1: =MAX(Sheet2:Sheet5!A1)
Place this formula in A2:
=INDEX(Sheets,MATCH(1,COUNTIF(INDIRECT("'"&Sheets&"'!A1"),A1),0))
Enter it as an array formula, i.e. press Ctrl-Shift-Enter instead of Enter.
I uploaded the second solution here.
Kudos to this instruction!
Since the formula is intended to be copied down, you don't want to hard-code a reference to cell A1 in the INDIRECT in Peter Albert's formula. You can use CELL("address",A1) to get a reference to A1 that can be copied down instead.
You can also avoid the need to array-enter the formula by using LOOKUP to return the result instead of INDEX & MATCH.
Note that LOOKUP will return the name of the last worksheet with the max value in case of a tie.
The following formulas use a named range Sheets with the names of each worksheet
=MAX(Sheet2:Sheet5!A1) returns max value (identical to Peter Albert's formula)
=LOOKUP(2,1/COUNTIF(INDIRECT("'" & Sheets & "'!" & CELL("address",A1)),A1),Sheets) returns name of sheet with max value
Make two new sheets with one titled "First" and the other "Last". Make new sheets for your project originate through a button that contains a macro to add duplicate sheets between "First and Last". Then just put one of these simple formulas for example in your reporting cell: =SUM(First:Last!K28) or =MAX(First:Last!K28)
I want to reference another workbook and a specific worksheet which is dependent on some cell values.
This equation works
='S:\Down Time[11-Nov_2013_Downtime Tracker.xls]30'!$F$12
but the values 'Nov' and '30' are dynamic-- they depend on the cell values of D13 and E13, respectively.
How can I modify the equation?
Thank you
If you want to do this only by excel formulas, you could modify you formula like this:
=INDIRECT("'S:\Down Time[11-" & A1 & "_2013_Downtime Tracker.xls]" & A2 & "'!$F$12)
In this example it is assumed that the nov-value is in cell A1 and the 30-value is in cell A2. This formula, however, will only work if the referenced workbooks are open. Otherwise the formula would return a #REF! error. Only alternative would be to use VBA.
A little bit late, but here it goes in case someone else is looking for it.
Change the ranges to accommodate your needs.
With VBA:
Sub CallOtherWB()
Dim ThisMonth As String
Dim TheOtherVar As String
ThisMonth = Range("D13").Value
TheOtherVar = Range("E13").Value
With Range("A1")
.Formula = "'S:\Down Time[11-" & ThisMonth & "_2013_Downtime Tracker.xls]" & TheOtherVar & "'!$F$12"
End With
End Sub
In excel 2007, I have a formula in a cell like the following:
=COUNTIFS('2008-10-31'!$C:$C;">="&'$A7)
Now I want to make the name of the sheet ('2008-10-31') be dependent on the value of some cell (say A1). Something like:
=COUNTIFS(A1!$C:$C;">="&'$A7) // error
Is there is way to do this? Or do I have to write a VBA-Macro for it?
INDIRECT does what you want. Note that if the sheet name has any spaces, you need to put single quotes round it, ie
=COUNTIFS(INDIRECT("'" & A1 & "'!$C:$C"); ">=" & $A7)
You are looking for the INDIRECT worksheet function:
=INDIRECT("SHEET2!A1")
=COUNTIFS(INDIRECT(A1 & "!$C:$C"); ">=" & $A7)
The function turns a string into a real cell reference.