将数组值复制到图表生成的inActive工作表中的范围

时间:2013-02-01 03:19:57

标签: arrays excel-vba charts range vba

由于某些原因我的代码编译,但它不会在图表生成的工作表ArrayData中插入任何内容....我说的这一部分是在代码的末尾用For / If循环...

由于有许多值,而且excel的系列输入的字符数限制为255,我将这些值放在未使用的工作表中......但是当代码写入时它们没有显示...

   Private Sub cmdGraphs_Click()

    '*************************************************************************************'
    'The Code below is only for the Graphing Parameter inputs                             '
    '*************************************************************************************'
    Static Counter As Integer
    'Variable to keep track of the amount of graphs generated


    Dim graphName As String
    graphName = form2.textName.Text

    Dim slot As Integer, sheet As String
    'Verify that an item was selected
    If listSlot.ListIndex = -1 Then
    'If ListIndex is -1, nothing selected
        MsgBox "Select a slot number!"
    Else
             'If ListIndex not -1 inform user what was selected
        MsgBox "You selected: " & listSlot.Value
        'As a reference to the selected value
        slot = listSlot.Value
    End If

    If listSheet.ListIndex = -1 Then
        MsgBox "Select a Sheet"
    Else
        MsgBox "You selected: " & listSheet.Value
        sheet = listSheet.Value
    End If

    '*************************************************************************************'
    'The Code below is for setting up the table where the first graphs will be            '
    '*************************************************************************************'

    'Selects sheet for graphic puposes
    Sheets("Sheet2").Select
    Sheets("Sheet2").Cells(2, 1).Select


    'Begin Plotting Table Data
    Dim rang As Range
    Set rang = Range("A3")


    If Counter <> 0 Then
        rang.Offset(Counter, 0) = graphName
        rang.Offset(Counter, 1).Value = sheet
        rang.Offset(Counter, 2).Value = slot
        rang.Offset(Counter, 3).Value = "Formula to be written"
        rang.Offset(Counter, 4).Value = "Formula to be written"
        rang.Offset(Counter, 5).Value = "Formula to be written"

    Else

        'Table Setup
        Sheets("Sheet2").Cells(1, 1).Value = "Current Sheet: " & ActiveSheet.Name
        Sheets("Sheet2").Range("A1:F2").Interior.ColorIndex = 15
        Sheets("Sheet2").Cells(1, 1).Font.Name = "Lucida Calligraphy"
        Sheets("Sheet2").Cells(1, 1).Font.Size = 16
        Sheets("Sheet2").Range("A1:F2").Font.Italic = True
        Sheets("Sheet2").Cells(2, 1).Value = "Graph Name"
        Sheets("Sheet2").Cells(2, 2).Value = "Data Sheet: " & sheet
        Sheets("Sheet2").Cells(2, 3).Value = "Slot No. "
        Sheets("Sheet2").Cells(2, 4).Value = "TW AVG "
        Sheets("Sheet2").Cells(2, 5).Value = "Sigma %"
        Sheets("Sheet2").Cells(2, 6).Value = "Angle"
        Sheets("Sheet2").Columns("A:G").AutoFit


        rang.Value = graphName
        rang.Offset(0, 1).Value = sheet
        rang.Offset(0, 2).Value = slot
        rang.Offset(0, 3).Value = "Formula to be written"
        rang.Offset(0, 4).Value = "Formula to be written"
        rang.Offset(0, 5).Value = "Formula to be written"

        'Adds a sheet for Array sorting due to 255 character limit
        Worksheets.Add(After:=Worksheets(1)).Name = "Array Data" & Counter
    End If

    Sheets("Sheet2").Columns("A:G").AutoFit


    '*************************************************************************************'
    'Search Algorithm for user defined Slot Number                                        '
    '*************************************************************************************'
    Dim slotRang As Range, slotArr() As Variant, i As Long
    Set slotRang = Range("P2", Range("P2").End(xlDown))
    slotArr = slotRang.Value


'This section is to  to search for the value the user selected which is slot and only 
'store the rows in this case into arrays and then to be parsed into another worksheet for 
'another code to be added to generate a graph

    For i = 2 To UBound(slotArr, 1)
        If slotArr(i, 1) = slot Then
            MsgBox ("In the Array Loop")
            Dim xRang As Range, xArr2() As Variant
            Set xRang = slotRang.Offset(0, 4)
            xArr2 = xRang.Value
            Dim arrDatax As Range
            Dim arrDatay As Range
            Set arrDatax = Worksheets("ArrayData" & Count).Range("A1", Range("A1").End(xlDown))
            arrDatax.Value = Application.Transpose(arrDatax(i, 1))
            Dim yRang As Range, yArr() As Variant
            Set yRang = slotRang.Offset(0, 5)
            yArr = yRang.Value
            Set arrDatay = Worksheets("ArrayData" & Count).Range("B1", Range("B1").End(xlDown))
            arrDatay.Value = Application.Transpose(arrDatay(i, 1))
        End If
    Next






    textName.Value = ""
    Counter = Counter + 1




    End Sub

1 个答案:

答案 0 :(得分:1)

假设您的代码放在ThisWorkBook模块或模块中,而不是表单模块

您发布的代码的问题: 案例Counter = 0

您将运行代码部分:

Worksheets.Add(After:=Worksheets(1)).Name = "Array Data" & Counter

正在创建新工作表

此工作表变为ActiveSheet

所以,你排队: Set slotRang = Range("P2", Range("P2").End(xlDown))

P范围将指新纸张的范围P,其为空 - &gt;没有结果

如果您的代码位于工作表模块

您应该始终在范围前添加Me以使其清晰

该范围属于哪个工作表。 例如。 Me.Range("P1:P5")

而不是Range("P1:P5")

您应该使用调试模式来确定问题所在的行,并突出显示该行代码。