pick negative figures with all related columns data

Zubair

Active Member
Joined
Jul 4, 2009
Messages
304
Office Version
  1. 2016
Platform
  1. Windows
I have a huge data with many years and products, from that I want to pick only negative figures and there corresponding row/column data, please help with formula.

DATA
ProductMonthVolumeGMCPL
Product AFeb1214.001.17
Product BJan1315.001.15
Product BMar14(15.00)(1.07)
Product BFeb1510.000.67
Product AJan16(15.00)(0.94)
Product AMar1715.000.88

Result required.

ProductMonthVolumeGMCPL
Product BMar14(15.00)(1.07)
Product AJan16(15.00)(0.94)
 
Please ignore previous which was only example and treat new as actual
Yes column M, R, T to X having formulas
Need formula or code in new sheet please.
Apologies again for unclear question.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
new sheet not exist.
OK, try this with a copy of your workbook.

I have assumed that the original data is on a sheet called "Data". Change that in the code where indicated.

VBA Code:
Sub Extract_Negatives()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  Sheets("Data").Copy After:=Sheets("Data") '<- Edit sheet name as required
  With Sheets(Sheets("Data").Index + 1)
    a = .Range("X2", .Range("X" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) >= 0 Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Range("A2:Y2").Resize(UBound(a))
        .Columns(25).Value = b
        .Sort Key1:=.Columns(25), Order1:=xlAscending, Header:=xlNo
        .Resize(k).EntireRow.Delete
      End With
      Application.ScreenUpdating = True
    End If
  End With
End Sub
 
Upvote 0
OK, try this with a copy of your workbook.

I have assumed that the original data is on a sheet called "Data". Change that in the code where indicated.

VBA Code:
Sub Extract_Negatives()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  Sheets("Data").Copy After:=Sheets("Data") '<- Edit sheet name as required
  With Sheets(Sheets("Data").Index + 1)
    a = .Range("X2", .Range("X" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) >= 0 Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Range("A2:Y2").Resize(UBound(a))
        .Columns(25).Value = b
        .Sort Key1:=.Columns(25), Order1:=xlAscending, Header:=xlNo
        .Resize(k).EntireRow.Delete
      End With
      Application.ScreenUpdating = True
    End If
  End With
End Sub
Many thanks but can you share steps to perform this I was expecting formula I am very weak in VBA
 
Upvote 0
can you share steps to perform this
To implement ..
1. With your workbook active press Alt+F11 to bring up the vba window.
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2 & edit the sheet name (twice) in the line indicated.
4. Close the Visual Basic window.
5. Press Alt+F8 to bring up the Macro dialog
6. Select the macro & click ‘Run’
7. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)
 
Upvote 0
To implement ..
1. With your workbook active press Alt+F11 to bring up the vba window.
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2 & edit the sheet name (twice) in the line indicated.
4. Close the Visual Basic window.
5. Press Alt+F8 to bring up the Macro dialog
6. Select the macro & click ‘Run’
7. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)
Peter - thanks its working fine Excellent !, it will be more convenient if same data may come through formula instead of VBA if possible, As I have some dashboards so I wanted to link negative figures from the data.
 
Upvote 0
it will be more convenient if same data may come through formula
With 24 columns and nearly 30,000 rows I suspect a formula solution may make your workbook very sluggish. However, if you want to try please update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

1616725910176.png
 
Upvote 0
Account details updated thanks, Please share formula 30k rows contained 7 years data and normal view is for current and last year so if will be good if selection of year may include in the formula.
 
Upvote 0
You haven't given us more than one row of meaningful sample data so my sample is just random numbers but the logic of using column X (GM CPL) and column J (Year) to identify the rows required remains the same.

I strongly suggest that you try this on something much smaller than your 30,000 rows to start with.

Here is my sample data.

Zubair.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1BusinessActual/PlanAreaSectorItemDescriptionProduct FamilyPackIMP/LocalYearQuarterMonthYTD No.VolumeGSV SARRebateDiscountTurnover SARStd Cost SARStd Gross Profit SARTurnover US$Std Cost US$Std Gross Profit US$GM CPL
2-92-500765-42019-1-63-9107-5-7-110-12-11
328109-1953-62021-1-1-8-1-112-2-1262-2-5
4-10-2-27-2-1-4-62020-6-4-1-490-850404-2-6
5941056577-32021-4-38-3-98-4-7-15-258-2
6-2-31010110-5-7-9201903-733001034-54710
7-510-6989-9-1320199-24-2520108102105-9
8-6-6-2191-3-2-12021410209-4-81-5-310151
906417-4-1-3320193-17-34183-730-8-39
1093-59115-6120212103-3-1-8-7-4104-917-7
1144-6-3-87-82-32021-6933258-5-3-45-8-10
120-510-6803-9-12019576-9-330-30-179-3-4
1301-7-21-8-4-362021-7-8-53-74-4-5-33-3-90-1
14-73-14108-67-220193-4-1-7-606108-604-3-9
158-110-675-8-93201978-553928-8-571-97
Data


.. and formula Results sheet.

Zubair.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1BusinessActual/PlanAreaSectorItemDescriptionProduct FamilyPackIMP/LocalYearQuarterMonthYTD No.VolumeGSV SARRebateDiscountTurnover SARStd Cost SARStd Gross Profit SARTurnover US$Std Cost US$Std Gross Profit US$GM CPL
228109-1953-62021-1-1-8-1-112-2-1262-2-5
3-10-2-27-2-1-4-62020-6-4-1-490-850404-2-6
4941056577-32021-4-38-3-98-4-7-15-258-2
593-59115-6120212103-3-1-8-7-4104-917-7
601-7-21-8-4-362021-7-8-53-74-4-5-33-3-90-1
7                        
8                        
Results
Cell Formulas
RangeFormula
A2:X8A2=IFERROR(INDEX(Data!A:A,AGGREGATE(15,6,ROW(Data!A$2:A$30000)/((Data!$X$2:$X$30000<0)*((Data!$J$2:$J$30000=YEAR(TODAY()))+(Data!$J$2:$J$30000=YEAR(TODAY())-1))),ROWS(A$2:A2))),"")
 
Upvote 0
Solution

Forum statistics

Threads
1,216,077
Messages
6,128,674
Members
449,463
Latest member
Jojomen56

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