【问题标题】:Getting Error messages on ranges获取范围内的错误消息
【发布时间】:2013-12-21 01:01:25
【问题描述】:

我收到错误消息,指出该变量未定义。 VB 将这段代码染成红色 If LCase(wb.Range("Q" & i) = "y" Then .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

结束子

Application.ScreenUpdating = True

End With

结束子

[代码/]

【问题讨论】:

  • Set SBath1 = Range("Materials_Budget!Supplies_Bathroom1") 更改为Set SBath1 = Range("Supplies_Bathroom1"),其他人也一样。更不用说你将这些对象调暗为Range

标签: excel inventory invoice vba


【解决方案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

【讨论】:

  • 当系统查看代码时,我仍然收到错误消息。
  • 我在上面使用了您的代码,但使用时遇到了运行时错误 9: Set ws = ProjectBudgeting1.Sheets("Materials_Budget,Materials_Estimate,LowesFax,HomeDepotFax") 或者我在使用时收到运行时错误 424你的陈述是 Set ws = wb.Sheets("Sheets1") 你能帮忙吗?
  • 1 ws 是一个工作表对象。您不能在其中存储多张纸。 2关于"Set ws = wb.Sheets("Sheets1")",你是怎么定义wswb的?
  • 见上文,我今天早些时候编辑了我的代码。 wb 设置为工作簿 Dim ProjectBudgeting1 As Workbook , Set ProjectBudgeting1 = ActiveWorkbook , ws 在上面单独列出 Dim Materials_Budget As Worksheet , Set Materials_Budget = Worksheets("Materials_Budget") Set Materials_Estimate = Worksheets("Materials_Estimate") Set LowesFax = Worksheets("LowesFax") Set HomeDepotFax = Worksheets("HomeDepotFax") 将 Materials_Estimate 作为工作表将 LowesFax 作为工作表将 HomeDepotFax 作为工作表进行调暗
猜你喜欢
  • 1970-01-01
  • 2014-05-28
  • 2019-12-14
  • 2013-08-29
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多