查找方法对象变量未设置

时间:2017-01-06 19:32:39

标签: excel vba excel-vba

这是一个错误,我一直试图弄清楚,我的发现方法没有产生任何结果,我无法弄清楚原因。

代码假设在InputSheet中搜索字符串,报告行号并根据该行号开始将信息移动到Background。然后下一个.find将在SummaryResults中找到该字符串并开始从Background移动信息,重新格式化它,然后粘贴到SummaryResults。

我的find方法没有产生任何结果,并且即使字符串存在于工作表和正确的范围内,也会使FindRow = Nothing。

在运行宏打开另一个Excel工作表后,这个错误开始发生,所以可能ActiveWorkbook不正确,但我从未能让它运行起来。

显示的一些变量来自代码的其他部分,但是当我在调试模式中将鼠标悬停在它们上面时,它们显示了他们想要的内容。

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
ReDim LoadCases(NodeList, 6)
FindRowNumber = 0
SecondRowNumber = 0

For c = 0 To y

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

    'MsgBox FindRowNumber

    With InputSheet
        For i = 0 To NodeList - 1
        x = 4
            For j = 0 To 5
                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, 5).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

任何帮助将不胜感激。编辑:上传整个代码。附加信息,代码在未标记为Excel时会起作用,但在再次运行标签时会失败。

1 个答案:

答案 0 :(得分:0)

问题似乎是LoadCaseList()数组永远不会被填充。这是你的Find语句:

Set FindRow = InputSheet.Range("C:C").Find(What:=intValueToFind, LookIn:=xlValues)

intValueToFind由此语句设置:

intValueToFind = LoadCaseList(c)

但LoadCaseList()数组由以下代码填充,该代码是GoTo语句永远不会调用的标签(据我所知):

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

因为GoCo语句从不调用LoadCaseList标签语句,所以永远不会填充LoadCaseList()数组,因此intValueToFind没有值,因此Find方法没有要搜索的值(可能是空字符串除外) )。