I want to use Vlookup in my vba that searches in another file that is chosen. The vlookup I copied from recorded macro. I changed the name of Workbook and Worksheets because it is different every time. The range is the same. I got the error 438 “object doesn't support this property or method”.
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select BPM Report for previous month"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
oldbpm = .SelectedItems.Item(1) End With
Workbooks.Open oldbpm
Dim Sheetstarybpm As Worksheet
Set Sheetstarybpm = ActiveSheet
Dim countrowsoldbpm1 As Long
countrowsoldbpm1 = Range("a10", Range("a10").End(xlDown)).Rows.Count
Workbooks("BPM-Tool.xlsm").Activate Worksheets("OU").Activate
Dim countrowsoldbpm As Long
countrowsoldbpm = Range("a1", Range("a1").End(xlDown)).Rows.Count
Range("D1:D1823").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(VLOOKUP(B2,'[" & oldbpm & "]" & Sheetstarybpm & "'!R10C2:R7000C101,100,FALSE),0)"
Range("D1").Select
Selection.AutoFill Destination:=Range("D1:D" & countrowsoldbpm)
Related
This code allows user to select the files they want to merge together, I'm getting an error on the marked line when I try to copy the sheets from the files to the destination workbook (xlBook).
I might have the wrong approach; I've been using a lot of examples from google, with no luck.
Sub complie_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim xlBook, srcBook As Workbook
Dim fileSlct, pdfDialog As fileDialog
Dim xlSheet, srcSheet As Worksheet
Dim xlRow, srcRow, xlColm, srcColm As Long
Dim fileIdx As Integer
Dim hdrRang As Range
Set xlBook = ThisWorkbook.Sheets
On Error GoTo error
Set fileSlct = Application.fileDialog(msoFileDialogFilePicker) 'Allows user to select the files/reports
With fileSlct
.AllowMultiSelect = True 'Allows for multi seletion
.Title = "Select target files:"
.ButtonName = "Open"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xlsb; *.xls; *.xlw"
.Show
End With
If fileSlct.SelectedItems.Count = 0 Then
MsgBox "No file found that match.", vbExclamation
Exit Sub
End If
For fileIdx = 1 To fileSlct.SelectedItems.Count 'Loops through each of the selected items, and copies them to workbook
Set srcBook = Workbooks.Open(fileSlct.SelectedItems(fileIdx))
Set xlSheet = srcBook.ActiveSheet
srcRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
srcColm = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(1, 1), Cells(srcRow, srcColm)).Copy xlBook.Sheets("Sheet2").Cells(1, 1) 'Error here
Next fileIdx
Application.ScreenUpdating = True
error:
MsgBox Err.Number & " " & Err.Description, vbCritical
End Sub
Thanks to #GSerg for pointing out my mistake. It was simply having the wrong property.
Sub complie_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim xlBook, srcBook As Workbook
Dim fileSlct, pdfDialog As fileDialog
Dim xlSheet, srcSheet As Worksheet
Dim xlRow, srcRow, xlColm, srcColm As Long
Dim fileIdx As Integer
Dim hdrRang As Range
Set xlBook = ThisWorkbook 'took out .Sheets
On Error GoTo error
Set fileSlct = Application.fileDialog(msoFileDialogFilePicker) 'Allows user to select the files/reports
With fileSlct
.AllowMultiSelect = True 'Allows for multi seletion
.Title = "Select target files:"
.ButtonName = "Open"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xlsb; *.xls; *.xlw"
.Show
End With
If fileSlct.SelectedItems.Count = 0 Then
MsgBox "No file found that match.", vbExclamation
Exit Sub
End If
For fileIdx = 1 To fileSlct.SelectedItems.Count 'Loops through each of the selected items, and copies them to workbook
Set srcBook = Workbooks.Open(fileSlct.SelectedItems(fileIdx))
Set xlSheet = srcBook.ActiveSheet
srcRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
srcColm = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(1, 1), Cells(srcRow, srcColm)).Copy xlBook.Sheets("Sheet2").Cells(1, 1)
Next fileIdx
Application.ScreenUpdating = True
error:
MsgBox Err.Number & " " & Err.Description, vbCritical
End Sub
Thanks again GSerg! Now it's just formatting the output.
I need help, cant figure out how to import only values with this code
Sub ImportDatafromotherworksheet()
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As range
Dim rngDestination As range
Set wkbCrntWorkBook = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa; *.xls"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Set rngSourceRange = Application.ActiveWorkbook.ActiveSheet.range("A2:C200")
wkbCrntWorkBook.Activate
Set rngDestination = Application.ActiveWorkbook.Sheets("DS").range("G17:G17")
rngSourceRange.Copy rngDestination
rngDestination.CurrentRegion.EntireColumn.AutoFit
wkbSourceBook.Close False
Application.ScreenUpdating = False
End If
End With
End Sub
Tnx.
This should work:
Sub ImportDatafromotherworksheet()
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Set wkbCrntWorkBook = ActiveWorkbook 'or ThisWorkbook for file containing this code.
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa; *.xls"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
'Just reference the files - no need to activate them first.
Set wkbSourceBook = Workbooks.Open(.SelectedItems(1))
wkbSourceBook.Worksheets("Sheet1").Range("A2:C200").Copy 'Remember to change the sheet name.
wkbCrntWorkBook.Worksheets("DS").Range("G17").PasteSpecial xlPasteValues
'For a normal Copy/Paste you can use:
'wkbSourceBook.Worksheets("Sheet1").Range("A2:C200").Copy _
' Destination:=wkbCrntWorkBook.Worksheets("DS").Range("G17")
wkbSourceBook.Close False
End If
End With
End Sub
I'm trying to take data from one workbook and paste it into another.
The workbooks change every month. I'd like to select the source file using Application.fileDialog.
Sub CopyTest ()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
fullpath = .SelectedItems.Item(1)
End With
Dim sourceBook As Workbook
Set sourceBook = Application.Workbooks.Open(sourceBookPath)
Dim sourceSheet As Worksheet
Set sourceSheet = sourceBook.Worksheets("Account Detail GHOA ")
Dim targetBook As Workbook
Set targetBook = Application.Workbooks.Open(targetBookPath)
Dim targetSheet As Worksheet
Set targetSheet = targetBook.Worksheets(“Macro Data”)
sourceSheet.Range("A1:W79").Copy targetSheet.Range("A1:W79")
End Sub
I referenced this question to find the above partial solution: Excel VBA file name changes
I found the solution thank you all for the help!
Replace the sheet names with the ones for your work book. This code will enable you to select the source file (where data is being pulled from) and the target file (where you'd like it to paste to). This worked great for what I needed.
I plan on taking the raw data that I imported in (which changes on a monthly basis) and using vlookups to populate the summary tab.
Sub CopyTest()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in sourceBook variable
sourceBookPath = .SelectedItems.Item(1)
End With
Dim sourceBook As Workbook
Set sourceBook = Application.Workbooks.Open(sourceBookPath)
Dim sourceSheet As Worksheet
Set sourceSheet = sourceBook.Worksheets("Account Detail GHOA")
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in targetBook variable
targetBookPath = .SelectedItems.Item(1)
End With
Dim targetBook As Workbook
Set targetBook = Application.Workbooks.Open(targetBookPath)
With Worksheets("Macro Data").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Macro Data"
Application.DisplayAlerts = True
End With
Dim targetSheet As Worksheet
Set targetSheet = targetBook.Worksheets("Macro Data")
sourceSheet.Range("A4:W79").Copy targetSheet.Range("A1:W79")
End Sub
I get all the time runtime error 9, “subscript out of range” . Dont know how to do this. I get the error by the last line of code below.
Dim Sheetstarybpm As Worksheet
Dim countrowsoldbpm1 As Long
Dim rng2 As Range
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select BPM Report for previous month"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
oldbpm = .SelectedItems.Item(1)
End With
Workbooks.Open oldbpm
Set Sheetstarybpm = ActiveSheet
countrowsoldbpm1 = Range("a10", Range("a10").End(xlDown)).Rows.Count
Set myrange = Range("A10:CW" & countrowsoldbpm1)
Set rng2 = Workbooks(oldbpm).Worksheets(Sheetstarybpm).Range(myrange)
You are using the object methods incorrectly. Once you have stored the Sheetstarybpm you can use this further... So when opening the workbook store this as a workbook object so you can reference it later on, if needed. Additionally, when setting your worksheet object you can now explicitly reference your workbook because you have stored it (much better coding practice). It is also recommended to explicitly define your sheet object references when setting your ranges. Please see amended code below.
Dim Sheetstarybpm As Worksheet
Dim countrowsoldbpm1 As Long
Dim myWB As Workbook
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select BPM Report for previous month"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
oldbpm = .SelectedItems.Item(1)
End With
Set myWB = Workbooks.Open(oldbpm)
Set Sheetstarybpm = myWB.ActiveSheet
countrowsoldbpm1 = Sheetstarybpm.Range("a10", Range("a10").End(xlDown)).Rows.Count
Set myrange = Sheetstarybpm.Range("A10:CW" & countrowsoldbpm1)
Edit: Removed rng2 from the code because it is the same as myrange, as noted by OP in comments.
By the line
Set srchrange = Workbooks(wipreport).Worksheets("1. WIP report").Range("B15:B")
I got the error Subscript out of range. wipreport is chosen above from the file. The workbook for sure has the Sheet "1. WIP report". I copied it. Workbooks BPM-Tool is already opened but I have no error by this line of code.
I can not even try if vlookup works.
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select WIP Report"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
wipreport = .SelectedItems.Item(1)
End With
Workbooks.Open wipreport
Dim lookFor As Range
Dim srchrange As Range
Set lookFor = Workbooks("BPM-Tool.xlsm").Worksheets("BPM-Report").Cells(10, 2)
Set srchrange = Workbooks(wipreport).Worksheets("1. WIP report").Range("B15:B")
lookFor.Offset(0, 317).Value = Application.VLookup(lookFor, srchrange, 18, False)
Workbooks.Open takes the full file name e.g. C:\temp\myfile.xlsx
Workbooks() takes file name only e.g. myfile.xlsx
You can use this code to get the workbook object and then use it:
' Get the workbook object
Dim wk As Workbook
Set wk = Workbooks.Open(wipreport)
' Use the workbook object
wk.Worksheets("1. WIP report").Range ("B15:B")
Try this:
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select WIP Report"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
wipreport = .SelectedItems.Item(1)
End With
Dim wb As Workbook
Set wb = Workbooks.Open(wipreport)
Dim lookFor As Range
Dim srchrange As Range
Set lookFor = Workbooks("BPM-Tool.xlsm").Worksheets("BPM-Report").Cells(10, 2)
Set srchrange = wb.Worksheets("1. WIP report").Range("B15:B1000")
lookFor.Offset(0, 317).Value = Application.VLookup(lookFor, srchrange, 18, False)
wipreport is a String not a Workbook object, that's why you are getting an error.