Do循环Excel中的应用程序定义或对象定义错误

时间:2015-07-03 13:17:36

标签: excel vba excel-vba excel-2010

我在Excel 2010中有一个sub,它在运行时标记了应用程序定义或对象定义的错误。子应该填充一张表格,其中包含从Access 2010中提取的数据。这最初标记了Row_Rehab = Row_Rehab + 1处的溢出错误。我将Row_Rehab从整数更改为长整数。现在我收到了这个应用程序定义的错误。

错误发生在第3行(第一个If Worksheets...

Do
   Row_Rehab = Row_Rehab + 1
   If Worksheets("Rehab").Cells(Row_Rehab, 1) = Worksheets("Raw Export").Cells(Row_Export, 1) And Worksheets("Rehab").Cells(Row_Rehab, 2) = Worksheets("Raw Export").Cells(Row_Export, 2) Then
      Do
        Row_Rehab = Row_Rehab + 1
          If Worksheets("Rehab").Cells(Row_Rehab, 1) = "" And Worksheets("Rehab").Cells(Row_Rehab, 2) = "" And Worksheets("Rehab").Cells(Row_Rehab, Rehab_GWP_Col) = "" Then
          ProjLine.Copy Destination:=Worksheets("Rehab").Range(Worksheets("Rehab").Cells(Row_Rehab, 1), Worksheets("Rehab").Cells(Row_Rehab, numCol))
          Exit Do
          End If
     Loop
     Exit Do
     End If
Loop

我该怎么做才能解决此错误?

编辑:这是此过程的完整代码。

Sub Populate_Rehab()

Dim Row_Export As Long
Dim Row_Rehab As Long
Dim Funding As String
Dim PROJ As String
Dim WorkType As String
Dim Dom As Integer    
Dim Rehab_PROJ_Col As Integer
Dim Export_PROJ_Col As Integer
Dim Rehab_WorkType_Col As Integer
Dim PROJ_Row As Integer    
Dim flag As Boolean    
Dim Export_SF_Col As Integer    
Dim Export_Column As Integer
Dim RawExport_Col(255) As Integer    
Dim numRecords As Long
Dim numCol As Integer
Dim line As Range
Dim PROJLine As Range
Dim WPLine As Range    
Dim numSF As Integer
Dim SF As Integer
Dim col As Integer
Dim startCol, startCol2 As Integer
Dim SFflag As Boolean
Dim SF_Row As Integer
Dim SF_type As String
Dim i, j As Integer 'used in for loop
Dim SFInfo As Range    
Dim numP As Integer
Dim strSearch As String
Dim aCell As Range
Dim startColExp As Integer     
Dim colPriority As Integer
Dim colEngComment As Integer
Dim colSubmissionCRO As Integer
Dim colActualSubmissionCRO As Integer    
Dim colEmpty As Integer
Dim colEmptyS As String
Dim colEmptyE As String    
Dim statusP As Integer    
Dim wks As Integer

RawExport_Col(0) = 0
RawExport_Col(1) = 0
RawExport_Col(2) = 0
RawExport_Col(3) = 0
RawExport_Col(4) = 5
RawExport_Col(5) = 1
RawExport_Col(6) = 2
RawExport_Col(7) = 3
RawExport_Col(8) = 4
RawExport_Col(9) = 6
RawExport_Col(10) = 7
RawExport_Col(11) = 7
RawExport_Col(12) = 8
RawExport_Col(13) = 10
RawExport_Col(14) = 11
RawExport_Col(15) = 12
RawExport_Col(16) = 13
RawExport_Col(17) = 13
RawExport_Col(18) = 17
RawExport_Col(19) = 17
RawExport_Col(20) = 18
RawExport_Col(21) = 19
RawExport_Col(22) = 20
RawExport_Col(23) = 21
RawExport_Col(24) = 22
RawExport_Col(25) = 23
RawExport_Col(26) = 26
RawExport_Col(27) = 27
RawExport_Col(28) = 28
RawExport_Col(29) = 29
RawExport_Col(30) = 30
RawExport_Col(31) = 31
RawExport_Col(32) = 32
RawExport_Col(33) = 33
RawExport_Col(34) = 0
RawExport_Col(35) = 31
RawExport_Col(36) = 34
RawExport_Col(37) = 35
RawExport_Col(38) = 36
RawExport_Col(39) = 37
RawExport_Col(40) = 38
RawExport_Col(41) = 39
RawExport_Col(42) = 40
RawExport_Col(43) = 14
RawExport_Col(44) = 0
RawExport_Col(45) = 0
RawExport_Col(46) = 0
RawExport_Col(47) = 0
RawExport_Col(48) = 0
RawExport_Col(49) = 0
RawExport_Col(50) = 0
RawExport_Col(51) = 0
RawExport_Col(52) = 0
RawExport_Col(53) = 0
RawExport_Col(54) = 0
RawExport_Col(55) = 0
RawExport_Col(56) = 0

colPriority = 9
colEngComment = 15
colSubmissionCRO = 24
colActualSubmissionCRO = 25

Export_SF_Col = 34

Rehab_PROJ_Col = 7
Export_PROJ_Col = 10
Rehab_WorkType_Col = 22

'search the first added extra column

strSearch = "SOPri"
Set aCell = Worksheets("Raw Export").Rows(1).Find(What:=strSearch, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
    numP = aCell.Column
End If          

'Get the funding types/total number of funding types
With Worksheets("Template")
    numSF = .Range(.Cells(12, 1), .Cells(12, 1).End(xlToRight)).Count - 2
End With

'Count number of records from dataset
With Worksheets("Raw Export")
    numRecords = .Range(.Cells(2, 3), .Cells(2, 3).End(xlDown)).Count
End With

'define expansion column
startColExp = numP + 17
RawExport_Col(startColExp) = numSF + 41
RawExport_Col(startColExp + 1) = numSF + 42
RawExport_Col(startColExp + 2) = numSF + 43
RawExport_Col(startColExp + 3) = numSF + 44
RawExport_Col(startColExp + 4) = numSF + 45
RawExport_Col(startColExp + 5) = numSF + 46
RawExport_Col(startColExp + 6) = numSF + 47
RawExport_Col(startColExp + 7) = numSF + 48

'add allocation fomula if its expansion
If Worksheets("Parameters").Cells(18, 14) = "Expansion" Then
    With Worksheets(2)
        For j = 0 To 7
            .Cells(globalAllocation, 41 + numSF + j).FormulaR1C1 = "=SUM(R[-" + CStr(globalAllocation - 3) + "]C:R[-1]C)"
            .Cells(globalAllocation + 2, 41 + numSF + j).FormulaR1C1 = "=SUM(R[3]C:R[65523]C)"
            .Cells(globalAllocation + 3, 41 + numSF + j).FormulaR1C1 = "=R[-3]C-R[-1]C"
            .Range(.Cells(globalAllocation, 41 + numSF), .Cells(globalAllocation + 3, 41 + numSF + 7)).NumberFormat = "$#,##0"
            Worksheets("Template").Range("BD2:BK2").Copy Destination:=.Range(.Cells(2, 41 + numSF), .Cells(2, 41 + numSF + 7))
            .Range(.Cells(2, 41 + numSF), .Cells(2, 41 + numSF + 7)).ColumnWidth = 13.71
            .Range(.Cells(2, 41 + numSF), .Cells(2, 41 + numSF + 7)).NumberFormat = "$#,##0"
        Next
    End With
    For wks = 3 To (Worksheets.Count) - 2
        With Worksheets(wks)
            For j = 0 To 7
                .Cells(5, 41 + numSF + j).FormulaR1C1 = "=SUM(R[3]C:R[65531]C)"
                .Cells(6, 41 + numSF + j).FormulaR1C1 = "=R[-3]C-R[-1]C"
                Worksheets("Template").Range("BD2:BK2").Copy Destination:=.Range(.Cells(2, 41 + numSF), .Cells(2, 41 + numSF + 7))
                .Range(.Cells(2, 41 + numSF), .Cells(2, 41 + numSF + 7)).ColumnWidth = 13.71
                .Range(.Cells(5, 41 + numSF), .Cells(6, 41 + numSF + 7)).NumberFormat = "$#,##0"
            Next
        End With
    Next
End If

'This will count the number of columns to include as per the header
With Worksheets("Template")
    numCol = .Range(.Cells(2, 1), .Cells(2, RawExport_Col(36)).End(xlToRight)).Count

    'Set the PROJ and WP template line
    Set PROJLine = .Range(.Cells(6, 1), .Cells(6, numCol))
    Set WPLine = .Range(.Cells(8, 1), .Cells(8, numCol))
End With

'----------------------------------START POPULATE THE SHEETS    
Row_Export = 1
Do
    Row_Export = Row_Export + 1
    SFflag = False 'set flag false since new PROJ
    'Set status bar
    statusP = CInt((Row_Export - 1) / (numRecords + 1) * 100)
    If statusP = 10 Or statusP = 30 Or statusP = 60 Or statusP = 90 Then
        Application.Wait (Now() + 0.00001)
    End If

    Application.StatusBar = "Please wait while performing task 7 (" + CStr(statusP) + "%) of 7 ..."

    If Worksheets("Raw Export").Cells(Row_Export, 3) = "" Then 'Funding Area
        Exit Do
    ElseIf Not (Worksheets("Raw Export").Cells(Row_Export, 1) = "" Or Worksheets("Raw Export").Cells(Row_Export, 2) = "") Then
        With Worksheets("Rehab")
            Row_Rehab = .Range(.Cells(2, RawExport_Col(32)), .Cells(2, RawExport_Col(32)).End(xlDown)).Count + 2
        End With
        Do
            Row_Rehab = Row_Rehab + 1
            If Worksheets("Rehab").Cells(Row_Rehab, 1) = Worksheets("Raw Export").Cells(Row_Export, 1) And Worksheets("Rehab").Cells(Row_Rehab, 2) = Worksheets("Raw Export").Cells(Row_Export, 2) Then
                Do
                    Row_Rehab = Row_Rehab + 1
                    If Worksheets("Rehab").Cells(Row_Rehab, 1) = "" And Worksheets("Rehab").Cells(Row_Rehab, 2) = "" And Worksheets("Rehab").Cells(Row_Rehab, Rehab_PROJ_Col) = "" Then
                        PROJLine.Copy Destination:=Worksheets("Rehab").Range(Worksheets("Rehab").Cells(Row_Rehab, 1), Worksheets("Rehab").Cells(Row_Rehab, numCol))
                        Exit Do
                    End If
                Loop
                Exit Do
            End If
        Loop

        PROJ = Worksheets("Raw Export").Cells(Row_Export, Export_PROJ_Col)
        '-----------------------------PROJ LINE
        For Export_Column = 4 To 10
            Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = Worksheets("Raw Export").Cells(Row_Export, Export_Column)
        Next
        For Export_Column = 13 To 16
            Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = Worksheets("Raw Export").Cells(Row_Export, Export_Column)
        Next
        If Worksheets("Raw Export").Cells(Row_Export, 18) = "" Then
            Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(18)) = Worksheets("Raw Export").Cells(Row_Export, 19)
        ElseIf Worksheets("Raw Export").Cells(Row_Export, 19) = "" Then
            Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(18)) = Worksheets("Raw Export").Cells(Row_Export, 18)
        End If
        For Export_Column = 20 To 33
            If Export_Column = 25 Then
                If Worksheets("Raw Export").Cells(Row_Export, Export_Column) = True Then
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = "YES"
                End If
            ElseIf Export_Column = 27 Or Export_Column = 29 Then
                If Worksheets("Raw Export").Cells(Row_Export, Export_Column) = True Then
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)).Font.Name = "Wingdings"
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = Chr(252)
                End If
            Else
                Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = Worksheets("Raw Export").Cells(Row_Export, Export_Column)
            End If
        Next
        For Export_Column = 36 To 42
            If Not Worksheets("Raw Export").Cells(Row_Export, Export_Column) = 0 Then
                Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = Worksheets("Raw Export").Cells(Row_Export, Export_Column)
            End If
        Next                    

        Worksheets("Rehab").Cells(Row_Rehab, 14) = Worksheets("Raw Export").Cells(Row_Export, 43)
        Worksheets("Rehab").Cells(Row_Rehab, 16) = Worksheets("Raw Export").Cells(Row_Export, numP + 10)
        Worksheets("Rehab").Cells(Row_Rehab, colEngComment) = Worksheets("raw export").Cells(Row_Export, numP + 3)
        Worksheets("rehab").Cells(Row_Rehab, colSubmissionCRO) = Worksheets("raw export").Cells(Row_Export, numP + 4)
        Worksheets("Rehab").Cells(Row_Rehab, colPriority) = Worksheets("raw export").Cells(Row_Export, numP + 11)


        If Worksheets("raw export").Cells(Row_Export, numP + 5) = False Then
            Worksheets("rehab").Cells(Row_Rehab, colActualSubmissionCRO) = ""
        Else
            Worksheets("rehab").Cells(Row_Rehab, colActualSubmissionCRO) = Chr(252)
            Worksheets("Rehab").Cells(Row_Rehab, colActualSubmissionCRO).Font.Name = "Wingdings"
        End If
                '----------Expansion only
        If Worksheets("Parameters").Cells(18, 14) = "Expansion" Then
            For Export_Column = startColExp To (startColExp + 7)
                If Not Worksheets("raw export").Cells(Row_Export, Export_Column) = 0 Then
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = Worksheets("Raw Export").Cells(Row_Export, Export_Column)
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)).NumberFormat = "$#,##0"
                End If
            Next
        End If
        '-----------------------------------
                   PROJ_Row = Row_Rehab
        Worksheets("Rehab").Rows(Row_Rehab).RowHeight = 12.75
        Row_Rehab = Row_Rehab + 1
        Worksheets("Rehab").Rows(Row_Rehab).EntireRow.Insert
        Row_Rehab = Row_Rehab - 1
        '-------------------SUBWB LINES
        Do
            If Not Worksheets("Raw Export").Cells(Row_Export, Export_PROJ_Col) = PROJ Then
                Row_Export = Row_Export - 1
                Exit Do
            Else
                Row_Rehab = Row_Rehab + 1
                Worksheets("Rehab").Rows(Row_Rehab).EntireRow.Insert
                WPLine.Copy Destination:=Worksheets("Rehab").Range(Worksheets("Rehab").Cells(Row_Rehab, 1), Worksheets("Rehab").Cells(Row_Rehab, numCol))

                Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(11)) = Worksheets("Raw Export").Cells(Row_Export, 11)
                If Worksheets("Raw Export").Cells(Row_Export, 12) = True Then
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(12)) = "DOM"
                End If

                Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(15)) = Worksheets("Raw Export").Cells(Row_Export, 15)
                Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(17)) = Worksheets("Raw Export").Cells(Row_Export, 17)

                If Worksheets("Raw Export").Cells(Row_Export, 18) = "" Then
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(18)) = Worksheets("Raw Export").Cells(Row_Export, 19)
                ElseIf Worksheets("Raw Export").Cells(Row_Export, 19) = "" Then
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(18)) = Worksheets("Raw Export").Cells(Row_Export, 18)
                End If

                For Export_Column = 20 To 24
                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(Export_Column)) = Worksheets("Raw Export").Cells(Row_Export, Export_Column)
                Next

                Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(35)) = Worksheets("Raw Export").Cells(Row_Export, 35)

                WorkType = Worksheets("Rehab").Cells(Row_Rehab, Rehab_WorkType_Col)
                Set line = Worksheets("Rehab").Range(Worksheets("Rehab").Cells(Row_Rehab, 1), Worksheets("Rehab").Cells(Row_Rehab, numCol))
                If Not (InStr(UCase(WorkType), "STR") > 0) And Not (InStr(UCase(WorkType), "SAF") > 0) Then
                    line.Interior.ColorIndex = 36 'yellow
                    Worksheets("Rehab").Cells(Row_Rehab, colPriority) = Worksheets("raw export").Cells(Row_Export, numP + 2)
                ElseIf InStr(UCase(WorkType), "STR") > 0 Then
                    line.Interior.ColorIndex = 34 'blue
                    Worksheets("Rehab").Cells(Row_Rehab, colPriority) = Worksheets("raw export").Cells(Row_Export, numP + 1)
                ElseIf InStr(UCase(WorkType), "SAF") > 0 Then
                    line.Interior.ColorIndex = 38 'pink
                    Worksheets("Rehab").Cells(Row_Rehab, colPriority) = Worksheets("raw export").Cells(Row_Export, numP)
                End If

                'Check if special funding applies to this WP
                'Check each special funding type
                For SF = 1 To numSF 'for each special funding type - will not execute if none
                    startCol = Worksheets("Template").Cells(11, SF + 2)
                        'Designer
                        If Worksheets("Raw Export").Cells(Row_Export, numP + 6) = Worksheets("Template").Cells(12, SF + 2) Then
                            Worksheets("Rehab").Cells(PROJ_Row, RawExport_Col(42) + SF) = "YES"
                            If Not Worksheets("raw export").Cells(Row_Export, numP + 6) = "" Then
                                If Worksheets("Raw export").Cells(Row_Export, numP + 6) = "In House" Or Worksheets("raw export").Cells(Row_Export, numP + 10) = "" Then
                                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(42) + SF).Font.Name = "Wingdings"
                                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(42) + SF) = Chr(252)
                                Else
                                    Worksheets("rehab").Cells(Row_Rehab, RawExport_Col(42) + SF) = Worksheets("raw export").Cells(Row_Export, numP + 10)
                                End If
                            End If
                        End If

                        'Delivery Method
                        If Worksheets("Raw Export").Cells(Row_Export, numP + 9) = Worksheets("Template").Cells(12, SF + 2) Then
                            Worksheets("Rehab").Cells(PROJ_Row, RawExport_Col(42) + SF) = "YES"
                            If Not Worksheets("raw export").Cells(Row_Export, numP + 9) = "" Then
                                Worksheets("rehab").Cells(Row_Rehab, RawExport_Col(42) + SF) = Worksheets("raw export").Cells(Row_Export, numP + 8)
                                Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(42) + SF).WrapText = False
                            End If
                        End If

                        'SFunding
                        If Worksheets("raw export").Cells(Row_Export, Export_SF_Col) <> "MNDM Design Build" _
                            And Worksheets("raw export").Cells(Row_Export, Export_SF_Col) <> "MTO Design Build" _
                            And Worksheets("raw export").Cells(Row_Export, Export_SF_Col) <> "In House" Then
                            If Worksheets("Raw Export").Cells(Row_Export, Export_SF_Col) = Worksheets("Template").Cells(12, SF + 2) Then
                                Worksheets("Rehab").Cells(PROJ_Row, RawExport_Col(42) + SF) = "YES"
                                If Not Worksheets("raw export").Cells(Row_Export, 34) = "" Then
                                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(42) + SF).Font.Name = "Wingdings"
                                    Worksheets("Rehab").Cells(Row_Rehab, RawExport_Col(42) + SF) = Chr(252)
                                End If
                            End If
                        End If

                        If Worksheets("Rehab").Cells(PROJ_Row, RawExport_Col(42) + SF) = "YES" Then
                            Worksheets("Rehab").Cells(PROJ_Row, RawExport_Col(42) + SF).Interior.ColorIndex = Worksheets("Rehab").Cells(2, RawExport_Col(42) + SF).Interior.ColorIndex
                            SFflag = True
                        End If
                Next SF
                Worksheets("Rehab").Rows(Row_Rehab).RowHeight = 12.75
                Row_Export = Row_Export + 1
            End If
        Loop
        '------------------------End SUBWP Line

        '------------------------Special Funding Tabs
        'Add to special funding tab iF special funding applied
        If SFflag Then
            For SF = 1 To numSF 'check if special funding type applies
                If Worksheets("Rehab").Cells(PROJ_Row, RawExport_Col(42) + SF) = "YES" Then
                'find place for that project in the tab
                    SF_type = Worksheets("Rehab").Cells(2, RawExport_Col(42) + SF)
                    With Worksheets(SF_type)
                        SF_Row = .Range(.Cells(2, RawExport_Col(32)), .Cells(2, RawExport_Col(32)).End(xlDown)).Count + 2
                    End With
                    Do 'Find the header
                        SF_Row = SF_Row + 1
                        If Worksheets(SF_type).Cells(SF_Row, 1) = Worksheets("Raw Export").Cells(Row_Export, 1) And Worksheets(SF_type).Cells(SF_Row, 2) = Worksheets("Raw Export").Cells(Row_Export, 2) Then
                            'Found header, now find next available spot
                            Do
                                SF_Row = SF_Row + 1
                                'Check if next line is free by checking if PROJ null
                                If (Worksheets(SF_type).Cells(SF_Row, 7) = "") Then
                                'Found free row, add project above it
                                'Add new lines
                                    For i = 0 To (Row_Rehab - PROJ_Row)
                                        Worksheets(SF_type).Rows(SF_Row).EntireRow.Insert
                                    Next i
                                    'Select added new lines
                                    With Worksheets(SF_type)
                                        Set SFInfo = .Range(.Cells(SF_Row, 1), .Cells(SF_Row + (Row_Rehab - PROJ_Row), numCol))
                                    End With
                                    'Copy rehab project to the range
                                    With Worksheets("Rehab")
                                        .Range(.Cells(PROJ_Row, 1), .Cells(Row_Rehab, numCol)).Copy Destination:=SFInfo
                                    End With
                                    'Copy costs into range
                                    startCol = Worksheets("Template").Cells(11, SF + 2)
                                    startCol2 = Worksheets("Template").Cells(9, SF + 2)
                                    For col = 0 To 6
                                        Worksheets(SF_type).Cells(SF_Row, RawExport_Col(36) + col) = Worksheets("Raw Export").Cells(Row_Export, startCol + col)
                                    Next col
                                    'Copy costs for extra years if its expansion
                                    If Worksheets("Parameters").Cells(18, 14) = "Expansion" Then
                                        For col = 0 To 7
                                            Worksheets(SF_type).Cells(SF_Row, 41 + numSF + col) = Worksheets("Raw Export").Cells(Row_Export, startCol2 + col)
                                        Next col
                                    End If
                                    Set SFInfo = Nothing
                                    Exit Do
                                End If
                            Loop
                            Exit Do
                        End If
                    Loop
                End If
            Next SF
        End If
        '------------------------End Special Funding Tabs
    End If
Loop

Application.StatusBar = "Formatting..."

For wks = 2 To (Worksheets.Count - 2)
    With Worksheets(wks)
        .Columns("A:C").ColumnWidth = 7.57 'Year
        .Columns("D:D").ColumnWidth = 9.86 'Contract
        .Columns("F:F").ColumnWidth = 4 'Area
        .Columns("G:G").ColumnWidth = 14.57 'PROJ
        .Columns("H:H").ColumnWidth = 3.43 'DOM
        .Columns("I:I").ColumnWidth = 6 'Priority
        .Columns("J:J").ColumnWidth = 9.57 'Contingency Year
        .Columns("L:L").ColumnWidth = 6 'Hwy
        .Columns("M:M").ColumnWidth = 31.71 'Location
        .Columns("N:O").ColumnWidth = 16.14 'Comment
        .Columns("V:V").ColumnWidth = 23 'Project Type
        .Columns("R:U").ColumnWidth = 6.43 'LHRS
        .Columns("X:AB").ColumnWidth = 10.29 'Date
        .Columns("Y:Y").ColumnWidth = 2.57 'Actual
        .Columns("AA:AA").ColumnWidth = 2.57 'Actual
        .Columns("AC:AC").ColumnWidth = 2.57 'Actual
        .Columns("AE:AG").ColumnWidth = 12.5
        .Columns("AH:AN").ColumnWidth = 13.71 'Cost
        .Rows("2:2").EntireRow.AutoFit
    End With
Next

'Finalize - Rearrange the column, format color

'last column before defined print area end
If Worksheets("Parameters").Cells(18, 14) = "Expansion" Then
    colEmptyE = Cells(1, 40 + numSF).Address(1, 0)
    colEmptyE = Left(colEmptyE, InStr(colEmptyE, "$") - 1)

    For wks = 2 To (Worksheets.Count) - 2
            With Worksheets(wks)
                .PageSetup.PrintArea = "A:" & colEmptyE & ""
            End With
    Next wks
End If

Application.CutCopyMode = False
Worksheets(2).Name = Worksheets("Parameters").Cells(18, 14).Value

ActiveWorkbook.Save

End Sub

3 个答案:

答案 0 :(得分:1)

将第一个Do更改为:

Do While Row_Export <= Sheets("Raw Export").Rows.Count

答案 1 :(得分:0)

试试这个。如果行&#39; Debug.Print Row_Export&#39;是零或负是代码不对。

debug.print可用于检查分配给变量的值。在这种情况下,数字应为正数且大于零。

Do
   Row_Rehab = Row_Rehab + 1
   Debug.Print Row_Rehab
   Debug.Print Row_Export
   If Worksheets("Rehab").Cells(Row_Rehab, 1) = Worksheets("Raw Export").Cells(Row_Export, 1) And Worksheets("Rehab").Cells(Row_Rehab, 2) = Worksheets("Raw Export").Cells(Row_Export, 2) Then
      Do
        Row_Rehab = Row_Rehab + 1
          If Worksheets("Rehab").Cells(Row_Rehab, 1) = "" And Worksheets("Rehab").Cells(Row_Rehab, 2) = "" And Worksheets("Rehab").Cells(Row_Rehab, Rehab_GWP_Col) = "" Then
          ProjLine.Copy Destination:=Worksheets("Rehab").Range(Worksheets("Rehab").Cells(Row_Rehab, 1), Worksheets("Rehab").Cells(Row_Rehab, numCol))
          Exit Do
          End If
     Loop
     Exit Do
     End If
Loop

答案 2 :(得分:0)

如果我理解你的目标,那就不是真的,但我会选择这样的事情:

Set ShtRehab = Worksheets("Rehab")
Set ShtExport = Worksheets("Raw Export")
matchFound = False
For Each RowRehab In ShtRehab.UsedRange.Rows
    ' Find the keys that match those in your export sheet
    If RowRehab.Cells(1, 1) = ShtExport.Cells(Row_Export, 1) _
    And RowRehab.Cells(1, 2) = ShtExport.Cells(Row_Export, 2) Then
        matchFound = True
    Else
        ' Now copy to the first key-less line after it
        If RowRehab.Cells(1, 1) = "" _
        And RowRehab.Cells(1, 2) = "" _
        And RowRehab.Cells(1, Rehab_GWP_Col) = "" Then
            ProjLine.Copy Destination:=RowRehab
            Exit For
        End If
    End If
Next