Hi Everyone, First ever post so apologies if i'm doing this wrong.
I have written some code that in theory is supposed to after being passed a filename selected from a list box(Listbox1) and a directory as a string (MyFolder), use these to see if that workbook is open. If it is then it copies and pastes data from that workbook into the one that houses this code.
If the workbook is not open, then the code should open it and then perform the same process as above (copying and pasting from just opened workbook to the one that is home to this code).
However it fails immediately after opening the workbook at the point where the code reads:
"Set ActiveWorkbook = Workbooks.Open(FileToOpen)"
I'm then greeted with the run time error 438 "Object Doesn't Support This Property or Method".
the code then ends and i'm given no debug option. The workbook I tried to open is opened though.
Here's the code with the point it breaks directly after the 200 marker
I have no idea why this is happening. Scoured loads of threads and can't work it out.
Apologies, I'm not the most elegant coder
I have written some code that in theory is supposed to after being passed a filename selected from a list box(Listbox1) and a directory as a string (MyFolder), use these to see if that workbook is open. If it is then it copies and pastes data from that workbook into the one that houses this code.
If the workbook is not open, then the code should open it and then perform the same process as above (copying and pasting from just opened workbook to the one that is home to this code).
However it fails immediately after opening the workbook at the point where the code reads:
"Set ActiveWorkbook = Workbooks.Open(FileToOpen)"
I'm then greeted with the run time error 438 "Object Doesn't Support This Property or Method".
the code then ends and i'm given no debug option. The workbook I tried to open is opened though.
Here's the code with the point it breaks directly after the 200 marker
I have no idea why this is happening. Scoured loads of threads and can't work it out.
Apologies, I'm not the most elegant coder
Code:
Sub ImportTotals(MyFolder, Listbox1)
Dim wbk As Workbook
Dim NumberOfProducts As Integer
Dim FileToOpen As String
FileToOpen = MyFolder & "\" & Listbox1
Application.ScreenUpdating = False
For Each wbk In Workbooks
If wbk.FullName = FileToOpen Then
Workbooks(wbk.Name).Activate
GoTo 100
End If
Next
GoTo 200
100
ActiveWorkbook.Sheets("Overview").Select
Range("J70").Select
ActiveCell.Formula = "=COUNTIFS(J63:J68,""<>*PRODUCT*"",J63:J68,""<>"")"
NumberOfProducts = ActiveCell.Value
ActiveCell.clear
ThisWorkbook.Activate
Range("F10").Select
ActiveCell.Value = NumberOfProducts
Workbooks(wbk.Name).Activate
ActiveWorkbook.Sheets("Product Totals").Select
Range("A14:O" & (13 + (NumberOfProducts - 1)) + (NumberOfProducts * 33)).Select
Selection.Copy
ThisWorkbook.Activate
Range("B12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B12").Select
Application.CutCopyMode = False
Range("B" & (NumberOfProducts * 33) + (11 + (NumberOfProducts - 1))).Select
Dim LastRow As Integer
LastRow = ActiveCell.Row
Range("P12").Select
For x = 1 To LastRow
If ActiveCell.Value <> "0" Then
ActiveCell.Offset(1, 0).Select
Else: Rows(ActiveCell.Row).EntireRow.Delete
End If
Next
Exit Sub
200
Set ActiveWorkbook = Workbooks.Open(FileToOpen)
Sheets("overview").Select
Range("J70").Select
ActiveCell.Formula = "=COUNTIFS(J63:J68,""<>*PRODUCT*"",J63:J68,""<>"")"
NumberOfProducts = ActiveCell.Value
ActiveCell.clear
ThisWorkbook.Activate
Range("F10").Select
ActiveCell.Value = NumberOfProducts
Workbooks(wbk.Name).Activate
ActiveWorkbook.Sheets("Product Totals").Select
Range("A14:O" & (13 + (NumberOfProducts - 1)) + (NumberOfProducts * 33)).Select
Selection.Copy
ThisWorkbook.Activate
Range("B12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B12").Select
Application.CutCopyMode = False
Range("B" & (NumberOfProducts * 33) + (11 + (NumberOfProducts - 1))).Select
LastRow = ActiveCell.Row
Range("P12").Select
For x = 1 To LastRow
If ActiveCell.Value <> "0" Then
ActiveCell.Offset(1, 0).Select
Else: Rows(ActiveCell.Row).EntireRow.Delete
End If
Next
End Sub