0

I have a random issue I'm running into about once or twice a day, on a script that runs every 2 minutes from 7am to 5pm daily, and am hoping somebody here might be able to determine what could be causing it.

I'm going to try and keep this post as organized and to the point as possible, as the process utilizes 3 separate scripts.

Script #1 is housed in the PERSONAL.XLSB workbook & is the timer to begin the process @ 7am and repeat every 120 seconds. Code below:

**ThisWorkbook:
Private Sub Workbook_Open()
Application.OnTime TimeValue("07:00:00"), "'RunScripts2'"
End Sub

**Module1:
Sub RunScripts2()
On Error Resume Next
Shell "wscript ""R:\xxxx\xxxx\xxxx\scripts2.vbs""", vbNormalFocus
Dim scr As ScriptControl: Set scr = New ScriptControl
scr.Language = "VBScript"
Application.OnTime DateAdd("s", 120, Now), "RunScripts2"
End Sub

Script #2 is the scripts2.VBS script that Script #1 calls every 120 seconds. This opens an excel workbook and runs the macro "RunCopyPaste." Code below:

**scripts2.vbs:
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample() 

  Dim xlApp 
  Dim xlBook 
  Set xlApp = GetObject(,"Excel.Application") 
  xlApp.Visible = True
  xlApp.DisplayAlerts = False

  Set xlBook = xlApp.Workbooks.Open("\\xxxx\xxxx\xxxx\Model.xlsm",3,True) 

  Dim dteWait
  dteWait = DateAdd("s", 8, Now())
  Do Until (Now() > dteWait)
  Loop

  xlApp.Run "RunCopyPaste"

  Set xlApp = GetObject(,"Excel.Application") 

End Sub

Script #3 is housed in the Model.xlsm workbook that Script #2 calls. Code below:

**ThisWorkbook:
Private Sub Workbook_Open()
    Application.Run "BloombergUI.xla!RefreshAllWorkbooks"
    Application.Run "BloombergUI.xla!RefreshAllStaticData"
End Sub

