简体   繁体   中英

Excel VBA Macro: create a chart from CSV?

I have a CSV file with three columns (A,B,C).

I can record a Macro which selects Col A + Col B, then inserts a chart of A versus B.

This works, but the code generated contains a hardcoded ref to the 'Sheet1' like this:

...
ActiveChart.SetSourceData Source:=Range( _
        "'Sheet1'!$A:$A,'Sheet1'!$B:$B,'Sheet1'!$A:$A,'Sheet1'!$B:$B")
...

So I change this to match the active document like this:

...
ActiveChart.SetSourceData Source:=ActiveSheet.Range("$A:$B")
...

This works, however I need to also insert a chart using COL A + COL C, the generated code looks like this:

...
ActiveChart.SetSourceData Source:=ActiveSheet.Range("'Sheet1'!$A:$A,'Sheet1'!$C:$C")
...

How to a similarily change this code to make it sheetname-agnostic ? [ The issue is that how to do I select two columns which are not adjacent to each other - I think I was luck in the first example - its a special case ]

怎么样:

ActiveChart.SetSourceData Source:=ActiveSheet.Range("$A:$A,$C:$C")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM