vba excel 2007引用数据?

时间:2014-03-20 20:48:14

标签: vba charts excel-2007

我从其他人那里继承了一些VBA代码。 .......其他一些代码是在excel中制作一些现有数据的图表。我只是没有得到源数据集的引用位置。 x轴的数据是什么?来自y轴的数据是什么?..........

........其他一些代码............

“的 * ** * ** * ** * ** * ** * ** “的 **** 图表 * ** * ** * * “的 * ** * ** * ** * ** * ** * ***

    Windows(nm_driver).Activate
    ActiveSheet.Range("A19") = ""
    'intialize the cell as empty

    '*******Copies the previous sheet and makes a new sheet********
    Windows(nm_out).Activate
    Sheets(2).Select
    Sheets(2).Copy After:=Sheets(Worksheets.Count)
    j = j + 1
    Sheets(j).Select
    Debug.Print "j=" & j
    Windows(nm_driver).Activate
    out_sht_nm = ActiveSheet.Range("A8") & " Plots"
    Debug.Print "out_sht_nm" & out_sht_nm

    ActiveSheet.Range("A10").Select
    Selection.Copy
    Windows(nm_out).Activate
    Sheets(j).Select
    ActiveSheet.Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '****Paste the heading in the plots tab*****
    Sheets(j).Name = out_sht_nm     '**** Assign the Sheet's name******
    Debug.Print "out_sht_nm=" & out_sht_nm

    '*********copies the actual data for seq num*********

    Windows(nm_inp2).Activate

    Sheets(i).Select
    ActiveSheet.Range("B3:B23").Select
    'i could use usedrange here
    Selection.Copy
    Windows(nm_out).Activate
    Sheets(j).Select
    ActiveSheet.Range("B3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


    Windows(nm_inp2).Activate
    Sheets(i).Select
    ActiveSheet.Range("C3").Select
    col_len = Selection.End(xlToRight).Column
    Debug.Print "col_len=" & col_len


    p = 3

    '*******This FOR loop if for checking till where we have columns ending with *_XB
    For x = col_len To 1 Step -1
        Debug.Print "x=" & x
        Windows(nm_inp2).Activate

        Sheets(i).Select
        ActiveSheet.Cells(3, x).Select
        Selection.Copy
        Windows(nm_driver).Activate
        ActiveSheet.Range("A24").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        If ActiveSheet.Range("A25") = "_Xb" Then
            Start = x
        Debug.Print "start=" & Start
        End If

    Next x


    '*******This FOR loop is to copy all the remaining columns from the input file*********
    For k = 3 To Start - 1
    'these columns do not end with _xb
        Debug.Print "k=" & k
        Windows(nm_inp2).Activate
        ActiveSheet.Cells(3, k).Select

        Selection.Copy
        Windows(nm_driver).Activate
        ActiveSheet.Range("A19").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        flag = ActiveSheet.Range("A21").Text        '***It just checks if the column does not end with _SL_?

        If flag = "_SL_" Then
        'do nothing

        Else

            Windows(nm_inp2).Activate
            Sheets(i).Select
            ActiveSheet.Cells(3, k).Select

            ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
            Selection.Copy

            Windows(nm_out).Activate
            ActiveSheet.Cells(3, p).Select

            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

            p = p + 1       '*******Variable (p) is still incrementing as we need to append the remaing columns after the *_XB columns********
        End If

        Debug.Print "flag=" & flag


    Next k

    '****This FOR loop is for pasting the columns which are ending with *_XB and creating the charts*****
    For k = Start To col_len

        Windows(nm_inp2).Activate
        ActiveSheet.Cells(3, k).Select
        Selection.Copy
        Windows(nm_driver).Activate
        ActiveSheet.Range("A19").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        flag = ActiveSheet.Range("A21").Text    '***It just checks if the column does not end with _SL_?

        If flag = "_SL_" Then


        Else

            chart_title = ActiveSheet.Range("A14").Text

            '**Copies the column which ends with *_XB from the spline consist input file***
            '***Variable (k) is for iterating the columns in the spline consist input file******
            Windows(nm_inp2).Activate
            Sheets(i).Select
            ActiveSheet.Cells(3, k).Select
            ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
            Selection.Copy

            '*****Pastes it in the output file and then creates the chart*******
            'Variable (p) is for iterating the columns in the output file******
            Windows(nm_out).Activate
            ActiveSheet.Cells(3, p).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


            p = p + 1

        End If

    Next k
    Debug.Print "k=" & k
    Debug.Print "p=" & p
    Debug.Print "chart_title=" & chart_title

    '*********** Creating charts and labelling the X-Axis accordingly****************

    start_2 = ((p - 3) / 2) + 3
    Z = 105
    m = 0
    n = 0

    Debug.Print "start_2=" & start_2
    For b = 3 To start_2 - 1

        Windows(nm_out).Activate
        Sheets(j).Select
        ActiveSheet.Cells(3, b).Select
        Selection.Copy
        Windows(nm_driver).Activate
        ActiveSheet.Range("A19").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        chart_title = ActiveSheet.Range("A14").Text

        Windows(nm_out).Activate
        Sheets(j).Select
        text1 = ActiveSheet.Cells(3, b).Text

        alp_in = 0

        For c = start_2 To p - 1
            text2 = ActiveSheet.Cells(3, c).Text
            If UCase(Left(text2, Len(text2) - 3)) = UCase(text1) Then
                alp_in = c
            End If
        Next c

        Debug.Print "alp_in=" & alp_in

        ActiveSheet.Cells(3, b).Select
        ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        ActiveSheet.Cells(3, Z).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        ActiveSheet.Cells(3, alp_in).Select
        ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        ActiveSheet.Cells(3, Z + 1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        ActiveSheet.Cells(3, Z) = ""
        ActiveSheet.Cells(3, Z + 1) = chart_title

        ActiveSheet.Cells(3, Z + 1).Select
        row_end = Selection.End(xlDown).Row
        ActiveSheet.Range(Cells(3, Z), Cells(row_end, Z + 1)).Select

        Debug.Print "row_end=" & row_end

        Call border_design

        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlLine
        ActiveChart.Parent.Name = "MyChart" & Z
        ActiveChart.Axes(xlCategory).Select
        Selection.TickLabelPosition = xlLow
        ActiveChart.Axes(xlCategory).TickLabelSpacing = 2

        With ActiveSheet.ChartObjects("MyChart" & Z)
            .Height = 260 ' resize
            .Width = 290  ' resize
            .Left = m * 300
            .Top = n * 270 + 600

        End With

        '**** Variable (m) defines the left alignment of the chart and Variable (n) defines teh top alignment of the chart***
        '**These variables are used because the requirement was to have only 3 charts in a row****

        ActiveChart.Legend.Select
        Selection.Delete
        ActiveChart.Axes(xlValue).Select
        ActiveChart.SetElement (msoElementPrimaryValueAxisTitleVertical)
        ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Log Odds"
        ActiveChart.Axes(xlValue).TickLabels.NumberFormat = "#,##0.00"
        ActiveChart.Axes(xlValue).CrossesAt = -100
        'ActiveChart.ChartTitle.Text = chart_title
        ActiveChart.ChartTitle.Font.Size = 10
        ActiveChart.ChartTitle.Font.Bold = True


        m = m + 1
        If m = 3 Then
           m = 0
           n = n + 1
        End If
        Z = Z + 2
    'm and n is to control the position of the chart
    Next b



    '********Now we have to create the Charts for the class variables as well which are coming from raw_data
    'input file****
    Windows(nm_driver).Activate
    ActiveSheet.Range("AA1").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)) = ""
    ActiveSheet.Range("AB1") = ""

    '*****Copies all the spline variables that are in the output file into the driver******
    '***Basically this will check if the variable coming from raw_data input file is a part of the spline variable or not***
    Windows(nm_out).Activate
    Sheets(j).Select
    ActiveSheet.Range("C3").Select
    ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Windows(nm_driver).Activate
    ActiveSheet.Range("AA1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    'transpose=true here converts row into column

    Windows(nm_inp1).Activate
    Sheets(i).Select
    ActiveSheet.Range("A3").Select
    tot_row = Selection.End(xlDown).Row '****Gives the total number of rows in raw_data input file*****

    v = 53          '******Variable (v) defines the column number where the class variables will get pasted in the M18 plots sheet****


    '****This FOR loop checks every variable of raw_data input file (only column A) with the list of spline variables and see if
    'it is a spline variable or a class variable. if it comes out to be a spline variable it leaves it and if it comes out to
    'be class variable it copies it in the M18 plots sheet and creates a graph out of that*********


    For g = 3 To tot_row

        Windows(nm_inp1).Activate
        text1 = ActiveSheet.Cells(g, 1).Text

        end1 = g

        Windows(nm_driver).Activate
        ActiveSheet.Range("AB1") = text1
        sum1 = ActiveSheet.Range("AC26")
        If sum1 = 1 Then
        '*****Checking if the variable is spline or class; if spline then sum1 = 1 else sum1 =0 ***

        Else
            Windows(nm_inp1).Activate

            '*****This FOR loop checks for the number of rows that single variable has in the raw_data input file*****
            For h = g + 1 To tot_row
                text11 = ActiveSheet.Cells(h, 1).Text
                If text11 = text1 Then
                    end1 = h        '****end1 variable gives the row number that has the last occurance of the variable from the raw_data input file*****
                End If
            Next h

            heading1 = ActiveSheet.Cells(g, 1).Text
            '****The variable name***
            Windows(nm_driver).Activate
            ActiveSheet.Range("A19") = heading1

            chart_title = ActiveSheet.Range("A14").Text     '****Chart title is a combination of sheet name and variable name***

            Windows(nm_out).Activate
            Sheets(j).Select
            ActiveSheet.Cells(3, v + 1) = chart_title   '****Pastes the chart_title in the M18 plots sheet
            ActiveSheet.Cells(3, v) = "bin"             '****Pastes the text "bin" in the M18 plots sheet



            Windows(nm_inp1).Activate
            ActiveSheet.Range(Cells(g, 3), Cells(end1, 4)).Select
            Selection.Copy
            Windows(nm_out).Activate
            Sheets(j).Select
            ActiveSheet.Cells(4, v).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

            '****Selects the full table and gives the border****
            ActiveSheet.Range(Cells(3, v), Cells(3, v + 1)).Select
            ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
            With Selection
                    .WrapText = False
            'Call border_design
            End With

“的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** “的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** ' * ** * ** * *** 创建图表 * ** * ** * ** * ** * * * “的 ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** “的 ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ****                 ActiveSheet.Shapes.AddChart.Select                 ActiveChart.ChartType = xlLine                 ActiveChart.Parent.Name =“MyChart”& v                 ActiveChart.Axes(xlCategory)。选择                 Selection.TickLabelPosition = xlLow                 ActiveChart.Axes(xlCategory).TickLabelSpacingIsAuto = True

            'use parent property here
            With ActiveSheet.ChartObjects("MyChart" & v)
                .Height = 260 ' resize
                .Width = 290  ' resize
                .Left = m * 300
                .Top = n * 270 + 600

            End With

            ActiveChart.Legend.Select
            Selection.Delete
            ActiveChart.Axes(xlValue).Select
            ActiveChart.SetElement (msoElementPrimaryValueAxisTitleVertical)
            ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Log Odds"
            ActiveChart.Axes(xlValue).TickLabels.NumberFormat = "#,##0.00"
            ActiveChart.Axes(xlValue).CrossesAt = -100
            ActiveChart.ChartTitle.Text = chart_title
            ActiveChart.ChartTitle.Font.Size = 10
            ActiveChart.ChartTitle.Font.Bold = True

            m = m + 1
            If m = 3 Then
               m = 0
               n = n + 1
            End If
            v = v + 2
            g = end1

         End If

    Next g

    Windows(nm_out).Activate
    ActiveSheet.Range("B3").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown).End(xlToRight)).Select
    Call border_design
    ActiveSheet.Range("B3").Select
    ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True

    ActiveSheet.Range("BA3").Select
    ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True

    ActiveSheet.Range("A1").Select

