0

I use a batch file with the following code:

cscript DeleteDuplicateDataMacro.vbs "C:\Users\techadmin\Documents\DeleteDuplicateDataMacro\DeleteDuplicateDataMacro.xlsm

Which runs this VBScript code:

Dim args, objExcel

Set args = wScript.Arguments
Set objExcel = CreateObject("Excel.Application")

objExcel.Workbooks.Open args(0)
objExcel.Visible = True

ObjExcel.Run "DeleteDuplicateData"

objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit

Which opens my Macro-Enabled Workbook and runs the following Macro:

Sub DeleteDuplicateData()  
'  
' DeleteDuplicateData Macro  
'  

'  
    Application.DisplayAlerts = False  
    **With ActiveSheet.QueryTables(1).Refresh _
        .Connection = "TEXT;C:\Users\techadmin\Documents\Sapphire Report Agent\Sapphire_NK_Export.csv"**  
        '.CommandType = 0  
        .Name = "Sapphire_NK_Export"  
        .FieldNames = True  
        .RowNumbers = False  
        .FillAdjacentFormulas = False  
        .PreserveFormatting = True  
        .RefreshOnFileOpen = False  
        .RefreshStyle = xlInsertDeleteCells  
        .SavePassword = False  
        .SaveData = True  
        .AdjustColumnWidth = True  
        .RefreshPeriod = 0  
        .TextFilePromptOnRefresh = False  
        .TextFilePlatform = 437  
        .TextFileStartRow = 1  
        .TextFileParseType = xlDelimited  
        .TextFileTextQualifier = xlTextQualifierDoubleQuote  
        .TextFileConsecutiveDelimiter = False  
        .TextFileTabDelimiter = False  
        .TextFileSemicolonDelimiter = False  
        .TextFileCommaDelimiter = True  
        .TextFileSpaceDelimiter = False  
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1)  
        .TextFileTrailingMinusNumbers = True  
        .Refresh BackgroundQuery:=False  
    End With  
    ActiveWindow.SmallScroll Down:=-9  
    ActiveSheet.Range("$A$1:$Q$2500").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlYes  
    ActiveSheet.Range("$A$1:$Q$2500").RemoveDuplicates Columns:=17, Header:=xlYes  
    ChDir "C:\Users\techadmin\Documents\Sapphire Report Agent"  
    ActiveWorkbook.SaveAs Filename:="C:\Users\techadmin\Documents\Sapphire Report Agent\Sapphire_NK_Export", FileFormat:=xlCSV, CreateBackup:=False  
    ActiveCell.FormulaR1C1 = "STATE_STUDENT_ID"  
    Range("A1:Q2500").Select  
    Range("A1").Activate  
    Selection.ClearContents  
    Range("A1").Select  
    ChDir "C:\Users\techadmin\Documents\DeleteDuplicateDataMacro"  
    ActiveWorkbook.SaveAs Filename:="C:\Users\techadmin\Documents\DeleteDuplicateDataMacro\DeleteDuplicateDataMacro", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False  
End Sub  

Every few times this sequence is run, my Macro-Enabled Workbook's file size gets a few Kilobytes larger. This increases exponentially. Once it gets around the 30,000 Kilobyte range then my other Workbook (the one that is getting imported during this macro, then edited and saved back to its original location) starts having it's columns duplicated.

Community
  • 1
  • 1
  • This is a question for https://superuser.com. Because it's _How do I configure Excel_. The answer is in Excel's Help - _Speedup Saving A File_. –  Jan 16 '20 at 16:31
  • Why do you add a new connection instead of refreshing it? – Nacorid Jan 16 '20 at 16:31
  • Also the tags [tag:cmd] [tag:vbscript] and [tag:wsh] can be removed, I believe. – Nacorid Jan 16 '20 at 16:33
  • @Nacorid I actually didn't even add the WSH tag so I'm not sure how it got there. I will remove the cmd and vbscript tags though. As far as the connection question. I don't know much about VBA. I just recorded a macro as a base and then modified it a bit after the recording. Excel used that during macro recording. What's the syntax for a connection refresh? I only saw Add and Item as methods for QueryTables object on Microsoft's website. https://learn.microsoft.com/en-us/office/vba/api/excel.querytables – KingKairos Jan 16 '20 at 16:41
  • @Mark Thanks for the input; however, I disagree. I'm not configuring excel, I'm looking at VBA code that just happens to be in excel. This site explicitly says during tag adding that if you're posting about VBA code in excel, then add the excel and vba tags... – KingKairos Jan 16 '20 at 16:43
  • First check how many connections you have with `MsgBox ActiveSheet.QueryTables.Count`, a refresh would look something like `Dim q as QueryTable: For each q in ActiveSheet.QueryTabels: q.refresh: Next`, also try to refrain from using `Select` `ActiveSheet` and `Activate` by having a look at [this](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) question. – Nacorid Jan 16 '20 at 16:45
  • Exactly, you are NOT configuring Excel to save small files so it will do _fast saves_. Read HELP. –  Jan 16 '20 at 16:48
  • `ActiveSheet.QueryTables(1).Refresh` Would also work for the first QueryTable in your QueryTables collection. – Nacorid Jan 16 '20 at 16:48
  • 2
    @Mark I believe you're missing the problem here. – Nacorid Jan 16 '20 at 16:49
  • You have to know a program to program it. Excel (and Word) does _fast saves_ by default. That is until a certain number of fast saves it does a full save. In a fast save changes are added to the END OF THE FILE so the original file is not rewritten to disk. Change the option as Help explains. –  Jan 16 '20 at 16:57
  • @Mark could you explain what "increasing file size per script interation" has to do with saving a file? Because I, for one, am lost. – Nacorid Jan 16 '20 at 17:04
  • I did explain it. Why won't you read Help. You just tick a box in Options. And if you do nothing sometimes it will grow and sometimes it will shrink as Excel manages fast file access (which MS has decades of user research saying that's what customers expect). –  Jan 16 '20 at 17:12
  • _Fast Saves_ (reexplained) The original file on disk is unchanged, a list of changes to the original file are _appended_ to the end of it. Every now and then Excel does a proper save. –  Jan 16 '20 at 17:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206083/discussion-between-nacorid-and-mark). – Nacorid Jan 16 '20 at 17:18
  • You do realize I'm not the poster, right? – Nacorid Jan 16 '20 at 17:32
  • No I don't. But file size can't change unless one saves. So that is how it is linked. –  Jan 16 '20 at 17:35
  • @Nacorid Sorry for the slow response. I am at work right now and got tied up in something. I will look at the other post you linked along with the info you provided me with. – KingKairos Jan 16 '20 at 18:12
  • Update your question with your findings when got the time – Nacorid Jan 16 '20 at 18:14
  • @Nacorid I've updated my question to reflect my changes. I tried to make the changes bold but apparently that doesn't work in code blocks, so instead the changes just have the ** surrounding them. I am now getting an "Object Required" error on those two lines though. – KingKairos Jan 16 '20 at 19:52

1 Answers1

0

That is because With expects an object to work with (no pun intended) while QueryTable.Refresh doesn't return one.
You will need to have a connection already set up for .Refresh to work.
I removed the adding of a new connection from your code and the cleanup part. If you want to add a new connection each time you open the workbook you should also remove it before saving and closing the workbook, but I don't recommend doing that, because Excel files tend to break that way over time.

Sub DeleteDuplicateData()  
'  
' DeleteDuplicateData Macro
'  
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False  
    ActiveSheet.QueryTables(1).Refresh BackgroundQuery:=False
    Application.CalculateUntilAsyncQueriesDone
    ActiveSheet.Range("$A$1:$Q$2500").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlYes  
    ThisWorkbook.SaveAs Filename:="C:\Users\techadmin\Documents\Sapphire Report Agent\Sapphire_NK_Export", FileFormat:=xlCSV, CreateBackup:=False
    ' I assume A1 to be the ActiveCell, but it's still referring to the ActiveSheet
    Range("A1").FormulaR1C1 = "STATE_STUDENT_ID"
    ThisWorkbook.SaveAs Filename:="C:\Users\techadmin\Documents\DeleteDuplicateDataMacro\DeleteDuplicateDataMacro", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Please look at this question again to understand how to avoid using Select, Activate and implicit ActiveSheet references.

Nacorid
  • 783
  • 8
  • 21