Formula or Macro to Extract Account Numbers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have account numbers in Col D in Sheet TB and decriptions in Col G

I would like a formula or macro to extract the account numbers where the description New Car Sales appears in Col G on Sheet Extract

Your assistance in this regard is most appreciated
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi

See Sample data below as requested

Excel Workbook
DEFG
291000-120,403-10,035NEW CAR SALES
391000D11,526989NEW CAR SALES
491000K-7,500-500NEW CAR SALES
591001-235,618-34,453NEW CAR SALES
691001D19,9275,332NEW CAR SALES
791001K-21,333-5,333NEW CAR SALES
891002-290,974-20,167NEW CAR SALES
Sheet1
 
Upvote 0
howard,

Sample worksheets:


Excel 2007
DEFG
1Account NumbersTitle ETitle FAccount Numbers
291000-120,403-10,035NEW CAR SALES
3
491000D11,526989NEW CAR SALES
5
691000K-7,500-500NEW CAR SALES
791001-235,618-34,453NEW CAR SALES
8
991001D19,9275,332NEW CAR SALES
1091001K-21,333-5,333NEW CAR SALES
1191002-290,974-20,167NEW CAR SALES
12
TB



Excel 2007
G
1Account Numbers
2
3
4
5
6
7
8
9
Extract


After the macro in worksheet Extract:


Excel 2007
G
1Account Numbers
291000
391000D
491000K
591001
691001D
791001K
891002
9
Extract


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub GetNewCarSales()
Dim c As Range, n As Long, o As Variant, i As Long, nr As Long
Application.ScreenUpdating = False
With Sheets("TB")
  n = Application.CountIf(.Columns(7), "NEW CAR SALES")
  If n = 0 Then
    MsgBox "There are no 'NEW CAR SALES' in column G - macro terminated!"
    Exit Sub
  ElseIf n > 0 Then
    ReDim o(1 To n, 1 To 1)
    For Each c In .Range("G2", .Range("G" & Rows.Count).End(xlUp))
      If c = "NEW CAR SALES" Then
        i = i + 1
        o(i, 1) = c.Offset(, -3)
      End If
    Next c
  End If
End With
With Sheets("Extract")
  nr = .Cells(Rows.Count, 7).End(xlUp).Row + 1
  .Cells(nr, 7).Resize(UBound(o, 1)) = o
  .Columns(7).AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetNewCarSales macro.
 
Upvote 0
in J1

=IFERROR(INDEX($D$2:$D$8,SMALL(IF((TRIM($G$2:$G$8)="NEW CAR SALES"),ROW($G$2:$G$8)-ROW($G$2)+1),ROWS(J$1:J1))),"")

confrim with CONTROL SHIFT ENTER and drag down
 
Upvote 0
Hi Hiker95

Thanks for the help much appreciated. If I have a N/A# in Col G macro comes up with type mismatch. It would be appreciated if your macro will ignore all errors
Col G such as N/A#
 
Upvote 0
I have account numbers in Col D in Sheet TB and decriptions in Col G

I would like a formula or macro to extract the account numbers where the description New Car Sales appears in Col G on Sheet Extract

Your assistance in this regard is most appreciated

Sheet1, A:B, houses the source data, with headers in the first row.

Extract

A1: New car Sales

A2: Accounts

A3, control+shift+enter, not just enter, and copy down:
Rich (BB code):
=IFERROR(INDEX(Sheet1!$A$2:$A$100,
  SMALL(IF(Sheet1!$B$2:$B$100=$A$1,
  ROW(Sheet1!$B$2:$B$100)-ROW(Sheet1!$B$2)+1),
  ROWS($A$3:A3))),"")
 
Upvote 0
formula variation to allow for errors

=IFERROR(INDEX($D$2:$D$8,AGGREGATE(15,6, IF((TRIM($G$2:$G$8)="NEW CAR SALES"),ROW($G$2:$G$8)-ROW($G$2)+1),ROWS(J$1:J1))),"")
 
Upvote 0
Hi Guys

Thanks for the help, much appreciated
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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