Create a chart in every *.xls file

Keala

New Member
Joined
Jul 9, 2018
Messages
37
I have issue which I hope someone can suggest a solution for.

I have several *.csv files in a folder, which my program can open and convert to *.xls-file. What I need to do is to make a chart in every new open *.xls in an certain range. The file name and sheet name is for example
"Wv_B-2_Fre" what is changing for each file is the number which is increased by one (the last part of the code below)
So my question is how do I make the code, so it create a chart in each open file. The code below only create chart in first file then it pop a alert code.

Thank you for your help,

[/code]

Selection.FormatConditions(1).StopIfTrue = False
ActiveWorkbook.SaveAs filename:=folderName & "" & Replace(myFile, ".txt", ".xlsx"), FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Range("B300039").Select
ActiveCell.FormulaR1C1 = "=MAX(R[-300001]C:R[-2]C)"
Range("B300040").Select
ActiveCell.FormulaR1C1 = "=MIN(R[-300002]C:R[-2]C)"
Range("B300041").Select
ActiveCell.FormulaR1C1 = "=(R[-2]C-R[-1]C)"
Range("A300039").Select
ActiveCell.FormulaR1C1 = "Max"
Range("A300040").Select
ActiveCell.FormulaR1C1 = "Min"
Range("A300041").Select
ActiveCell.FormulaR1C1 = "Diff"

'filename = ActiveWorkbook.Name
ActiveCell.FormulaR1C1 = filename
Range("B300020:B300037").Select
Range("B300037").Activate
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range( _
"'Wv_Bursts-2_Fre_'!$B$300020:$B$300037")
Code:
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
What is the error and when does it pop up?
Barring the beginner's look of your code (no offence) the last line puzzles me.
If what I assume is true (which is - everything happens in the active sheet) then try it like this:
Code:
Selection.FormatConditions(1).StopIfTrue = False
ActiveWorkbook.SaveAs filename:=folderName & "" & Replace(myFile, ".txt", ".xlsx"), FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
with Activesheet
  .Range("B300039").FormulaR1C1 = "=MAX(R[-300001]C:R[-2]C)"
  .Range("B300040").FormulaR1C1 = "=MIN(R[-300002]C:R[-2]C)"
  .Range("B300041").FormulaR1C1 = "=(R[-2]C-R[-1]C)"
  .Range("A300039").FormulaR1C1 = "Max"
  .Range("A300040").FormulaR1C1 = "Min"
  .Range("A300041").FormulaR1C1 = "Diff"


'filename = ActiveWorkbook.Name
'[COLOR=#ff0000]ActiveCell.FormulaR1C1 = filename[/COLOR]
  .Range("B300020:B300037").Select
  .Range("B300037").Activate
  .Shapes.AddChart2(240, xlXYScatter).Select

  ActiveChart.SetSourceData Source:= .Range("B300020:B300037").address
end With
I have commented the red line because it is also confusing - maybe a part of the code is missing?
 
Last edited:
Upvote 0
one correction:
It seems that setsourcedata requires a range object as input so the line has to change like this:
Code:
ActiveChart.SetSourceData .Range("B300020:B300037")
 
Upvote 0
Thank you for your suggestion, it works very well.

Could you please tell me how I should change the code if I want to set X-axis in the same range but from column A instead of as it is now a increased number series? And also if I want to add a new curve in the same chart but values from column C.

Thank you,
 
Upvote 0
Best I think is to use the macro recorder then adapt the captured code to you needs.
 
Upvote 0
Thank you, I have already tried it, but the difference between your suggestion and the record was to big to be able to transfer it.
Thank you for your help, it is highly appreciated.
 
Upvote 0
Well, try to capture a code with the recorder.
Then post it in a new post here and I am sure some will help with the necessary changes.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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