Export rows to another sheet if the quantity for a device is not zero

theredjay

New Member
Joined
Sep 20, 2023
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
Hello
I have attached a workbook where I would like to export from the "info" sheet only the rows within the range C2:F60 if the Quantity (column E) for a device is not zero; these rows should be exported to a new sheet within the "example" workbook. Something to note is that the information for the Device Name (Column C) and the Number (Column D) comes from the "data" sheet through a simple formula, so I think the data needs to be exported as values. Thank you
 

Attachments

  • data tab.PNG
    data tab.PNG
    25.5 KB · Views: 21
  • info tab1.PNG
    info tab1.PNG
    35.7 KB · Views: 21
  • info tab2.PNG
    info tab2.PNG
    30.6 KB · Views: 24
  • info tab3.PNG
    info tab3.PNG
    37 KB · Views: 22
  • info tab4.PNG
    info tab4.PNG
    25.2 KB · Views: 20

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I forgot to mention on my previous post if someone could help me with the VBA code to complete the task
 
Upvote 0
Assuming you have headers in row 1 of your example sheet and entered into column A, give this a try on a copy of your sheet:

What is the name of your second workbook where the "example" sheet is located?

VBA Code:
Private Sub MoveData()
Dim wsA, wsB As Worksheet:      Set wsA = Sheets("info"):     Set wsB = Sheets("example")
Dim i, lRow&:                   lRow = wsB.Range("A" & Rows.Count).End(xlUp).Row + 1

For i = 2 To 60
    With wsA
        If .Range("E" & i).Value > 0 Then
            With wsB
                .Range("A" & lRow).Value = wsA.Range("C" & i).Value
                .Range("B" & lRow).Value = wsA.Range("D" & i).Value
                .Range("C" & lRow).Value = wsA.Range("E" & i).Value
                .Range("D" & lRow).Value = wsA.Range("F" & i).Value
            End With
        lRow = lRow + 1
        End If
    End With
Next i
End Sub
 
Last edited:
Upvote 0
Assuming you have headers in row 1 of your example sheet and entered into column A, give this a try on a copy of your sheet:

What is the name of your second workbook where the "example" sheet is located?

VBA Code:
Private Sub MoveData()
Dim wsA, wsB As Worksheet:      Set wsA = Sheets("info"):     Set wsB = Sheets("example")
Dim i, lRow&:                   lRow = wsB.Range("A" & Rows.Count).End(xlUp).Row + 1

For i = 2 To 60
    With wsA
        If .Range("E" & i).Value > 0 Then
            With wsB
                .Range("A" & lRow).Value = wsA.Range("C" & i).Value
                .Range("B" & lRow).Value = wsA.Range("D" & i).Value
                .Range("C" & lRow).Value = wsA.Range("E" & i).Value
                .Range("D" & lRow).Value = wsA.Range("F" & i).Value
            End With
        lRow = lRow + 1
        End If
    End With
Next i
End Sub
Hello

I tried the VBA code but it did not work. I uploaded the file so its easier to come up with the VBA code, file can be downloaded at


Thank you
 
Upvote 0
Hello

I tried the VBA code but it did not work. I uploaded the file so its easier to come up with the VBA code, file can be downloaded at


Thank you
I can take a look at that in a few hours when I am home.
 
Upvote 0
Okay, you haven't said what name the new sheet is called, nor have you created it yet, so I used a temp sheet called "New Sheet" to copy the data to. Once you create the new sheet and name it, change the sheet name in the code where I have indicated, and this code should work.

VBA Code:
Private Sub MoveData()
Dim wsA, wsB As Worksheet:      Set wsA = Sheets("info"):     Set wsB = Sheets("New Sheet") 'Change "New Sheet" to the name of the target sheet where data is moved to.
Dim i, lRow&:                   lRow = wsB.Range("A" & Rows.Count).End(xlUp).Row + 1        'Identify the last used row on target sheet and add 1 to get the next empty row.