**Module2:
Sub RunCopyPaste()
    On Error Resume Next
    Application.DisplayAlerts = False

    ChDir _
        "R:\xxxx\xxxx\xxxx\xxxx\"
    Workbooks.Open Filename:= _
        "R:\xxxx\xxxx\xxxx\xxxx\Data.xlsx" _
        , UpdateLinks:=3, ReadOnly:=True
    Application.Run "ConnectChartEvents"
    Windows("Model.xlsm").Activate
    Sheets("Sheet1").Select
    Range("B5:J94").Select
    Selection.Copy
    Windows("Data.xlsx").Activate
    Sheets("Sheet1").Select
    Range("B5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Windows("Model.xlsm").Activate
    Sheets("Sheet2").Select
    Range("C5:D73").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Data.xlsx").Activate
    Sheets("Sheet2").Select
    Range("C5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Windows("Model.xlsm").Activate
    Sheets("Sheet3").Select
    Range("B6:C7").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Data.xlsx").Activate
    Sheets("Sheet3").Select
    Range("B6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False


FN = Replace(ActiveWorkbook.Name, "temp_", "")
FN = "temp_" + FN

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path + Application.PathSeparator + FN

    ActiveWindow.Close False
    ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
    Sheets("Sheet1").Select
    ActiveWorkbook.Close
Dim wb As Workbook
Set wb = Workbooks.Add

End Sub

Issue I am currently facing

This process works great; however, once it breaks during the SaveAs "temp_Data.xlsx" process it wont save correctly unless I kill excel and restart the "RunScripts2" macro. The break usually occurs in the middle of the day, somewhere between 12-2pm. It's completely random. Here's exactly what happens: the process will run as expected but when it gets to the SaveAs "temp_Data.xlsx" it will show the loading bar displaying the saving progress much quicker than usual (~.2 seconds) and then close all workbooks, open a blank one just as it should and then repeat the process 120 seconds later. However, I noticed that the "temp_Data.xlsx" shows a Date Modified reflective of a previous run. And every subsequent run once it "breaks" will look like it runs as normal but the file will not be fully saved down and the file Date Modified will not reflect an updated run. My remedy has been to close excel and reopen it and manually kick off the "RunScripts2" macro to get the timer & process going again. I've had days where it goes the entire day without "breaking" and I've had some where it breaks multiple times in a day; however, most recently it breaks once mid-day and I restart it and it's fine until EOD.

Solutions I've Attempted Unsuccessfully I've tried keeping the alerts set to True but even that doesn't show any issues with the saving process. It's like it acts like it's saving but it doesn't really save. It's bizarre. I've done quite a bit of research on it and haven't found any solutions. I'm hoping somebody here has encountered something similar.

Many thanks in advance for any help!

UPDATED CODE BELOW

Seems to be working as of now... any further efficiencies I can make? Much appreciated.

scripts2 NEW.vbs:

Option Explicit
ExcelMacroExample
Sub ExcelMacroExample() 

Dim xlApp
Dim CopyFrom

Set xlApp = GetObject(,"Excel.Application") 
xlApp.Visible = True

Set CopyFrom = xlApp.Workbooks.Open("\\xxxx\xxxx\xxxx\Model NEW.xlsm",3,True) 

Dim dteWait
dteWait = DateAdd("s", 5, Now())
Do Until (Now() > dteWait)
Loop

CopyFrom.WorkSheets("Data").Activate()
CopyFrom.Worksheets("Data").Range("B1:K275").Copy
CopyFrom.Worksheets("Data").Range("B1").PasteSpecial -4163, -4142, False, False
xlApp.CutCopyMode = False

xlApp.DisplayAlerts = False
CopyFrom.SaveAs "\\xxxx\xxxx\xxxx\Model NEW.xlsx", 51
xlApp.DisplayAlerts = True

CopyFrom.Close False

Dim xlAppp
Set xlAppp = GetObject(,"Excel.Application") 
xlAppp.Visible = True

End Sub
DrewBerg
  • 1
  • 1
  • 1
    First, I would start by getting rid of every instance of `.Select` & .`Activate`. These are middle man operators here and should be, *as they say*, cut out – urdearboy Feb 27 '19 at 17:11
  • 3
    I would very highly recommend getting rid of `On Error Resume Next` in all of your code and handling your errors appropriately with an error handler. This will be an absolute nightmare for you (and anyone else) to troubleshoot if you can't pinpoint what is causing the error. – Tate Garringer Feb 27 '19 at 17:12
  • @TateGarringer thanks man, I did not take that out when I had turned the alerts to true... I will troubleshoot with that in mind. @ urdearboy could you elaborate on what I should do instead? I'm capable of googing to figure it out but would like to hear what command you would utilize – DrewBerg Feb 27 '19 at 19:43
  • 1
    I imagine @urdearboy would've linked [this](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba?rq=1) for the purpose of avoiding `Select` and `Activate`. Let us know what you find after you run it without suppressing the errors and we'll be able to be more helpful to you. – Tate Garringer Feb 27 '19 at 19:48
  • @TateGarringer thanks a ton dude! This is something my team is going to be relying on me heavily for an I want to get it as efficient as possible – DrewBerg Feb 27 '19 at 20:02
  • Regarding error handling and after you removed `On Error Resume Next` you might be interested in reading [VBA Error Handling – A Complete Guide](https://excelmacromastery.com/vba-error-handling) to see how to do error handling the right way. – Pᴇʜ Feb 28 '19 at 07:13
  • @TateGarringer do you think i could use VBS to script "Script #3" and put that inside of "Script #2" in replacement of xlApp.Run "RunCopyPaste"? – DrewBerg Mar 05 '19 at 14:28
  • Yes, that's entirely possible, but whether or not that would actually fix your problem or just relocate it remains to be determined. – Tate Garringer Mar 05 '19 at 15:36
  • @TateGarringer I appreciate the help... I've been working on this and have rescripted this to work much more efficiently (in my opinion).... any chance you could give this a once over to let me know if there's anything that can be more efficient or better scripted? I still have the PERSONAL.XLSB running the VBS script "scripts2" every 120 seconds but now I've coded all the copy/pasting in VBS instead of having VBS run a macro inside excel and all that stuff that was going on.. – DrewBerg Mar 07 '19 at 20:31
  • You say _any further efficiencies I can make?_ well, the biggest improvement I can see would be to not use VBS at all. I see nothing in the code posted that can't be done in VBA directly. – chris neilsen Mar 07 '19 at 21:04

0 Answers0