Range.Find(String)不起作用

时间:2018-05-29 05:38:28

标签: excel-vba vba excel

我有以下电子表格: [A-Q栏中的数据和近3000行] [1]

我在另一个页面上有一个下拉列表,其中列有名称/模式/班次的单元格,并为每个页面创建一个变量。出于某种原因,当我在下拉列表中更改字段时,出现“91”对象错误。当我使用下拉列表中位于第一个位置的所有项目的组合时,宏工作正常。当我更改DC / Mode / Shift时,问题始终存在。 DC是字符串,Mode是String,Shift是Integer。

每个Dim Search / Dim FindRow都是它自己的passthrough功能,​​但它结合了一切。任何帮助将不胜感激!!!

以下是我的代码:

Sub DailyRouteInput_Button8_Click()
    Dim DC As String

    Worksheets("Daily Route Input").Activate

    Range("U1").Select
    DC = ActiveCell.Value

    ActiveCell.Offset(0, 1).Select
    Mode = ActiveCell.Value

    Range("C5").Select
    Shift = ActiveCell.Value

    Worksheets("Daily Route Master Data").Activate

    (Was new passthrough function)

    Dim SearchRange As Range
    Dim FindRow As Range
    Set SearchRange = Range("A2", Range("A2").End(xlUp))
    Set FindRow = SearchRange.Find(What:=DC, LookIn:=xlValues, lookat:=xlWhole)

    DC = FindRow.Row '---- Here is where the problem is ---------

    Range("A" & DC).Offset(0, 1).Select

    (Was new passthrough function)

    Dim newSearchRange As Range
    Dim newFindRow As Range
    Set newSearchRange = Range("B" & DC, Range("B" & DC).End(xlUp))
    Set newFindRow = newSearchRange.Find(Mode, LookIn:=xlValues, lookat:=xlWhole)

    Mode = newFindRow.Row '---- Here is where the problem is ---------

    Range("B" & Mode).Offset(0, 1).Select

    (Was new passthrough function)

    Dim finalNewSearchRange As Range
    Dim finalNewFindRow As Range
    Set finalNewSearchRange = Range("C" & Mode, Range("C" & Mode).End(xlUp))
    Set finalNewFindRow = finalNewSearchRange.Find(Shift, LookIn:=xlValues, lookat:=xlWhole)

    Shift = finalNewFindRow.Row '---- Here is where the problem is ---------

    Range("C" & Mode).Offset(0, 1).Select

    WeekCheck = ActiveCell.Value
    ActiveCell.Offset(0, 1).Select
    MonthCheck = ActiveCell.Value

1 个答案:

答案 0 :(得分:0)

谢谢大家试图给我一些提示。我弄清楚了。 XlUp是罪魁祸首,我将其改为xlDown。由于这个原因,我正在返回一个空对象,因为我正在测试列中没有任何内容的数据。然后我听了你们其余的人并设置了对象变量,并且能够消除.copy并完成我最初设置的操作,这将返回结果的整体速度从13秒增加到1秒多一点。你可以在下面找到我的代码。我知道我没有把一些变量称为字符串或整数,但除此之外,如果你能给我任何进一步的建设性批评,我一定会感激不尽!



Sub DailyRouteInput_Button8_Click()

Dim nResult As Long
 nResult = MsgBox( _
 Prompt:="Did you save your prior DC/Mode updates to Master?", _
 Buttons:=vbYesNo)
 If nResult = vbNo Then
 Exit Sub
 End If

Dim VL As Workbook
Dim DailyRouteInput As Worksheet
Dim DailyRouteMaster As Worksheet
Dim masterRange As Range
Dim inputRange As Range
Dim DCInput As String
Dim ModeInput As String
Dim SearchRange As Range
Dim FindRow As Range
Dim finalNewSearchRange As Range
Dim finalNewFindRow As Range
Dim newSearchRange As Range
Dim newFindRow As Range


Set VL = ThisWorkbook
Set DailyRouteInput = VL.Sheets("Daily Route Input")
Set DailyRouteMaster = VL.Sheets("Daily Route Master Data")
Set masterRange = DailyRouteMaster.Range("A1:N2545")
Set inputRange = DailyRouteInput.Range("A1:BH57")


DCInput = inputRange.Cells(1, 21)
ModeInput = inputRange.Cells(1, 22)
ShiftInput = inputRange.Cells(5, 3)

DailyRouteMaster.Activate

Set SearchRange = masterRange.Range("A1", Range("A1").End(xlDown))
Set FindRow = SearchRange.Find(DCInput, LookIn:=xlValues, lookat:=xlWhole)
DC = FindRow.Row

Set newSearchRange = Range("B" & DC - 1, Range("B" & DC - 1).End(xlDown))
Set newFindRow = newSearchRange.Find(ModeInput, LookIn:=xlValues, lookat:=xlWhole)
Mode = newFindRow.Row

Set finalNewSearchRange = Range("C" & Mode - 1, Range("C" & Mode - 1).End(xlDown))
Set finalNewFindRow = finalNewSearchRange.Find(ShiftInput, LookIn:=xlValues, lookat:=xlWhole)
Shift = finalNewFindRow.Row

MonthCheck = DailyRouteMaster.Range("E" & DC)

If (MonthCheck = "January") Then
        
        DailyRouteMaster.Range("F" & Shift, "N" & Shift + 3).Copy
        DailyRouteInput.Activate
        inputRange.Cells(5, 26).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 4, "N" & Shift + 7).Copy
        DailyRouteInput.Activate
        inputRange.Cells(5, 39).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 8, "N" & Shift + 12).Copy
        DailyRouteInput.Activate
        inputRange.Cells(5, 52).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        DailyRouteMaster.Range("F" & Shift + 13, "N" & Shift + 16).Copy
        DailyRouteInput.Activate
        inputRange.Cells(14, 26).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 17, "N" & Shift + 20).Copy
        DailyRouteInput.Activate
        inputRange.Cells(14, 39).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 21, "N" & Shift + 25).Copy
        DailyRouteInput.Activate
        inputRange.Cells(14, 52).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        
        DailyRouteMaster.Range("F" & Shift + 26, "N" & Shift + 29).Copy
        DailyRouteInput.Activate
        inputRange.Cells(23, 26).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 30, "N" & Shift + 33).Copy
        DailyRouteInput.Activate
        inputRange.Cells(23, 39).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 34, "N" & Shift + 38).Copy
        DailyRouteInput.Activate
        inputRange.Cells(23, 52).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        
        DailyRouteMaster.Range("F" & Shift + 39, "N" & Shift + 42).Copy
        DailyRouteInput.Activate
        inputRange.Cells(32, 26).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 43, "N" & Shift + 46).Copy
        DailyRouteInput.Activate
        inputRange.Cells(32, 39).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteMaster.Activate
        DailyRouteMaster.Range("F" & Shift + 47, "N" & Shift + 52).Copy
        DailyRouteInput.Activate
        inputRange.Cells(32, 52).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        DailyRouteInput.Activate

    Else
        MsgBox ("Something is Wrong!")
    End If

End Sub