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>
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>