应用程序/对象定义的错误

时间:2017-01-06 23:06:13

标签: excel-vba vba excel

运行下面的代码时,我得到运行时错误1004。我在下面评论了错误发生的地方。我能够运行一次代码,但第二次运行时会产生错误。我在代码中找不到这些对象未定义的任何地方。

感谢任何帮助。

Option Explicit

Sub CAESARCONVERSION()
Dim InputSheet As Worksheet, SummaryResults As Worksheet, Background As Worksheet
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim y As Integer
Dim h As Integer
Dim v As Integer
Dim c As Integer
Dim z As Integer
Dim myBook As Workbook
Set myBook = Excel.ThisWorkbook
Set InputSheet = myBook.Sheets("Input Sheet")
Set SummaryResults = myBook.Sheets("Summary Results")
Set Background = myBook.Sheets("Background")
Dim NodeList As Integer
Dim TotalCases As Integer
Dim sMyString As String
Dim Nodes As Variant
Dim FindRow As Range
Dim intValueToFind As String
Dim FindRowNumber As Long
Dim SecondRowNumber As Long



'Clear the last run of macro
Background.Range("A2:A1000").Cells.Clear
Background.Range("C2:I10000").Cells.Clear
SummaryResults.Cells.Clear
'Code that will count the total number of load cases

TotalCases = 0
h = 2
Dim text As String
For v = 12 To 100
    If InputSheet.Cells(v, 2).Value <> "" Then
        text = LTrim(InputSheet.Cells(v, 2).Value)
        Background.Cells(h, 3).Value = text
        h = h + 1
        TotalCases = TotalCases + 1
    Else
        GoTo NodeCounter
    End If
Next v

NodeCounter:
y = TotalCases - 1
x = 0
    Dim LoadCaseList() As Variant
    ReDim LoadCaseList(y)

LoadCaseList:
For x = 0 To y
    LoadCaseList(x) = Background.Cells(2 + x, 3).text
Next x


j = 2

For i = 17 + TotalCases To 20000 'Need to define how far for the program to search, we may exceed 20000 at some point
    If InputSheet.Cells(i, 2).Value <> "" Then
    Background.Cells(j, 1).Value = InputSheet.Cells(i, 2).Value
    j = j + 1
    End If
Next i

With Background
NodeList = Background.Cells(2, 2).Value
Background.Range("AA1:XX" & NodeList + 1).Cells.Clear
End With

ReDim Nodes(NodeList - 1)
v = 0
j = 2
For i = 0 To NodeList - 1
    Nodes(i) = Background.Cells(j, 1).Value
    j = j + 1
Next i



Headers:
Dim LoadCaseHeader() As String
Dim TypHeader()
TypHeader = Array("Node", "L", "Direction", "Magnitude")
Dim LoadDirections()
LoadDirections = Array("X", "Y", "Z", "MX", "MY", "MZ")

x = 0
z = 0

For x = 0 To NodeList - 1
    For z = 0 To TotalCases - 1
        SummaryResults.Range(("B" & 2 + (NodeList * 6 + 2) * z) & ":" & "E" & 2 + (NodeList * 6 + 2) * z) = TypHeader()
        SummaryResults.Range("A" & 2 + (NodeList * 6 + 2) * z) = Background.Range("C" & 2 + z)
    Next z
Next x

'Search rows for the first instance of this value.



LoadCases:

'Code that copies information from the InputSheet to the SummaryResults
Dim LoadCases() As Long
FindRowNumber = 0
SecondRowNumber = 0

For c = 0 To y

    ReDim LoadCases(NodeList, 6)
    intValueToFind = LoadCaseList(c)
    For i = 7 To 31 + TotalCases
        With InputSheet
            If Trim(Cells(i, 3).Value) = intValueToFind Then
                Set FindRow = InputSheet.Range("C:C").Find(What:=intValueToFind, LookIn:=xlValues)
                FindRowNumber = FindRow.Row
                GoTo Step1
            End If
        End With
    Next i


Step1:
    With InputSheet
        For i = 0 To NodeList - 1
        x = 4
            For j = 0 To 5
'THE LINE BELOW IS WHERE THE ERROR OCCURS
                LoadCases(i, j) = InputSheet.Cells(FindRowNumber + (TotalCases + 3) * i, x)                    
                x = x + 1
            Next j
        Next i
    End With



    Background.Range("AC2:AH" & NodeList + 1).Offset(0, c * 7) = LoadCases

    For i = 1 To NodeList * 6 * TotalCases
        With SummaryResults
            If Trim(Cells(i, 1).Value) = intValueToFind Then
                Set FindRow = SummaryResults.Range("A:A").Find(What:=intValueToFind, LookIn:=xlValues)
                SecondRowNumber = FindRow.Row
                GoTo Step2
            End If
        End With
    Next i
Step2:
    With SummaryResults
        For x = 0 To NodeList - 1
            For j = 0 To 5
                SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 5) =     Background.Cells(x + 2, 29 + j)
                SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 3) = TypHeader(1)
                SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 4) = LoadDirections(j)
                SummaryResults.Cells(SecondRowNumber + 1 + j + 6 * x, 2) = Nodes(x)
            Next j
        Next x
    End With

Next c





End Sub

0 个答案:

没有答案