VBA Not Copying Dynamic Graph Range When Importing CSV

scotboy15

New Member
Joined
Mar 19, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hi All,

Hoping someone can help me.

During isolation I've got some spare time and have created an Excel workbook with a macro that when a button is pressed it will allow the user to select a CSV file. The Macro then creates a copy of a "Template" work sheet which contains a Graph that contains dynamic ranges. The import of the CSV works correctly and everything appears fine except the dynamic range of the graph isn't copied across and it reverts to static references. The OFFSET function and things copys across is the name manager but the graphs dynamic range reference does not.

I've been trying this for a huge amount of time and not getting anywhere but am sure it is childs play for some of you Excel/VBA experts.

The excel file can be downloaded here and I have included a sample CSV file for reference: Spreadsheet - Google Drive

The code in the macro is:

VBA Code:
Sub GetCSVList()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
With dlgOpen
    .AllowMultiSelect = True
    ''Start in
    .InitialFileName = "C:\Test"
    .Show
End With

For Each fname In dlgOpen.SelectedItems
    ImportCSV fname
Next
End Sub



Sub ImportCSV(fname)
ws = Worksheets("Template").Copy(Before:=Worksheets("Template"))
'Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
ActiveSheet.Name = Mid(fname, InStrRev(fname, "\") + 1)

With ActiveSheet.QueryTables.Add( _
        Connection:="TEXT;" & fname, _
        Destination:=Range("B3"))
    .Name = "Test" & Worksheets.Count + 1
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlOverwriteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 65001
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .Refresh BackgroundQuery:=False
    '.UseListObject = False
End With

ActiveSheet.Name = Range("L17")
ActiveSheet.Range("B4:G4").Clear
ActiveSheet.Range("E3").Clear
ActiveSheet.Range("A8:A9").EntireRow.Insert
ActiveSheet.Range("B5:C5").Cut Range("B9:C9")
ActiveSheet.Range("C7").Value = "seconds"


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This has been solved. The code below needs to be added to the import CSV method:

VBA Code:
With ActiveSheet.ChartObjects(1).Chart
    .SeriesCollection(1).Values = "=" & Range("B10", Range("B" & Rows.Count).End(xlUp)).Address(1, 1, xlR1C1, 1)
    .SeriesCollection(2).Values = "=" & Range("C10", Range("C" & Rows.Count).End(xlUp)).Address(1, 1, xlR1C1, 1)
End With
 
Upvote 0

Forum statistics

Threads
1,215,738
Messages
6,126,579
Members
449,319
Latest member
iaincmac

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