在范围上获取错误消息

时间:2013-12-21 01:01:26

标签: excel-vba inventory invoice vba excel

我收到的错误信息表明该变量未定义。 VB正在将此代码着色为红色如果LCase(wb.Range(“Q”& i)=“y”那么.AutoFilter并且我不知道为什么。

非常重要的是,只粘贴每个范围中Q列中带有“y”的行,而不是其他所有行。

我必须将i更改为2到500,并且j = 2到20,但我担心我可能会获得不希望粘贴到Sheet2(Materials_Estimate)的列。我只想粘贴范围列。

范围包括Sheet2信息,如下图所示(B =文本,c =文本,D =文本,F =最多3个数字,G =字母y,H =文本,I =从中复制的计算第1页的数量*成本)

任何人都可以帮助我吗?

[代码]

选项明确

Sub Estimating2()     Application.ScreenUpdating = False

'naming the workbook and worksheets and ranges
Dim ProjectBudgeting1 As Workbook
Dim Materials_Budget As Worksheet
Dim Materials_Estimate As Worksheet
Dim LowesFax As Worksheet
Dim HomeDepotFax As Worksheet
Dim SBath1 As Range
Dim SBath2 As Range
Dim SBed1 As Range
Dim SBed2 As Range
Dim SBed3 As Range
Dim SBed4 As Range
Dim SHall As Range
Dim SFP As Range
Dim SRP As Range
Dim SKit As Range
Dim SGar As Range
Dim BuyOA As Range
Dim SFlorida As Range
Dim TargetRange As Range
Dim ActiveWorksheet As Worksheet


'naming the worksheets and ranges in code
Set ProjectBudgeting1 = ActiveWorkbook
Set Materials_Budget = Worksheets("Materials_Budget")
Set Materials_Estimate = Worksheets("Materials_Estimate")
Set LowesFax = Worksheets("LowesFax")
Set HomeDepotFax = Worksheets("HomeDepotFax")
Set SBath1 = Range("Materials_Budget!Supplies_Bathroom1")
Set SBath2 = Range("Materials_Budget!Supplies_Bathroom2")
Set SBed1 = Range("Materials_Budget!Supplies_Bedroom1")
Set SBed2 = Range("Materials_Budget!Supplies_Bedroom2")
Set SBed3 = Range("Materials_Budget!Supplies_Bedroom3")
Set SBed4 = Range("Materials_Budget!Supplies_Bedroom4")
Set SHall = Range("Materials_Budget!Supplies_Hallway")
Set SFP = Range("Materials_Budget!Supplies_FrontPorch")
Set SRP = Range("Materials_Budget!Supplies_RearPorch")
Set SKit = Range("Materials_Budget!Supplies_Kitchen")
Set SGar = Range("Materials_Budget!Supplies_Garage")
Set SFlorida = Range("Materials_Budget!Supplies_Florida")
 'Here I'm calling out the column q and looking for a "Y"
Set BuyOA = Range("Materials_Budget!Buy_OrderApproval")
'Here I'm naming the source of the information that gets copied into other sheets
Set ActiveWorksheet = Materials_Budget
'Here is the sheet where the source cells are pasted
Set TargetRange = Range("Materials_Estimate!EstimateTableArea1")

'Looking for the "Y" in column q for duplicating and printing corresponding rows (i) and columns (j)
For i = 12 To 520
        Cells("Q", i) = "Row " & i & " Col " & j
For j = 2 To 20
If LCase(wb.Range("Q" & i) = "y" Then .AutoFilter
i = i + 1
    Range("Q" & i).Select
    i = i - 1
Next q
Next i

    For j = 1 To 5
        Cells(i, j) = "Row " & i & "   Col " & j

End Sub

Application.ScreenUpdating = True

End With

End Sub

[代码/]

1 个答案:

答案 0 :(得分:1)

我看到很多错误。

A)您尚未声明对象。例如,您需要将SBath1, SBath2 etc..声明为Range

B)您已将ProjectBudgeting1声明为工作簿,但之后您将其用作工作表对象。

C)设置范围时,完全符合条件

D)您的wb对象未获声明。我强烈建议您在代码顶部使用Option Explicit

E) )

中有一个额外的括号wb.Range("Q12:Q" & LastRow))

F)避免使用.Select INTERESTING READ

G)最后,我强烈建议忘记vba中的一个单词并使用End来停止代码。理由很简单。这就像使用POWER OFF按钮切换计算机一样。 End语句突然停止代码执行。其他程序保留的对象引用(如果有)也是无效的。

以下是您的代码应如何显示的基本要点

Sub Estimating2()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim rng1 As Range, rng2 As Range

    Set wb = ActiveWorkbook '~~> Or ThisWorkbook?

    Set ws = wb.Sheets("Sheet1")

    With ws
        Set rng1 = .Range("Supplies_Bathroom1")
        Set rng2 = .Range("Supplies_Bathroom2")

        '
        '~~> And so on
        '

    End With
End Sub