runtime error 438 after opening workbook with vba

AndrewI

New Member
Joined
Nov 1, 2017
Messages
40
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

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
 
"thisworkbook" was the correct workbook and given the paste values line, i figured it was already focused in the right place
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
So what code do you have now?

Did you change the part that I didn't change so that it referred to the correct workbook/worksheet?

so the old saying about assuming istrue

added a "thisworkbook.activate" above the code that wasn't running and now it works perfectly without repeating code.

you're a star.. thanks :beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,012
Messages
6,175,937
Members
452,687
Latest member
A_QA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top