“Reset” collections after running a VBA script










-1















I am not sure why VB is doing this since I don't have a lot of experience. Basically, the first time I run my script the output is what I expect, but the second time it looks completely wrong.



Here's the code:



'define queue type
Dim Queue As New Collection


'define job list
Dim JobList As New Collection

'loop parameters
Dim rng As Range, rng2 As Range
Dim rng_w As Range, rng_w2 As Range

Dim TimeRange As Double




'loop over list and get jobs at frequency
Dim i As Double
Dim j As Integer



Sub Generate_Queue()
ThisWorkbook.Sheets("AGV capacity").Activate
Set rng = Range("L6:L7")
Set rng2 = Range("M6:M7")

'fill JobList

Dim Frequencies As Range
Set Frequencies = rng
Dim Jobs As Range
Set Jobs = rng2
Dim dividend

TimeRange = ThisWorkbook.Sheets("AGV capacity").Range("O6") * 3600



For i = 1 To rng.Count
Set tmp = New Transport
tmp.Frequency = (TimeRange / Frequencies(i))
tmp.SourceDest = Jobs(i)
JobList.Add tmp
Next i

i = 1

'while loop over time range (e.g. 24 hours)
While i < TimeRange
'for loop over number of entries in job list
For j = 1 To JobList.Count
'check if i is mod Frequency, meaning it should be added to the queue
If ((i Mod JobList(j).Frequency) = 0) Then
Set tmp = New Transport
dividend = (i / JobList(j).Frequency)

tmp.Frequency = ((dividend * JobList(j).Frequency) / 3600)
tmp.SourceDest = JobList(j).SourceDest
Queue.Add tmp
End If
Next j
i = i + 1
Wend

ThisWorkbook.Sheets("order queue").Activate
Set rng_w = Range("A2", "A" & Queue.Count)
Set rng_w2 = Range("B2", "B" & Queue.Count)

i = 1

For i = 1 To 1000
rng_w(i).Value = ""
rng_w2(i).Value = ""

Next i

For i = 1 To Queue.Count
rng_w(i).Value = Queue(i).Frequency
rng_w2(i).Value = Queue(i).SourceDest

Next i
End Sub


I am working on two different Excel sheets, but the data that's read stays the same over the two runs. When I watch the different collections, they change with the same data set and sometimes are even created twice! Coming from other languages, I expected them to be deleted each run, but that does not seem to be the case. How can I "reset" these object?










share|improve this question

















  • 1





    This line needs to be in the procedure Set Queue = New Collection and just Private Queue as collection for define queue type

    – Nathan_Sav
    Nov 14 '18 at 9:55












  • Where exactly do I have to put this and why?

    – mneumann
    Nov 14 '18 at 9:57






  • 1





    In your procedure Generate_Queue just before you use Queue for the first time. Google the New keyword in VBA.

    – Nathan_Sav
    Nov 14 '18 at 9:59












  • Thanks, this is what solved the problem.

    – mneumann
    Nov 14 '18 at 10:57















-1















I am not sure why VB is doing this since I don't have a lot of experience. Basically, the first time I run my script the output is what I expect, but the second time it looks completely wrong.



Here's the code:



'define queue type
Dim Queue As New Collection


'define job list
Dim JobList As New Collection

'loop parameters
Dim rng As Range, rng2 As Range
Dim rng_w As Range, rng_w2 As Range

Dim TimeRange As Double




'loop over list and get jobs at frequency
Dim i As Double
Dim j As Integer



Sub Generate_Queue()
ThisWorkbook.Sheets("AGV capacity").Activate
Set rng = Range("L6:L7")
Set rng2 = Range("M6:M7")

'fill JobList

Dim Frequencies As Range
Set Frequencies = rng
Dim Jobs As Range
Set Jobs = rng2
Dim dividend

TimeRange = ThisWorkbook.Sheets("AGV capacity").Range("O6") * 3600



For i = 1 To rng.Count
Set tmp = New Transport
tmp.Frequency = (TimeRange / Frequencies(i))
tmp.SourceDest = Jobs(i)
JobList.Add tmp
Next i

i = 1

