简体   繁体   中英

How can I get values from one column copied to another sheet only if a value exists in a second column

New here, and trying to find a solution to auto create work orders and materials lists from a list of upgrades. I think I may need to write a VBA for this. Below is a table like what we have in one of our sheets. We need to be able to copy the values in column a (CFM target reduction, Recessed light box, etc) in to another spreadsheet only if the value in column c is greater than 0.

+--------------------------------------+--+----+------+
|          Fire Rated Sealing          |  |    |  LF  |
+--------------------------------------+--+----+------+
| CFM Target Reduction                 |  |  1 | CFM  |
| Recessed Light Box                   |  | 10 | EA   |
| Seal Ducts at Plenum                 |  |  1 | Duct |
| Attic Access Mate- Magnetic          |  |  1 | EA   |
| Attic Tent for Attic Stairs (zipper) |  |  0 | EA   |
| Attic Insulation- Blown in Cellulose |  |  0 | SF   |
| Batt Insulation- Fiberglass          |  |  0 | SF   |
| Insulate Condensent Line             |  |  0 | LF   |
| Knee wall:  (Insul. w/ Fiberglass)   |  |  2 | SF   |
| Knee wall:  (Seal w/ Foamboard)      |  |  4 | SF   |
+--------------------------------------+--+----+------+

I tried this with IF functions, however it ended up with just this -

╔════════════════════════════════════╗
║                 0                  ║
╠════════════════════════════════════╣
║ CFM Target Reduction               ║
║ Recessed Light Box                 ║
║ Seal Ducts at Plenum               ║
║ Attic Access Mate- Magnetic        ║
║ 0                                  ║
║ 0                                  ║
║ 0                                  ║
║ 0                                  ║
║ Knee wall:  (Insul. w/ Fiberglass) ║
║ Knee wall:  (Seal w/ Foamboard)    ║
╚════════════════════════════════════╝

Which is unattractive and doesn't fit our needs. What we need is to return only the values of column A to another spreadsheet without the zeroes. Ideally we'd also be able to query a short description of each of the items on the work order. All help is appreciated!

You can use this simple macro:

Sub test()
   Dim sh1 As Worksheet
   Dim sh2 As Worksheet
   Dim lastrow As Long
   Dim i As Long, j As Long
   'you can change Sheet1 and Sheet2 to your sheet names'
   Set sh1 = ThisWorkbook.Worksheets("Sheet1")
   Set sh2 = ThisWorkbook.Worksheets("Sheet2")

   'determining last row in Sheet1 in column A (I suppose that your data in column A)'
   lastrow = sh1.Range("A" & sh1.Rows.Count).End(xlUp).Row

   'I suppose that in Sheet2 your data will starts from row №2'
   j = 2 

   'I suppose that in Sheet1 your data will starts from row №2 too'
   For i = 2 To lastrow
       'if in Sheet1 in column C there is value greater 0, than copy'
       If sh1.Range("C" & i) > 0 Then
           'copy data from sheet1 column A to sheet2 column A'
           sh2.Range("A" & j) = sh1.Range("A" & i)
           j = j + 1
       End If
   Next i

End Sub
  1. Select the relevant cells of data including the headings (A1:C12?)
  2. Insert > Pivot table
  3. Drag "Fire Rated Sealing" to the ROWS section of the pivot table
  4. Drag "Heading of Column C" to the VALUES section of the pivot table
  5. Click the filter icon for "Fire Rated Sealing" and choose Value Filters > Greater Than 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM