“无效限定符”VBA IF THEN 多个条件嵌套循环[重复]

问题描述 投票:0回答:1

这可能超出了我目前的技能范围,但我正在尝试创建一个引用 2 个活动工作表的嵌套循环。如果sheet2满足sheet1中的多个条件,它会从一张sheet1中获取2个特定值。

这些工作表是 ProjSheet(项目工作表)和 PlateSheet(项目内的板)。我需要项目信息(特别是批次和农场)从 ProjSheet 拉入 PlateSheet,如果项目代码匹配并且板代码是 <= the first plate# in ProjSheet AND >= ProjSheet 中的最后一个板代码。应该只是逐行 IF AND AND THEN 嵌套循环。

再说一遍,我可能还没有足够的知识或技能来意识到我做错了什么或如何正确地构建它。

Sub ProjectPlateLoops()

'define workbooks and worksheets
Dim wb As Workbook
Dim ws As Worksheet
Dim PlateSheet As Worksheet
Dim ProjSheet As Worksheet

' HOPE THIS ACTIVATES EACH SHEET AS IT IS USED IN LOOP?
Set ws = ActiveSheet
PlateSheet = ws("Plate_Analysis")
ProjSheet = ws("Project_Alalysis")

'last row in Project Analysis Page
Dim lrProject As Long
lrProj = ProjSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'law row in Plate Analysis Page
Dim lrPlate As Long
lrPlate = PlateSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

' Create Loop; if 3 conditions are met, print Batch and Farm from Project sheet to Plate sheet
Dim i, j As Long
For i = 3 To lrProj
Dim FarmProj, BatchProj, ProjCodeProj As Object
Dim FirstPlateProj, LastPlateProj As Integer
FarmProj = ProjSheet.Range("D3" & i)
BatchProj = ProjSheet.Range("E3" & i)
ProjCodeProj = ProjSheet.Range("F3" & i)
FirstPlateProj = ProjSheet.Range("J3" & i)
LastPlateProj = ProjSheet.Range("L3" & i)
  For j = 3 To lrPlate
  Dim FarmPlate, BatchPlate, ProjCodePlate As Object
  Dim PlateNumPlate As Integer
  FarmPlate = PlateSheet.Range("H3" & j)
  BatchPlate = PlateSheet.Range("G3" & j)
  ProjCodePlate = PlateSheet.Range("F3" & j)
  PlateNumPlate = PlateSheet.Range("E3" & j)
    If ProjCodeProj.Value = ProjCodePlate.Value And _
      PlateNumPlate.Value >= FirstPlateProj.Value And _
      PlateNumPlate.Value <= LastPlateProj.Value Then
        BatchPlate.Cells.Value = BatchProj.Cells.Value
        FarmPlate.Cells.Value = BatchProj.Cells.Value
    End If
  Debug.Print (i)
  Debug.Print (j)
  End If
  Next j
Next i

End Sub

我遇到的问题是第二个标准中的对象:

      PlateNumPlate.Value >= FirstPlateProj.Value And _

PlateNumPlate 给我一个“无效限定符”错误,我不确定在这种情况下为什么或该怎么办。将对象类型更改为“对象”而不是“字符串”解决了另一个问题,但却产生了这个问题。 我将非常感谢任何帮助!

excel vba if-statement nested-loops
1个回答
0
投票

我会跳过大部分范围类型变量,直接使用单元格:

Sub ProjectPlateLoops()

    'define workbooks and worksheets
    Dim wb As Workbook, ws As Worksheet, PlateSheet As Worksheet, ProjSheet As Worksheet
    Dim pltNum As Long, lrProj As Long, lrPlate As Long, i As Long, j As Long
    Dim proj
    
    Set wb = ThisWorkbook 'or ActiveWorkbook?
    Set ws = ActiveSheet
    Set PlateSheet = wb.Worksheets("Plate_Analysis")
    Set ProjSheet = wb.Worksheets("Project_Alalysis")
    
    'last rows in Project Analysis/Plate sheets (call separate function for this)
    lrProj = LastOccupiedRow(ProjSheet)
    lrPlate = LastOccupiedRow(PlateSheet)
    
    ' Create Loop; if 3 conditions are met, print Batch and Farm from Project sheet to Plate sheet
    For i = 3 To lrProj
        proj = ProjSheet.Cells(i, "D").Value
        For j = 3 To lrPlate
            If PlateSheet.Cells(j, "F").Value = proj Then
                pltNum = PlateSheet.Cells(j, "E").Value
                If pltNum >= ProjSheet.Cells(i, "J").Value And pltNum <= ProjSheet.Cells(i, "L").Value Then
                    PlateSheet.Cells(j, "G").Value = ProjSheet.Cells(i, "E").Value 'batch
                    PlateSheet.Cells(j, "H").Value = ProjSheet.Cells(i, "E").Value 'E again?
                End If
           End If
        Next j
    Next i

End Sub

Function LastOccupiedRow(ws As Worksheet) As Long
    Dim f As Range
    Set f = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    If Not f Is Nothing Then LastOccupiedRow = f.Row
End Function
© www.soinside.com 2019 - 2024. All rights reserved.