Home |
Search |
Today's Posts |
#18
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Yes.
"Faboboren" wrote: Hi JMB, Thanks so much for your extensive answer. It is really great and working perfect. I was out of town last week, I could not check the answer properly in my blackberry. When you referred in the third option to: Const strPath As String = "I:\Excel\Test" '<<<CHANGE Is this the path where 11 files are, without the names of files? Thanks once again. "JMB" wrote: When using a For Each loop, you must reference the variable placeholder within the loop. If not fully qualified (eg. Workbook.Sheets("SomeSheet").Range("A1")), object references are assumed to refer to the active workbook, worksheet, range, etc. For example Range("a1") refers the the active worksheet of the active workbook Sheets("sheet1").Range("A1") refers to the active workbook So assuming that all 11 workbooks are opened (12 including planos.xls), I believe you only need the minor changes: For Each wb In Workbooks If wb.Name < "Planos.xls" Then For i = 13 To 15 With wb.Sheets("WSP_Sheet" & i) .... End With Next i End If Next wb However, if you have hidden workbooks opened (such as personal.xls) they will be included. If you intend to have the code run on all open workbooks, be sure to close hidden books you don't want the code to run on. Or, check the visible property: For Each wb In Workbooks If Windows(wb.Name).Visible And wb.Name < "Planos.xls" Then For i = 13 To 15 With wb.Sheets("WSP_Sheet" & i) ..... End With Next i End If Next wb If all of the target workbooks are in one folder, you could have the macro open them up, put the formula in, save, close, etc. The Planos.xls file will need to be open (if not, the Vlookup formula will need edited to include the file path - I'm pretty sure vlookup can pull info from a closed file). Option Explicit Sub test() Const strPath As String = "I:\Excel\Test" '<<<CHANGE Dim wbTemp As Workbook Dim wsTemp As Worksheet Dim rngResults As Range Dim rngLookupValue As Range Dim lngWS As Long Dim lngWB As Long With Application.FileSearch .NewSearch .LookIn = strPath .SearchSubFolders = False .Filename = "*.xls" .MatchTextExactly = True .FileType = msoFileTypeAllFiles .Execute If .FoundFiles.Count 0 Then For lngWB = 1 To .FoundFiles.Count Set wbTemp = Workbooks.Open(.FoundFiles(lngWB)) For lngWS = 13 To 15 Set wsTemp = Nothing On Error Resume Next Set wsTemp = wbTemp.Sheets("WSP_Sheet" & lngWS) On Error GoTo 0 If Not wsTemp Is Nothing Then With wsTemp If IsEmpty(.Cells(.Rows.Count, 1)) Then Set rngResults = .Range(.Cells(7, 13), _ .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 12)) Else: Set rngResults = .Range(.Cells(7, 13), _ .Cells(.Rows.Count, 13)) End If Set rngLookupValue = .Cells(7, 1) End With With rngResults .Formula = "=IF(ISNA(VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE)),2,VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE))" .Value = .Value End With End If Next lngWS wbTemp.Close savechanges:=True Next lngWB End If End With End Sub "Faboboren" wrote: Hi JMB, As I said I am trying to do the same in 11 workbooks, I added to your code "For Each wb In Workbooks", and not working, any idea why? Thanks Sub Vlookupsheets() Dim rngResults As Range Dim rngLookupValue As Range Dim i As Long Dim wb As Workbook For Each wb In Workbooks For i = 13 To 15 With Sheets("WSP_Sheet" & i) If IsEmpty(.Cells(.Rows.Count, 1)) Then Set rngResults = .Range(.Cells(7, 13), _ .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 12)) Else: Set rngResults = .Range(.Cells(7, 13), _ .Cells(.Rows.Count, 13)) End If Set rngLookupValue = .Cells(7, 1) End With With rngResults .Formula = "=IF(ISNA(VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE)),2,VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE))" .Value = .Value End With Next i Next wb End Sub "JMB" wrote: My apologies for delayed response. Try: Sub test() Dim rngResults As Range Dim rngLookupValue As Range Dim i As Long For i = 13 To 15 With Sheets("sheet" & i) If IsEmpty(.Cells(.Rows.Count, 1)) Then Set rngResults = .Range(.Cells(7, 13), _ .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 12)) Else: Set rngResults = .Range(.Cells(7, 13), _ .Cells(.Rows.Count, 13)) End If Set rngLookupValue = .Cells(7, 1) End With With rngResults .Formula = "=IF(ISNA(VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE)),2,VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE))" .Value = .Value End With Next i End Sub "Faboboren" wrote: JMB, I am trying to run the code we were talking about from Sheet13 to 15. Please any idea how. Thanks Dim rrLoli As range Dim sh As Worksheet Dim wb As Workbook Sub Vlookupsheets() For Each wb In Workbooks For i = Sheets("Sheet13") To ("Sheet15") Set rrLoli = range("M:M") Call Vlookupsheets1 Next i Next wb End Sub Sub Vlookupsheets1() Dim r As range For Each r In rrLoli If IsEmpty(Cells(Rows.Count, 1)) Then Set rngResults = range(Cells(7, 13), _ Cells(Rows.Count, 1).End(xlUp).Offset(0, 12)) Else: Set rngResults = range(Cells(7, 13), _ Cells(Rows.Count, 14)) End If Set rngLookupValue = Cells(7, 1) Next With rngResults .Formula = "=IF(ISNA(VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE)),2,VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",'[Planos.xls]A&P plano'!A:N,14,FALSE))" .Value = .Value End With End Sub "JMB" wrote: you're welcome. thanks for letting us know that you got it working. "Faboboren" wrote: Hi JMB, This is really working so well, 100 thanks!!! "JMB" wrote: Not sure my response was posted, so I'll try it again... Here is an approach using variables. Sub test() Dim rngResults As Range Dim rngLookupValue As Range Set rngResults = Sheets("sheet2").Range("M2:M27") Set rngLookupValue = Sheets("sheet2").Range("C2") With rngResults .Formula = "=IF(ISNA(VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",Sheet1!A:C,2,FALSE)),2,VLOOKUP(" & _ rngLookupValue.Address(False, False) & _ ",Sheet1!A:C,2,FALSE))" .Value = .Value End With End Sub If you want the range in column M to be from M2 to whatever the last row is in column C replace Set rngResults = Sheets("sheet2").Range("M2:M27") Set rngLookupValue = rngResults.Cells(1).Offset(0, -10) with this code With Sheets("sheet2") If IsEmpty(.Cells(.Rows.Count, 3)) Then Set rngResults = .Range(.Cells(2, 13), _ .Cells(.Rows.Count, 3).End(xlUp).Offset(0, 10)) Else: Set rngResults = .Range(.Cells(2, 13), _ .Cells(.Rows.Count, 13)) End If Set rngLookupValue = .Cells(2, 3) End With "Faboboren" wrote: |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Macro - Vlookup | Excel Discussion (Misc queries) | |||
vlookup macro | New Users to Excel | |||
VBA Macro for VLOOKUP | Excel Programming | |||
Please help.. VLookup Macro | Excel Programming | |||
VLOOKUP in a Macro | Excel Programming |