Results 1 to 1 of 1

Looping -import each text file into a different worksheet and insert a chart

Threaded View

  1. #1
    Registered User
    Join Date
    03-06-2008
    Posts
    1

    Looping -import each text file into a different worksheet and insert a chart

    I'm a bit of a novice with this, so bear with me...

    I have lots of text files with data in them. I need to import each file into a different worksheet within the same workbook. After each set of data is imported I need to insert a chart on the same worksheet (all scales,axis,etc the same for each worksheet).

    Now I've managed a code by using the record macro function that does this fine, but only for one set of data at a time. And it means I need to change the sheet number, chart title, text file name etc every time...

    I'm sure there is an obvious way to sort this by looping, but I'm not sure how.

    Here's my macro. The numbers in red are the numbers that I need to increase by 1 everytime it loops... and I need it to loop around 50 times.



    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 06/03/2008 by Me
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\Experiment Results\26_2_test1.txt" _
            , Destination:=Range("A1"))
            .Name = "26_2_test1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Charts.Add
        ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
        ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:C1001"), PlotBy _
            :=xlColumns
        ActiveChart.SeriesCollection(2).Delete
        ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
        With ActiveChart
            .HasTitle = True
            .ChartTitle.Characters.Text = "Test 1"
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (s)"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Load (N)"
        End With
        ActiveChart.ChartTitle.Select
        Selection.Characters.Text = "Test 1"
        Selection.AutoScaleFont = False
        With Selection.Characters(Start:=1, Length:=7).Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
        End With
        ActiveChart.Axes(xlValue).Select
        With ActiveChart.Axes(xlValue)
            .MinimumScale = 0
            .MaximumScale = 10
            .MinorUnit = 0.5
            .MajorUnit = 1
            .Crosses = xlAutomatic
            .ReversePlotOrder = False
            .ScaleType = xlLinear
            .DisplayUnit = xlNone
        End With
        ActiveChart.Axes(xlCategory).Select
        With ActiveChart.Axes(xlCategory)
            .MinimumScaleIsAuto = True
            .MaximumScale = 25
            .MinorUnitIsAuto = True
            .MajorUnitIsAuto = True
            .Crosses = xlAutomatic
            .ReversePlotOrder = False
            .ScaleType = xlLinear
            .DisplayUnit = xlNone
        End With
        ActiveWindow.Visible = False
        Sheets.Add
    End Sub
    Sub Macro2()
    '
    ' Macro2 Macro
    ' Macro recorded 06/03/2008 by Me
    '
    
    '
        ActiveSheet.ChartObjects("Chart 1").Activate
        ActiveChart.ChartTitle.Select
        Selection.Characters.Text = "Test 22"
        Selection.AutoScaleFont = False
        With Selection.Characters(Start:=1, Length:=7).Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
        End With
        ActiveChart.ChartArea.Select
    End Sub




    Any help would be hugely appreciated!
    Last edited by VBA Noob; 03-06-2008 at 03:00 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1