(VBA Excel)尝试使用宏从工作表中收集数据,错误91

时间:2015-06-18 16:11:43

标签: excel vba excel-vba

所以我有一张包含两张纸的工作簿,第一张包含一个人的名字和姓,第二张包含用户名(第一个名字和姓氏的第一个首字母,例如:John史密斯 - > jsmith)。 第二个工作表在每个用户名右侧的5列中包含信息,我需要收集该信息并将其与相应的名称放在第一个工作表上。 现在,运行它会导致:

Run-time error '91':
Object variable or With block variable not set

我已经获得了代码的主体,当它不在while循环中时运行。所以我知道代码在输入单个单元格时有效。但是,我对变量单元的实现不会对它表现出色。 这是代码:

Sub Macro()

' set up counter

    Dim rownum As Long
    rownum = 1

    Do While rownum < 273
        Dim cellA As String
        Dim cellB As String
        Dim cellC As String

' change cells depending on current rownum
        cellA = "A" & CStr(rownum)
        cellB = "B" & CStr(rownum)
        cellC = "C" & CStr(rownum)

        Dim rngA As String
        Dim rngB As String
        Dim rngAB As String

' select sheet and collect first initial of first name and full last name
        Sheets("Sheet1").Select
        first = Left(Range(cellA).Value, 1)
        last = Range(cellB).Value
        searchname = first & last

' select sheet with info and find the row with username
        Sheets("Sheet2").Select

' ***this is where the issue is***
        Cells.Find(What:=searchname, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
' ***this is where the issue is***

' copy the info and paste it into the first sheet
        Application.CutCopyMode = False
        ActiveCell.Offset(, 1).Resize(1, 5).Copy
        Sheets("Sheet1").Select
        Range(cellC).Select
        ActiveSheet.Paste

        rownum = rownum + 1

    Loop

End Sub

如果有人能告诉我自己做错了什么或知道更清洁的做法,那就太棒了。

2 个答案:

答案 0 :(得分:0)

您通常不需要使用复制和粘贴,只需直接设置值即可。您还可以使用工作表的.Cells属性,以避免创建单元格字符串。

Sub test()
    Dim i as Integer
    Dim rownum As Long
    Dim rng As Range
    rownum = 1

    Do While rownum < 273
'collect first initial of first name and full last name
        first = Left(Sheets("Sheet1").Cells(rownum, 1).Value, 1)
        last = Sheets("Sheet1").Cells(rownum, 2)
        searchname = first & last

'copy values to first sheet
        Set rng = Sheets("Sheet2").Cells.Find(What:=searchname, MatchCase:=False)
        For i = 1 To 5 Step 1
            Sheets("Sheet1").Cells(rownum, i + 2).Value = rng.Offset(, i).Value
        Next i

        rownum = rownum + 1
    Loop
End Sub

答案 1 :(得分:0)

问题在于您尝试使用.Find()的返回值,而不检查您是否确实在此行中找到了任何内容:

    Cells.Find(What:=searchname, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

如果.Find()没有返回任何内容,则无法激活任何内容 - 因此错误91.

尝试将返回保存到变量,检查它是否为空,然后根据您是否找到它做任何您需要做的事情:

Dim result As Range
Set result = Cells.Find(What:=searchname, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False)

If Not result Is Nothing Then
    result.Activate
    'Found code
Else
    'Not found code.
End If

也就是说,您通常应该避免使用.Activate.Select - 而是直接使用引用并使用对象而不是使用全局Active对象。