how to keep a log of previous runs in a seperate worksheet

purple_egg

New Member
Joined
Mar 9, 2015
Messages
5
I am currently working on VRP with "D arrays. I worked through initial solution, and now I did random descent technique. However, I am supposed to store the results from the previous runs that I did in the seperater worksheet in excel- a log has to be kept. There is a problem with my code, as the solutions do not show up in the second wosksheet and are not stored at all. Could someone have a look at my code and spot any mistake. I am sure it missing some commands, and IU don't know how to deal with it to get it right.

' log sub that try to keep results after every run DOES NOT WORK
Sub log(inputfilename As String)
Dim i As Long, n As Long
n = 100000
Sheets("Sheet2").Select
Sheets("Sheet2").Activate
Cells(1, 1).Value = "problem name"
Cells(1, 2).Value = "Initial Cost"
Cells(1, 3).Value = "Best Cost"

For i = 1 To n / 10000


Sheets("sheet2").Cells(i + 1, 1).Value = inputfilename

Sheets("sheet2").Cells(i + 1, 2).Value = Sheets("sheet1").Cells(1, 2).Value

Sheets("sheet2").Cells(i + 1, 3).Value = Sheets("sheet1").Cells(2, 2).Value

i = i + 1
Next i

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi.

I don't know how you are calling the macro but you need to pass it a file name. I have included a simple Test macro below that does that.

The only problem seems to be that you increment i inside the For next loop (i=i+1). That is not required as the loop will automatically do that. I commented it out.

Other than that it looks OK.

Code:
Sub log(inputfilename As String)
    Dim i As Long, n As Long
    n = 100000
    Sheets("Sheet2").Select
    Sheets("Sheet2").Activate
    Cells(1, 1).Value = "problem name"
    Cells(1, 2).Value = "Initial Cost"
    Cells(1, 3).Value = "Best Cost"
    
    For i = 1 To n / 10000
        Sheets("sheet2").Cells(i + 1, 1).Value = inputfilename
        Sheets("sheet2").Cells(i + 1, 2).Value = Sheets("sheet1").Cells(1, 2).Value
        Sheets("sheet2").Cells(i + 1, 3).Value = Sheets("sheet1").Cells(2, 2).Value
        'i = i + 1
    Next i

End Sub
Sub test()
    Call log("xxxxxxxx")
End Sub

You need to call the Test macro and that will call the log macro and pass a string to it.
When I tried it, it added the column headings to sheet2 and then added ten rows with file names in. (My sheet1 was empty.)
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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