'Loop through the range C2:F60 and check for col E values greater than 0 and exclude header rows.
For i = 2 To 60
    With wsA
        If .Range("E" & i).Value > 0 And .Range("E" & i).Value <> "QUANTITY" Then
        
            'Copy data from info sheet to target sheet.
            With wsB
                .Range("A" & lRow).Value = wsA.Range("C" & i).Value
                .Range("B" & lRow).Value = wsA.Range("D" & i).Value
                .Range("C" & lRow).Value = wsA.Range("E" & i).Value
                .Range("D" & lRow).Value = wsA.Range("F" & i).Value
            End With
        lRow = lRow + 1
        End If
    End With
Next i
End Sub
 
Upvote 0
A few edits to the code to shorten it a bit.

VBA Code:
Private Sub MoveData()
Dim wsA, wsB As Worksheet:      Set wsA = Sheets("info"):     Set wsB = Sheets("New Sheet") 'Change "New Sheet" to the name of the target sheet where data is moved to.
Dim i, lRow&:                   lRow = wsB.Range("A" & Rows.Count).End(xlUp).Row + 1        'Identify the last used row on target sheet and add 1 to get the next empty row.

'Loop through the range C2:F60 and check for col E values greater than 0 and exclude header rows.
For i = 2 To 60
        If wsA.Range("E" & i).Value > 0 And wsA.Range("E" & i).Value <> "QUANTITY" Then
                'Copy data from info sheet to target sheet.
                wsB.Range("A" & lRow & ":D" & lRow).Value = wsA.Range("C" & i & ":F" & i).Value
                lRow = lRow + 1
        End If
Next i
End Sub
 
Upvote 0
A few edits to the code to shorten it a bit.

VBA Code:
Private Sub MoveData()
Dim wsA, wsB As Worksheet:      Set wsA = Sheets("info"):     Set wsB = Sheets("New Sheet") 'Change "New Sheet" to the name of the target sheet where data is moved to.
Dim i, lRow&:                   lRow = wsB.Range("A" & Rows.Count).End(xlUp).Row + 1        'Identify the last used row on target sheet and add 1 to get the next empty row.

'Loop through the range C2:F60 and check for col E values greater than 0 and exclude header rows.
For i = 2 To 60
        If wsA.Range("E" & i).Value > 0 And wsA.Range("E" & i).Value <> "QUANTITY" Then
                'Copy data from info sheet to target sheet.
                wsB.Range("A" & lRow & ":D" & lRow).Value = wsA.Range("C" & i & ":F" & i).Value
                lRow = lRow + 1
        End If
Next i
End Sub
Hello
Is it possible to make the "New Sheet" output look like my new uploaded file (uploaded picture as well) where I have deleted rows 14, 15, 18, 26, 29, 30, 37-42, and 50.

thank you
 

Attachments

  • can it look like this.png
    can it look like this.png
    65.9 KB · Views: 18
Upvote 0
Hello
Is it possible to make the "New Sheet" output look like my new uploaded file (uploaded picture as well) where I have deleted rows 14, 15, 18, 26, 29, 30, 37-42, and 50.

thank you

Okay, slightly different approach then. Instead of copying each row at a time and only moving the rows where E>0, we can copy the entire range C2:F60 to the new sheet (to preserve the formatting) then loop through the data and delete the rows where E=0. Currently, it only pastes the values of the formulas, so if you want the formulas to copy over to the new sheet as well we'll have to adjust it a little.

VBA Code:
Private Sub MoveData()
Dim wsA, wsB As Worksheet:      Set wsA = Sheets("info"):     Set wsB = Sheets("New Sheet") 'Change "New Sheet" to the name of the target sheet where data is moved to.
Dim i, lRow&:                   'lRow = wsB.Range("A" & Rows.Count).End(xlUp).Row + 1        'Identify the last used row on target sheet and add 1 to get the next empty row.

wsA.Range("C2:F60").Copy
With wsB.Range("C2:F60")
    .PasteSpecial xlPasteColumnWidths
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
End With

'Loop through the range C2:F60 and delete rows where col E = 0.
For i = 60 To 9 Step -1
        If wsB.Range("E" & i).Value = 0 And wsB.Range("E" & i).Value <> "QUANTITY" And wsB.Range("E" & i) <> "" Then
                wsB.Range("C" & i & ":F" & i).Delete Shift:=xlUp
        End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top