Problem running multiple macros in one go

Michaelpfreem

Board Regular
Joined
Mar 14, 2008
Messages
92
Dear all,

I am having a problem I have written several smaller macros that provide steps I need in order to complete the function I am trying to do. I am trying to import some data, sort out a graph, sort the data then export the graph as a picture.

When I run each macro one at a time in steps they do exactly what I need them to. I can import tha data, I can sort the graph out to look at the new data tab (Rheotune), I can sort the data on the new tab and i can then export the graph once that is complete.

The problem is I try to put the macros together and it works all the way through except the graph that is exported has nothing on it, it will make the file but with nothing in it. Ultimately what i want to be able to do is have the macro open multiple files and produce a graph for each file that is opened, but currently I can't get it to work for one file does anyone have any ideas where I might be going wrong (see code below, I hope).

Any and all ideas much appreciated.

Many thanks in advance,
Mike

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Import()<br><br>    <SPAN style="color:#00007F">Dim</SPAN> FileList <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> MyFileName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FileCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FileNum <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> Wb <SPAN style="color:#00007F">As</SPAN> Workbook<br>    <SPAN style="color:#00007F">Dim</SPAN> strSheetName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><br><br><SPAN style="color:#00007F">Call</SPAN> Delete<br><br><br>ChDir "C:\"<br>FileList = Application.GetOpenFilename(, , , , <SPAN style="color:#00007F">True</SPAN>)<br><SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    FileCount = <SPAN style="color:#00007F">UBound</SPAN>(FileList)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">If</SPAN> Err.Number = 0 <SPAN style="color:#00007F">Then</SPAN><br><SPAN style="color:#00007F">For</SPAN> FileNum = 1 <SPAN style="color:#00007F">To</SPAN> FileCount<br>            MyFileName = FileList(FileNum)<br>            <br>Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>Workbooks.OpenText Filename:=MyFileName, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=<SPAN style="color:#00007F">False</SPAN>, Space:=True, Other:=<SPAN style="color:#00007F">False</SPAN>, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=<SPAN style="color:#00007F">True</SPAN><br>    ActiveSheet.Select<br>    ActiveSheet.Copy Before:=ThisWorkbook.Sheets(1)<br>    strSheetName = ActiveSheet.Name<br>    Sheets("Instructions").Range("B10").Value = strSheetName<br>    ActiveSheet.Name = "RheoTune"<br>    <br>    <br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Wb <SPAN style="color:#00007F">In</SPAN> Workbooks<br>    <SPAN style="color:#00007F">If</SPAN> Wb.Name <> "Excel2KMLRheo.xlsm" <SPAN style="color:#00007F">Then</SPAN><br>        Wb.Close savechanges:=<SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">Next</SPAN> Wb<br>Application.StatusBar = "All Workbooks Closed."<br><br><br><SPAN style="color:#00007F">Call</SPAN> GraphSetup<br><SPAN style="color:#00007F">Call</SPAN> SortData<br><SPAN style="color:#00007F">Call</SPAN> Delete<br><br><SPAN style="color:#00007F">Next</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#007F00">'Sheets("Control").Select</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> ExportChart()<br>     <SPAN style="color:#007F00">'   Export a selected chart as a picture</SPAN><br>    <SPAN style="color:#00007F">Const</SPAN> sSlash$ = "/"<br>    <SPAN style="color:#00007F">Const</SPAN> sPicType$ = ".gif"<br>    <SPAN style="color:#00007F">Dim</SPAN> sChartName$<br>    <SPAN style="color:#00007F">Dim</SPAN> sPath$<br>    <SPAN style="color:#00007F">Dim</SPAN> sBook$<br>    <SPAN style="color:#00007F">Dim</SPAN> chtObj <SPAN style="color:#00007F">As</SPAN> ChartObject<br>    <SPAN style="color:#00007F">Dim</SPAN> cht <SPAN style="color:#00007F">As</SPAN> Chart<br>    <br>    Application.DisplayAlerts = False<br><br>    Sheets("Graph").Select<br>    ActiveSheet.Calculate<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <br>    Range("A1").Select<br>    Selection.ClearContents<br>    <br>    <SPAN style="color:#00007F">With</SPAN> Range("B2:M57")<br>    .CopyPicture Appearance:=xlScreen, Format:=xlBitmap<br>   <SPAN style="color:#007F00">'size to range plus a bit for chart border</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> chtObj = ActiveSheet.ChartObjects.Add( _<br>    .Left, .Top, .Width, .Height)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br>     <SPAN style="color:#007F00">'   If a name was given, chart is exported as a picture in the same</SPAN><br>     <SPAN style="color:#007F00">'   folder location as their current file</SPAN><br>    chtObj.Chart.Paste<br>    sBook = ActiveWorkbook.Path<br>    sChartName = Sheets("Instructions").Range("B10")<br>    sPath = sBook & sSlash & sChartName & sPicType<br>    chtObj.Chart.Export Filename:=sPath, FilterName:="GIF"<br>    chtObj.Delete<br><br>    Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> GraphSetup()<br><br>    Sheets("Graph").Select<br>    <br>    Range("C51").Select<br>    ActiveCell.FormulaR1C1 = "=RheoTune!R[-46]C[-1]"<br>    Range("C52").Select<br>    ActiveCell.FormulaR1C1 = "=""Max.Depth: ""&RheoTune!R[-36]C[1]:R[2094]C[1]"<br>    Range("C53").Select<br>    ActiveCell.FormulaR1C1 = "=""DensiTune- ""&RheoTune!R[-42]C"<br>    Range("C54").Select<br>    ActiveCell.FormulaR1C1 = _<br>        "=RheoTune!R[-42]C[-1]&"" ""&RheoTune!R[-42]C&"" ""&RheoTune!R[-42]C[1]"<br>    Range("C55").Select<br>    ActiveCell.FormulaR1C1 = "=RheoTune!R[-42]C[-1]&"" ""&RheoTune!R[-42]C"<br>    Range("C56").Select<br>    ActiveCell.FormulaR1C1 = _<br>        "=RheoTune!R[-42]C[-1]&"" ""&RheoTune!R[-42]C&"" ""&RheoTune!R[-42]C[1]&"" ""&RheoTune!R[-42]C[2]"<br>    Range("I52").Select<br>    ActiveCell.FormulaR1C1 = _<br>        "=""Location: ""&RheoTune!R[-44]C[-7]&"" X, ""&RheoTune!R[-44]C[-6]&"" Y"""<br>    Range("I53").Select<br>    ActiveCell.FormulaR1C1 = "=""Kp: 0.00m"""<br>    Range("I54").Select<br>    ActiveCell.FormulaR1C1 = _<br>        "=""Date: ""&TEXT(RheoTune!R[-50]C[-7],""dd/mm/yyyy"")"<br>    Range("I55").Select<br>    ActiveCell.FormulaR1C1 = "=""Time: ""&TEXT(RheoTune!R[-51]C[-6],""hh:mm:ss"")"<br>    Range("I56").Select<br>    ActiveCell.FormulaR1C1 = "=""Tide: ""&RheoTune!R[-47]C[-7]&"" m"""<br>      <br>      <br>    ActiveSheet.ChartObjects("Chart 1").Activate<br>    ActiveChart.PlotArea.Select<br>    ActiveChart.SeriesCollection(1).XValues = "=RheoTune!$E$16:$E$10000"<br>    ActiveChart.SeriesCollection(1).Values = "=RheoTune!$D$16:$D$10000"<br>    <br>    Range("A1").Select<br><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> Delete()<br><br><SPAN style="color:#00007F">Dim</SPAN> sh <SPAN style="color:#00007F">As</SPAN> Worksheet<br><br>Application.DisplayAlerts = False<br><br>Sheets("Control").Select<br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> sh <SPAN style="color:#00007F">In</SPAN> Sheets<br><SPAN style="color:#00007F">If</SPAN> sh.Name = "RheoTune" <SPAN style="color:#00007F">Then</SPAN><br>sh.Delete<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">Next</SPAN><br><br>Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> SortData()<br><br>    Sheets("RheoTune").Select<br>    <br>    Range("A16").Select<br>    Range(Selection, Selection.End(xlToRight)).Select<br>    Range(Selection, Selection.End(xlDown)).Select<br>    Range(Selection, Selection.End(xlDown)).Select<br>    Selection.AutoFilter<br>    ActiveSheet.Range("$A$16:$P$16").AutoFilter Field:=1, Criteria1:="DAT"<br>    Range("A16").Select<br>    <br>    <SPAN style="color:#00007F">Call</SPAN> ExportChart<br>    <br>    Sheets("RheoTune").Select<br>    Selection.AutoFilter<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Have you tried stepping thru the macro? I find my mistakes many times by doing that. Sometimes when you do the macros independently you have your start place differently than when you run the combined macros. HTH
 
