Workbook.close fail

R2ah1ze1l

Board Regular
Joined
Nov 10, 2017
Messages
98
Code:
Dim wb_FWD As Workbook, wb_AFT As Workbook, ws_FWD As Worksheet, ws_AFT As Worksheet
Dim MEAS_wb As Workbook, MEAS_ws As Worksheet, SN_ws As Worksheet, SNL_ws As Worksheet
Set MEAS_wb = ActiveWorkbook
Set MEAS_ws = MEAS_wb.Worksheets("Measurements")
Set SN_ws = MEAS_wb.Worksheets("Sheet1")
Set SNL_ws = MEAS_wb.Worksheets("SN_List")
flpath = SN_ws.Range("B1").Value
fl_pth1 = flpath & "\something_PROD_OPA_J-"
fl_pth2 = flpath & "\something_PROD_OPB_J-"
SN_ws.Activate 'issue numbers
a_iss = Format(SN_ws.Range("B2").Value, "Standard") 
b_iss = Format(SN_ws.Range("B3").Value, "Standard") 
a_path = fl_pth1 & a_iss & ".xlsx"
b_path = fl_pth2 & b_iss & ".xlsx"
Set wb_FWD = Workbooks.Open(a_path)
Set ws_FWD = wb_FWD.Worksheets("Sheet1")
ws_FWD.Range("A1").Activate
    i = 0
While Not ActiveCell = ""
    i = i + 1
    ActiveCell.Offset(1, 0).Activate
Wend
SNL_ws.Activate
SNL_ws.Range("A1").Activate
While Not ActiveCell = ""
    a_sn_count = a_sn_count + 1
    ActiveCell.Offset(1, 0).Activate
Wend
SNL_ws.Range("B1").Activate
While Not ActiveCell = ""
    b_sn_count = b_sn_count + 1
    ActiveCell.Offset(1, 0).Activate
Wend
v = 0
s = 0
For v = 2 To i
ws_FWD.Activate
ws_FWD.Range("I" & v).Activate
            sn_a = ws_FWD.Range("I" & v).Value
SNL_ws.Activate
    For j = 1 To a_sn_count
    If sn_a = SNL_ws.Range("A" & j).Value Then GoTo skp_val
    Next j
    ws_FWD.Activate
    feat_id = ws_FWD.Range("A" & v).Value
    If feat_id = "Feature" Or feat_id = "130_REFERENCE" Then GoTo skp_val
    rept_val = ws_FWD.Range("C" & v).Value
    rec_sn = ws_FWD.Range("I" & v).Value
    oper_id = ws_FWD.Range("J" & v).Value
        MEAS_ws.Activate
    MEAS_ws.Range("A" & v + s).Value = pn_nam
    MEAS_ws.Range("B" & v + s).Value = feat_id
    MEAS_ws.Range("C" & v + s).Value = mach_nam
    MEAS_ws.Range("D" & v + s).Value = rec_sn
    MEAS_ws.Range("E" & v + s).Value = L_siz
    MEAS_ws.Range("F" & v + s).Value = rept_val
    MEAS_ws.Range("H" & v + s).Value = rec_sn
    MEAS_ws.Range("J" & v + s).Value = oper_id
    MEAS_ws.Range("N" & v + s).Value = tool_num
    If feat_id = "270.75" Then GoTo comp_v
    GoTo v_n
skp_val:
    s = s - 1
    GoTo v_n
comp_v:
    a_sn_count = a_sn_count + 1
    SNL_ws.Activate
    SNL_ws.Range("A" & a_sn_count + list_add).Value = rec_sn
    vs_tot = v + s - 1
v_n:
Next v
'need to clean out file for FWD here
wb_FWD.Close

When I am attempting to close the recently opened workbook an error is appearing:
Run-time error '-2147221080(800401a8)':
Method 'Close' of object '_Workbook' failed

I have tried multiple options and seem to be running into this similar error. This is something that I had been using in other VBA code that had functioned. Does anyone know an alternate method to close the specific workbook?

Thank you
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Does anyone know an alternate method to close the specific workbook?
I'm not the best person to help you. However, I noticed you are storing the filepath into a variable pulled from a cell value in a workbook.
Others may need to see the full path you are referring to, as some paths might be problematic; especially when the path is a URL.
Provide the Full Path to the Workbook, and that would be a good first start.

One example of a problematic path regarding URLs:

Best regards,
 
Upvote 0
The main file that contains the file path information is to remain open, it is only the secondary workbook I'd like to close before opening another file and pulling more information. This line of code for wb_FWD.Close did work for past projects, not sure what occurred in the latest update.
 
Upvote 0
What happens if you add this line in before the "Close" line?
VBA Code:
wb_FWD.Activate

If that does not fix it, try adding a break-point on your "close" line, and run your code again.
It should then stop at that line, just before you get the error.
When it gets to that point, take a look at what you are showing in Excel.
Is that workbook still open/active?
Is there anything else funny going on?

Note: I really do not like the way you are using multiple GoTos in your code. You should really avoid doing that, if at all possible, especially within your loop. It can really make debugging a pain. I only ever use "GoTo" statements in error handling, where I will have a single GoTo if it find an error, and then add my error handling code at the very bottom of my code, just before the End Sub. Since you have it within the loop, it may be working a handful of times, and then error out at the very end, or on one very specific record/workbook.
 
Upvote 0
Yes, the workbook was still active. All the goto's are to keep alignments between the different files, while not beautiful they work as desired.
The only error/incorrect action that is viewable is the Run-Time error when attempting to close the additional workbook.
 
Upvote 0
Did you try the breakpoint methodology while running your code like I suggested?
Without having access to all your files, I don't think I can really do anything else to test it, and I find your use of GoTo confusing to the point where I don't understand what you are using (there is a reason why they call code with a lot of GoTo's "spaghetti code"), so I really cannot critique that logic. I think you can probably replace the GoTos with If/Then or Select statements.
 
Upvote 0
Yes, I did use the break point as noted. The workbook is showing in use leading upto the .Close function. As soon as it hits that statement the error is raised.
It is spaghetti code, maybe it will get refined in future attempts but for today it works to sort the data how I need it to.
 
Upvote 0
Without having access to the files and testing our code for myself, I cannot say what the issue I may be.
I did a search on that error code on this site and found this thread: VBA - can't find the runtime error -2147221080 (800401a8)
I don't know if that helps. If it does not, I would recommend doing a Google Search on that error code, and seeing what other hits you get, and see if any of them are similar to your situation.
 
Upvote 0
Just grasping at straws here, are you making any changes to the workbook wb_FWD ?
If not does it make any difference is you put this line before the close line ?
VBA Code:
wb_FWD.Saved = True
 
Upvote 0
Solved:
ws_FWD.Activate not wb_FWD.Activate

I found by activating the worksheet instead of the workbook the error was resolved, and the workbook is able to be closed.
Thank you for the efforts, hope someone else benefits.
 
Upvote 0
Solution

Forum statistics

Threads
1,225,750
Messages
6,186,805
Members
453,373
Latest member
Ereha

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