'while loop over time range (e.g. 24 hours)
While i < TimeRange
'for loop over number of entries in job list
For j = 1 To JobList.Count
'check if i is mod Frequency, meaning it should be added to the queue
If ((i Mod JobList(j).Frequency) = 0) Then
Set tmp = New Transport
dividend = (i / JobList(j).Frequency)

tmp.Frequency = ((dividend * JobList(j).Frequency) / 3600)
tmp.SourceDest = JobList(j).SourceDest
Queue.Add tmp
End If
Next j
i = i + 1
Wend

ThisWorkbook.Sheets("order queue").Activate
Set rng_w = Range("A2", "A" & Queue.Count)
Set rng_w2 = Range("B2", "B" & Queue.Count)

i = 1

For i = 1 To 1000
rng_w(i).Value = ""
rng_w2(i).Value = ""

Next i

For i = 1 To Queue.Count
rng_w(i).Value = Queue(i).Frequency
rng_w2(i).Value = Queue(i).SourceDest

Next i
End Sub


I am working on two different Excel sheets, but the data that's read stays the same over the two runs. When I watch the different collections, they change with the same data set and sometimes are even created twice! Coming from other languages, I expected them to be deleted each run, but that does not seem to be the case. How can I "reset" these object?










share|improve this question

















  • 1





    This line needs to be in the procedure Set Queue = New Collection and just Private Queue as collection for define queue type

    – Nathan_Sav
    Nov 14 '18 at 9:55












  • Where exactly do I have to put this and why?

    – mneumann
    Nov 14 '18 at 9:57






  • 1





    In your procedure Generate_Queue just before you use Queue for the first time. Google the New keyword in VBA.

    – Nathan_Sav
    Nov 14 '18 at 9:59












  • Thanks, this is what solved the problem.

    – mneumann
    Nov 14 '18 at 10:57













-1












-1








-1








I am not sure why VB is doing this since I don't have a lot of experience. Basically, the first time I run my script the output is what I expect, but the second time it looks completely wrong.



Here's the code:



'define queue type
Dim Queue As New Collection


'define job list
Dim JobList As New Collection

'loop parameters
Dim rng As Range, rng2 As Range
Dim rng_w As Range, rng_w2 As Range

Dim TimeRange As Double




'loop over list and get jobs at frequency
Dim i As Double
Dim j As Integer



Sub Generate_Queue()
ThisWorkbook.Sheets("AGV capacity").Activate
Set rng = Range("L6:L7")
Set rng2 = Range("M6:M7")

'fill JobList

Dim Frequencies As Range
Set Frequencies = rng
Dim Jobs As Range
Set Jobs = rng2
Dim dividend

TimeRange = ThisWorkbook.Sheets("AGV capacity").Range("O6") * 3600



For i = 1 To rng.Count
Set tmp = New Transport
tmp.Frequency = (TimeRange / Frequencies(i))
tmp.SourceDest = Jobs(i)
JobList.Add tmp
Next i

i = 1

'while loop over time range (e.g. 24 hours)
While i < TimeRange
'for loop over number of entries in job list
For j = 1 To JobList.Count
'check if i is mod Frequency, meaning it should be added to the queue
If ((i Mod JobList(j).Frequency) = 0) Then
Set tmp = New Transport
dividend = (i / JobList(j).Frequency)

tmp.Frequency = ((dividend * JobList(j).Frequency) / 3600)
tmp.SourceDest = JobList(j).SourceDest
Queue.Add tmp
End If
Next j
i = i + 1
Wend

ThisWorkbook.Sheets("order queue").Activate
Set rng_w = Range("A2", "A" & Queue.Count)
Set rng_w2 = Range("B2", "B" & Queue.Count)

i = 1

For i = 1 To 1000
rng_w(i).Value = ""
rng_w2(i).Value = ""

Next i

For i = 1 To Queue.Count
rng_w(i).Value = Queue(i).Frequency
rng_w2(i).Value = Queue(i).SourceDest

Next i
End Sub


I am working on two different Excel sheets, but the data that's read stays the same over the two runs. When I watch the different collections, they change with the same data set and sometimes are even created twice! Coming from other languages, I expected them to be deleted each run, but that does not seem to be the case. How can I "reset" these object?










