I have a 32 bit excel 2016. The excel has a proprietary add-on which allows to query a proprietary database. the add-on includes a function DvCHRaw() which return an array of data.
I am writing a mother excel vba that list the query conditions. the vba tends to open new workbook and perform the query then close the workbook. for example, I will loop 1000 columns per day, and loop 10 days. I will save each day's data in its own folder with 1000 csv files.
However, the excel will be stuck at certain point, for example, 9 days, or 5 days. Not sure if it is memory related issue though I set wb = nothing after workbook closing.
Can anyone give a hand here?
here is the code I wrote
Sub raw_data_extract()
Set StartDate = Range("B1")
nextdate = StartDate
Set EndDate = Range("B2")
Do While nextdate <= EndDate
Set tagname = Range("E1")
savedate = Month(nextdate) & "-" & Day(nextdate) & "-" & Year(nextdate)
newfolderpath = "D:\rawdata\" & savedate
If Dir(newfolderpath, vbDirectory) = "" Then
MkDir (newfolderpath)
End If
Do While tagname.Value <> ""
Set wb = Workbooks.Add()
Range("A1").Value = nextdate
Range("B1").Value = tagname
Range("C1").Value = "=ROWS(DvCHRaw(""APP"",B1,FALSE,""Timestamp;Value"",""Local"",A$1,A$1+1,0,0,-1))"
Set arraystart = Range("A2")
Set arrayend = arraystart.Offset(Range("C1") - 1, 1)
Range(arraystart, arrayend).FormulaArray = "=DvCHRaw(""APP"",B1,FALSE,""Timestamp;Value"",""Local"",A$1,A$1+1,0,0,-1)"
Range("C1").Clear
Range("A2").Select
Range(Selection, Selection.End(xlDown)).NumberFormat = "m/d/yyyy h:mm:ss.000"
Range("A1").CurrentRegion.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.DisplayAlerts = False
replaceslash = Replace(tagname, "/", "#")
replaceslashdot = Replace(replaceslash, ".", "#")
newname = replaceslashdot & "_" & savedate
Application.ActiveWorkbook.SaveAs newfolderpath & "\" & newname, xlCSV
Application.DisplayAlerts = True
ActiveWorkbook.Close savechanges = False
Set tagname = tagname.Offset(1, 0)
Set wb = Nothing
Loop
nextdate = nextdate + 1
Loop
MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub