0

出于某种原因,我的代码可以编译,但它不会在图表生成的工作表 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
4

1 回答 1

1

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

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

您将运行到代码部分:

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

这是创建一个新的工作表

此工作表成为ActiveSheet

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

P Range 将引用新工作表的范围 P,它是空的 --> 没有结果

如果您的代码在工作表模块中

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

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

代替Range("P1:P5")

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

于 2013-02-01T04:02:48.537 回答