share|improve this question














I am not sure why VB is doing this since I don't have a lot of experience. Basically, the first time I run my script the output is what I expect, but the second time it looks completely wrong.



Here's the code:



'define queue type
Dim Queue As New Collection


'define job list
Dim JobList As New Collection

'loop parameters
Dim rng As Range, rng2 As Range
Dim rng_w As Range, rng_w2 As Range

Dim TimeRange As Double




'loop over list and get jobs at frequency
Dim i As Double
Dim j As Integer



Sub Generate_Queue()
ThisWorkbook.Sheets("AGV capacity").Activate
Set rng = Range("L6:L7")
Set rng2 = Range("M6:M7")

'fill JobList

Dim Frequencies As Range
Set Frequencies = rng
Dim Jobs As Range
Set Jobs = rng2
Dim dividend

TimeRange = ThisWorkbook.Sheets("AGV capacity").Range("O6") * 3600



For i = 1 To rng.Count
Set tmp = New Transport
tmp.Frequency = (TimeRange / Frequencies(i))
tmp.SourceDest = Jobs(i)
JobList.Add tmp
Next i

i = 1

'while loop over time range (e.g. 24 hours)
While i < TimeRange
'for loop over number of entries in job list
For j = 1 To JobList.Count
'check if i is mod Frequency, meaning it should be added to the queue
If ((i Mod JobList(j).Frequency) = 0) Then
Set tmp = New Transport
dividend = (i / JobList(j).Frequency)

tmp.Frequency = ((dividend * JobList(j).Frequency) / 3600)
tmp.SourceDest = JobList(j).SourceDest
Queue.Add tmp
End If
Next j
i = i + 1
Wend

ThisWorkbook.Sheets("order queue").Activate
Set rng_w = Range("A2", "A" & Queue.Count)
Set rng_w2 = Range("B2", "B" & Queue.Count)

i = 1

For i = 1 To 1000
rng_w(i).Value = ""
rng_w2(i).Value = ""

Next i

For i = 1 To Queue.Count
rng_w(i).Value = Queue(i).Frequency
rng_w2(i).Value = Queue(i).SourceDest

Next i
End Sub


I am working on two different Excel sheets, but the data that's read stays the same over the two runs. When I watch the different collections, they change with the same data set and sometimes are even created twice! Coming from other languages, I expected them to be deleted each run, but that does not seem to be the case. How can I "reset" these object?







excel vba excel-vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:52









mneumannmneumann

105111




105111







  • 1





    This line needs to be in the procedure Set Queue = New Collection and just Private Queue as collection for define queue type

    – Nathan_Sav
    Nov 14 '18 at 9:55












  • Where exactly do I have to put this and why?

    – mneumann
    Nov 14 '18 at 9:57






  • 1





    In your procedure Generate_Queue just before you use Queue for the first time. Google the New keyword in VBA.

    – Nathan_Sav
    Nov 14 '18 at 9:59












  • Thanks, this is what solved the problem.

    – mneumann
    Nov 14 '18 at 10:57












  • 1





    This line needs to be in the procedure Set Queue = New Collection and just Private Queue as collection for define queue type

    – Nathan_Sav
    Nov 14 '18 at 9:55












  • Where exactly do I have to put this and why?

    – mneumann
    Nov 14 '18 at 9:57






  • 1





    In your procedure Generate_Queue just before you use Queue for the first time. Google the New keyword in VBA.

    – Nathan_Sav
    Nov 14 '18 at 9:59












  • Thanks, this is what solved the problem.

    – mneumann
    Nov 14 '18 at 10:57







1




1





This line needs to be in the procedure Set Queue = New Collection and just Private Queue as collection for define queue type

– Nathan_Sav
Nov 14 '18 at 9:55






This line needs to be in the procedure Set Queue = New Collection and just Private Queue as collection for define queue type

– Nathan_Sav
Nov 14 '18 at 9:55














Where exactly do I have to put this and why?

– mneumann
Nov 14 '18 at 9:57





Where exactly do I have to put this and why?