Next i



Windows(nm_out).Activate
Application.DisplayAlerts = False
Sheets(1).Delete
Sheets(1).Delete
Application.DisplayAlerts = True
Sheets(1).Select
ActiveWorkbook.Save
ActiveWorkbook.Close

Windows(nm_inp1).Activate
ActiveWorkbook.Close

Windows(nm_inp2).Activate
ActiveWorkbook.Close

Windows(nm_driver).Activate
ActiveSheet.Range("A1").Select

MsgBox "Completed"

ans2 = MsgBox("Do you want to OPEN the Output File just created ??", vbYesNo)

If ans2 = vbYes Then
    Workbooks.Open Filename:=mv_file_sv_nm
End If

End Sub

1 个答案:

答案 0 :(得分:0)

这部分代码是用于创建图表的选择

ActiveSheet.Range(Cells(3, v), Cells(3, v + 1)).Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select

现在我们需要找到ActiveSheet。备份几行我们发现:

Sheets(j).Select

什么是Sheet" j"? j由您发布的代码顶部附近的此行定义:

j = j + 1

这告诉我(根据你在这里发布的内容,我能猜到的最好的)是代码循环遍历工作表并创建图表并进行其他格式化。

至于代码拉取数据的工作表中的位置,来自变量v。我必须做一些猜测(1.因为我无法看到你的表单结构,而且2.因为这里有太多的代码需要查看)。 v设置为53,然后添加到代码中的各种循环中。

考虑到你的问题有多广泛,这是我愿意做的最好的事情。这不是一般的最佳论坛"这意味着什么"的问题。

我还会在关于查看图表源数据(在Excel窗口中)的评论中重复这一点,因为这可能有助于破译正在发生的事情。