Results 1 to 1 of 1

how can i reduce this code

Threaded View

  1. #1
    Registered User
    Join Date
    09-16-2012
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    11

    how can i reduce this code

    Hi everyone!

    i just wondering how i can make my code better or make it somehow a bit little. just now its to much code.

    here my first code
    Sub AddChart()
        With ThisWorkbook.Worksheets("Chart").ChartObjects.Add(200, 200, 600, 400).Chart
    
            If Not .HasTitle Then
                .HasTitle = True
                .ChartTitle.Text = "Temperature"
            End If
            
            .ChartType = xlXYScatterSmoothNoMarkers
            .Axes(xlValue).CrossesAt = -20
            .Axes(xlCategory).TickLabels.NumberFormat = "YYYY-MM-DD"
            
            With .SeriesCollection.NewSeries
                .Name = "Us"
                .XValues = "=Us!A2:A372"
                .Values = "=Us!C2:C370"
            End With
            
            With .SeriesCollection.NewSeries
                .Name = "Ai"
                .XValues = "=Ai!A2:A472"
                .Values = "=Ai!C2:C472"
            End With
            
            With .SeriesCollection.NewSeries
                 .Name = "Eu"
                 .XValues = "=Eu!A2:A572"
                 .Values = "=Eu!C2:C572"
            End With
        End With
    
        With ThisWorkbook.Worksheets("Chart").ChartObjects.Add(200, 200, 600, 400).Chart
            
            If Not .HasTitle Then
                .HasTitle = True
                .ChartTitle.Text = "Precipitation"
            End If
            
            .ChartType = xlXYScatterSmoothNoMarkers
            .Axes(xlValue).CrossesAt = -100
            .Axes(xlCategory).TickLabels.NumberFormat = "YYY-MM-DD"
            With .SeriesCollection.NewSeries
                .Name = "Us"
                .XValues = "=Us!A2:A372"
                .Values = "=Us!D2:D372"
            End With
            With .SeriesCollection.NewSeries
                .Name = "Ai"
                .XValues = "=Ai!A2:A371"
                .Values = "=Ai!D2:D371"
            End With
        End With
    End Sub
    after that i make it like this
    
    Sub AddChart(namn As String, UxV As String, UyV As String, AxV As String, AyV As String, ExV As String, EyV As String, CA As Integer)
        
        With ThisWorkbook.Worksheets("Chart").ChartObjects.Add(200, 200, 600, 400).Chart
            .Parent.Name = namn
            
            If Not .HasTitle Then
                .HasTitle = True
                .ChartTitle.Text = namn
            End If
            
            .ChartType = xlXYScatterSmoothNoMarkers
            .Axes(xlValue).CrossesAt = CA
            .Axes(xlCategory).TickLabels.NumberFormat = "YYYY-MM-DD"
            
            With .SeriesCollection.NewSeries
                .Name = "Us"
                .XValues = UxV
                .Values = UyV
            End With
            
            With .SeriesCollection.NewSeries
                .Name = "Ai"
                .XValues = AxV
                .Values = AyV
            End With
            
            With .SeriesCollection.NewSeries
                 .Name = "Eu"
                 .XValues = ExV
                 .Values = EyV
            End With
        End With
    
    End Sub
    --------------------
    
    Sub calsub()
    
        Dim n As String
        Dim CA As Integer
        Dim UxT As String
        Dim UyT As String
        Dim AxT As String
        Dim AyT As String
        Dim ExT As String
        Dim EyT As String
        
        Dim n2 As String
        Dim CA2 As Integer
        Dim UxP As String
        Dim UyP As String
        Dim AxP As String
        Dim AyP As String
        Dim ExP As String
        Dim EyP As String
        
        n = "Temperature"
        SxT = "=US!A2:A372"
        SyT = "=US!C2:C370"
        NxT = "=AI!A2:A472"
        NyT = "=AI!C2:C472"
        FxT = "=EU!A2:A572"
        FyT = "=EU!C2:C572"
        CA = -20
        
        n2 = "Precipitation"
        SxP = "=US!A2:A372"
        SyP = "=US!D2:D372"
        NxP = "=AI!A2:A371"
        NyP = "=AI!D2:D371"
        FxP = ""
        FyP = ""
        CA = -100
        
        Call AddChart(n, UxT, UyT, AxT, AyT, ExT, EyT, CA)
        Call AddChart(n2, UxP, UyP, AxP, AyP, ExP, EyP, CA)
        
    End Sub
    Last edited by newapa; 09-19-2012 at 10:12 AM.

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