Upvote 0
Yep I tried that, when I run through each one individually from start to finish it works with no problems, but when I try to run them all in one go it fails. I am assuming it is something to do with updating the graph dureing a marco but I thought I had included that in the macro. Obviously not though.

Thanks,
Mike
 
Upvote 0
if each macro works one at a time , you could try and create a quick VBA to run each macro one at a time.

Sub Run_All ()

Application.Run "Macro1"
Application.Run "Macro2"

End Sub

Just need to add them to run in the order you need.
 
Upvote 0
I tried that but still it just copies a blank graph, so something is still not updating the graph during the course of the macro.

I also need the macros to run inside the import macro, as I want to open multiple files and have them all run the macros.

Thanks,
Mike
 
Upvote 0
To make things a little more confusing I have been going through each macro running them all and then taking one at a time out of the loop to see what causes the problem.

It turns out that when I include the import macro in the loop that is what casues the export macro to export nothing. Inlcuding the others has no affect.

Has anyone got any idea why the import section would cause the export macro to export a blank graph?

I should point out during the tests I asked the export macro to export a section of an exisiting sheet as I wasn't running the import macro and so there would be no Rheotune sheet to copy from.

Thanks,
Mike
 
Upvote 0
What if you put a pause into your macro for that section to give the calculations time to run?
 
Upvote 0
Hello all,

Thanks for the help I tried the pause and that failed as well, I have now tracked the issue down, it is one of those embarrassing mistakes you wished you'd noticed before you posted the problem on the internet.

The issue is in the import sub and the issue is:

Application.ScreenUpdating = False

This would appare to stop the graphs updating when it comes to copying. I turned this line off and it all works fine now.

Sorry to have wasted peoples time but thanks for the suggestions.

Thanks,
Mike
 
Upvote 0

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

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