0

I am trying to create a paste loop and want the loop to continue as long as the values within the column are negative and paste it to another workbook that the user will select. Also, I need to paste one range first before pasting another. And in the new workbook, I need to start pasting one cell down once it is finished with the 4oz loop to start with the 8oz loop.

Sub Absolute_Value() ' ' Absolute_Value Macro 'Defining Terms

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)

2 Answers2

1

As a clarification from my comment about setting multiple varaibles to the same object...

Instead of this:

   'Setting ranges for PackPlan workbook
   Set wb = Application.ActiveWorkbook
    Set sht = wb.Sheets("Arils Pack Plan ")
        LastRow = sht.Cells(sht.Rows.Count, "F").End(xlUp).Row
        Set rngToAbs = sht.Range("F7:F" & LastRow)
    
    Set wb = Application.ActiveWorkbook
    Set sht1 = wb.Sheets("Arils Pack Plan ")
        LastRW5 = sht1.Cells(sht1.Rows.Count, "B").End(xlUp).Row
        Set Item = sht1.Range("B7:B" & LastRW5)
    
    Set wb = Application.ActiveWorkbook
    Set sht2 = wb.Sheets("Arils Pack Plan ")
        LastRW4 = sht2.Cells(sht2.Rows.Count, "E").End(xlUp).Row
        Set PalletType = sht2.Range("E7:E" & LastRW4)

you could do this:

    With ActiveWorkbook.Worksheets("Arils Pack Plan ")
        Set rngToAbs = .Range("F7:F" & .Cells(.Rows.Count, "F").End(xlUp).Row)
        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

Typically though, you don't want to be reading columns from the same table using different "last row" values - pick one column to find the last row, and use that for all the columns in the table.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
-1

I think this is what you're trying to accomplish with your for loop:

Option Explicit
Sub Example()

    Dim CL As Range
    Dim Onhand As Range
    Dim OutputArray
    Dim I As Long
    
    ' This section is just to define my own ranges for testing.
    ' You delete this and just make sure onhand is properly defined.
    Set Onhand = Sheet4.Range("B3:B25")
    
'   All the rest of your code...
    
    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
    
    'Change this to wherever you want the data pasted.
    Sheet4.Range("E3").Resize(I, 1) = Application.Transpose(OutputArray)

End Sub

This ? :
enter image description here

Cameron Critchlow
  • 1,814
  • 1
  • 4
  • 14