Subscript out of range while setting the range - excel

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.

Related

Importing data from another workbook VBA

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

Copying data from one sheet to another with changing file name

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

How to reffer to workbooks-worksheet and range as variables

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.

Vlookup with variables

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)

Using File Opened via File Dialog with VBA

I am trying to Copy information from a tab in file opened via File Dialog and paste it into "ThisWorkbook"
Below is my attempt. I keep getting the error
"object doesn't support this property or method"
on the line in bold font.
Sub UpdateWeeklyJobPrep()
Dim xlFileName As String
Dim fd As Office.FileDialog
Dim source As Workbook
Dim currentwk As Integer
Dim wksheet As String
Dim target As ThisWorkbook
Dim fso As Object
Dim sourcename As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Calc the current fiscal week
currentwk = WorksheetFunction.WeekNum(Now, vbMonday)
wksheet = "FW" & currentwk
With fd
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show Then
xlFileName = .SelectedItems(1)
Else
Exit Sub
End If
End With
'Opens workbook
Workbooks.Open (xlFileName), ReadOnly:=True
'Get file name from path
Set fso = CreateObject("Scripting.FileSystemObject")
sourcename = fso.GetFileName(xlFileName)
sourcename = Left(sourcename, InStrRev(sourcename, ".") - 1)
'Copy/Paste Code Here
**Workbooks(sourcename).Activate**
Workbooks(sourcename).Worksheets(wksheet).Column("F").Copy
target.Activate
target.Sheets("Data Source").Column("C").PasteSpecial
'close workbook with saving changes
source.Close SaveChanges:=False
Set source = Nothing
End Sub
I think I have a solution. Primarily, as mentioned above in my comment, you should use a variable to hold your new, open workbook.
Sub UpdateWeeklyJobPrep()
Dim xlFileName As String
Dim fd As Office.FileDialog
Dim source As Workbook
Dim currentwk As Integer
Dim wksheet As String
Dim fso As Object
Dim sourcename As String
Dim mainWB As Workbook
Set mainWB = ThisWorkbook
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Calc the current fiscal week
currentwk = WorksheetFunction.WeekNum(Now, vbMonday)
wksheet = "FW" & currentwk
With fd
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show Then
xlFileName = .SelectedItems(1)
Else
Exit Sub
End If
End With
'Opens workbook
Dim newWB As Workbook
Set newWB = Workbooks.Open(xlFileName, ReadOnly:=True)
'Copy/Paste Code Here
mainWB.Sheets("Data Source").Column("C").Values = newWB.Worksheets(wksheet).Column("F").Values
newWB.Close savechanges:=False
Set newWB = Nothing
End Sub
I also changed the Copy/PasteSpecial bit, assuming you just needed values. Note since you're copying a whole column this might take time. You'd probably instead want to minimize that range to the used rows only, but I'll leave that as an exercise for the reader.

Resources