– mneumann
Nov 14 '18 at 9:57




1




1





In your procedure Generate_Queue just before you use Queue for the first time. Google the New keyword in VBA.

– Nathan_Sav
Nov 14 '18 at 9:59






In your procedure Generate_Queue just before you use Queue for the first time. Google the New keyword in VBA.

– Nathan_Sav
Nov 14 '18 at 9:59














Thanks, this is what solved the problem.

– mneumann
Nov 14 '18 at 10:57





Thanks, this is what solved the problem.

– mneumann
Nov 14 '18 at 10:57












1 Answer
1






active

oldest

votes


















1














The answer is to not use the New keyword when you Dim the variable, especially when you define it outside of a procedure, since you don't directly control when it will be instantiated.



If you need Queue and JobList to be available more widely then they're defined in the correct place, but when you execute Generate_Queue, that's probably when you should actually create the variables.



So, at the top of your module:



Dim Queue As Collection
Dim JobList As Collection


And within Generate_Queue:



Set Queue = New Collection
Set JobList = New Collection


So every time Generate_Queue is called, those two variables are created fresh (if that's what you want to happen).






share|improve this answer























  • This is what I wanted, thank you.

    – mneumann
    Nov 14 '18 at 10:56










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297297%2freset-collections-after-running-a-vba-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The answer is to not use the New keyword when you Dim the variable, especially when you define it outside of a procedure, since you don't directly control when it will be instantiated.



If you need Queue and JobList to be available more widely then they're defined in the correct place, but when you execute Generate_Queue, that's probably when you should actually create the variables.



So, at the top of your module:



Dim Queue As Collection
Dim JobList As Collection


And within Generate_Queue:



Set Queue = New Collection
Set JobList = New Collection


So every time Generate_Queue is called, those two variables are created fresh (if that's what you want to happen).






share|improve this answer























  • This is what I wanted, thank you.

    – mneumann
    Nov 14 '18 at 10:56















1














The answer is to not use the New keyword when you Dim the variable, especially when you define it outside of a procedure, since you don't directly control when it will be instantiated.



If you need Queue and JobList to be available more widely then they're defined in the correct place, but when you execute Generate_Queue, that's probably when you should actually create the variables.



So, at the top of your module:



Dim Queue As Collection
Dim JobList As Collection


And within Generate_Queue:



Set Queue = New Collection
Set JobList = New Collection


So every time Generate_Queue is called, those two variables are created fresh (if that's what you want to happen).






share|improve this answer























  • This is what I wanted, thank you.

    – mneumann
    Nov 14 '18 at 10:56













1












1








1







The answer is to not use the New keyword when you Dim the variable, especially when you define it outside of a procedure, since you don't directly control when it will be instantiated.



If you need Queue and JobList to be available more widely then they're defined in the correct place, but when you execute Generate_Queue, that's probably when you should actually create the variables.



So, at the top of your module:



Dim Queue As Collection
Dim JobList As Collection


And within Generate_Queue:



Set Queue = New Collection
Set JobList = New Collection


So every time Generate_Queue is called, those two variables are created fresh (if that's what you want to happen).






share|improve this answer













The answer is to not use the New keyword when you Dim the variable, especially when you define it outside of a procedure, since you don't directly control when it will be instantiated.



If you need Queue and JobList to be available more widely then they're defined in the correct place, but when you execute Generate_Queue, that's probably when you should actually create the variables.



So, at the top of your module:



Dim Queue As Collection
Dim JobList As Collection


And within Generate_Queue:



Set Queue = New Collection
Set JobList = New Collection


So every time Generate_Queue is called, those two variables are created fresh (if that's what you want to happen).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 10:49









NeepNeepNeepNeepNeepNeep

67547




67547












  • This is what I wanted, thank you.

    – mneumann
    Nov 14 '18 at 10:56

















  • This is what I wanted, thank you.

    – mneumann
    Nov 14 '18 at 10:56
















This is what I wanted, thank you.

– mneumann
Nov 14 '18 at 10:56





This is what I wanted, thank you.

– mneumann
Nov 14 '18 at 10:56



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297297%2freset-collections-after-running-a-vba-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo