excel 粘贴循环问题

6ojccjat  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(193)

我在继续我的循环的粘贴范围时遇到了麻烦。一旦它完成了一个粘贴范围,一旦我插入一个新的粘贴范围,它就会遇到麻烦。

Dim sht As Worksheet
    Dim sht1 As Worksheet
    Dim sht2 As Worksheet
    Dim sht3 As Worksheet
    Dim nwbsht1 As Worksheet
    Dim nwbsht2 As Worksheet
    Dim nwbsht3 As Worksheet
    Dim nwbsht4 As Worksheet
    Dim nwbsht5 As Worksheet
    Dim nwbsht6 As Worksheet
    Dim nwbsht7 As Worksheet
    Dim nwbsht8 As Worksheet
    Dim rngToAbs As Range
    Dim c As Range
    Dim wb As Workbook
    Dim nwb As Workbook
    Dim Onhand As Range
    Dim OnHand2 As Range
    Dim OnHand1 As Range
    Dim OnHand3 As Range
    Dim Pallet As Range
    Dim PalletType As Range
    Dim Item As Range
    Dim Item2 As Range
    Dim UnitQty As Range
    Dim CL As Range
    Dim DL As Range
    Dim OutputArray
    Dim I As Long
    
    
    
    'Setting ranges for PackPlan workbook
    Set wb = Application.ActiveWorkbook
    
       With ActiveWorkbook.Worksheets("Arils Pack Plan ")
        Set rngToAbs = .Range("F7:F28")
        Set Item = .Range("B7:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
        Set PalletType = .Range("E7:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)
    End With
    
    'Opening Recent ATS report

    With Application.FileDialog(msoFileDialogOpen)
        .Filters.Clear
        .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa"
        .AllowMultiSelect = False
        .Show
    Application.Workbooks.Open .SelectedItems(1)
    Set nwb = Application.ActiveWorkbook

    End With

    'Setting Ranges for Daily Need Worksheet

    '4oz Range Setting

    Set nwb = Application.ActiveWorkbook
    Set nwbsht1 = nwb.Sheets("DAILY NEED (DR)")
    Set Onhand = nwbsht1.Range("Q5:Q14")

    Set nwb = Application.ActiveWorkbook
    Set nwbsht2 = nwb.Sheets("DAILY NEED (DR)")
    Set Pallet = nwbsht2.Range("E5:E14")

    Set nwb = Application.ActiveWorkbook
    Set nwbsht3 = nwb.Sheets("DAILY NEED (DR)")
    Set OnHand1 = nwbsht3.Range("U5:U14")

    Set nwb = Application.ActiveWorkbook
    Set nwbsht4 = nwb.Sheets("DAILY NEED (DR)")
    Set OnHand2 = nwbsht4.Range("Y5:Y14")
   

    '8oz Range Setting
    Set nwb = Application.ActiveWorkbook
    Set nwbsht5 = nwb.Sheets("DAILY NEED (DR)")
    Set OnHand3 = nwbsht5.Range("Q15:Q25")

    Set nwb = Application.ActiveWorkbook
    Set nwbsht6 = nwb.Sheets("DAILY NEED (DR)")
    Set Pallet = nwbsht6.Range("E15:E25")

    Set nwb = Application.ActiveWorkbook
    Set nwbsht7 = nwb.Sheets("DAILY NEED (DR)")
    Set OnHand1 = nwbsht7.Range("T15:T25")

    Set nwb = Application.ActiveWorkbook
    Set nwbsht8 = nwb.Sheets("DAILY NEED (DR)")
    Set OnHand2 = nwbsht8.Range("Y15:Y25")

    'Copy and Paste Loop for Units

    nwb.Activate

     I = 1
    ReDim OutputArray(1 To Onhand.Cells.Count)
    For Each CL In Onhand.Cells
        If CL.Value < 0 Then
            OutputArray(I) = CL.Value
            I = I + 1
        End If
    Next CL

    wb.Activate
    wb.Sheets("Arils Pack Plan ").Range("F7").Resize(I, 1) = Application.Transpose(OutputArray)
  
  
  
  nwb.Activate

    ReDim OutputArray(1 To OnHand1.Cells.Count)
    For Each DL In OnHand1.Cells
        If DL.Value < 0 Then
            OutputArray(I) = DL.Value
            I = I + 1
        End If
    Next DL
  
    wb.Activate
    wb.Sheets("Arils Pack Plan ").Range("F7").Resize(I, 1) = Application.Transpose(OutputArray)
n53p2ov0

n53p2ov01#

以下是如何执行此操作的建议(仅适用于相关部分):

Option Explicit

Sub Tester()
    
    Dim wb As Workbook, colVals As Collection, v, arr, c As Range
    Dim nwb As Workbook, wsAPP As Worksheet, wsDNDR As Worksheet
    
    Set wb = Application.ActiveWorkbook           'ThisWorkbook?
    Set wsAPP = wb.Worksheets("Arils Pack Plan ") 'trailing space?
    
    'Opening Recent ATS report
    With Application.FileDialog(msoFileDialogOpen)
        .Filters.Clear
        .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub 'user didn't pick a file
        Set nwb = Application.Workbooks.Open(.SelectedItems(1))
    End With
    
    Set wsDNDR = nwb.Worksheets("DAILY NEED (DR)")
    
    Set colVals = New Collection
    'collect all of the negative values
    For Each c In wsDNDR.Range("Q5:Q14,E5:E14,U5:U14,Y5:Y14,Q15:Q25,E15:E25,T15:T25,Y15:Y25").Cells
        v = c.Value
        If v < 0 Then colVals.Add v
    Next c
    
    arr = CollectionToArray(colVals) 'transfer the collection values to an array
    If Not IsEmpty(arr) Then
        wsAPP.Range("F7").Resize(UBound(arr, 1), 1) = arr 'place the array on the sheet
    End If
  
End Sub

'return a 2D array ("column" format) with the values from a Collection
Function CollectionToArray(col As Collection)
    Dim arr, i As Long
    If col.Count > 0 Then
        ReDim arr(1 To col.Count, 1 To 1)
        For i = 1 To col.Count
            arr(i, 1) = col(i)
        Next i
        CollectionToArray = arr
    Else
        CollectionToArray = Empty
    End If
End Function

相关问题