how to do this in VBA??? plz help

So here is the set up of my worksheet

Name of Stock Long Inventory Short Inventory Apple 0 50 Apple 30 0 Google 10 0 Google 40 0 Google 20 0 . . .so essentially i have blocks of stocks of the same name (block sizes vary), and although MOST of them are EITHER long only OR short only, BUT some of them are BOTH long and short (ie look at apple above, one apply entry as 0 long and 50 short, while the next is like how most are (ie either long or short only)

How do I create a macro to identify the names that are BOTH long AND short...? what's making it hard is that although the same names are together, the size of the blocks of the same name is variable (ie there are 6 GS together, 11 citigroups etc, so i cant make a general formula to scan thru the blocks of holdings)

Please help anyone who knows VBA...any way to identify names that are BOTH long and short would help i.e if u can even highlight them or better yet copy and paste them to a new worksheet...thanks! .

5 Comments
 

Here's a first stab at it. Basically, just copy the entire block of values into a new sheet. Make sure that "Name of Stock" is in the top left cell of the new sheet. Then select the range which you just pasted into the new sheet. Disclaimer: I don't have Excel on my computer, so I haven't tried to run this macro...

sub deleteUnwanted() dim rangeToModify, rangeToDelete as range dim i as integer set rangeToModify = selection for i = 1 to rangeToModify.rows.count set rangeToDelete = range(activesheet.cells(1+i,1),activesheet.cells(1+i,256)) if rangeToDelete.cells(1,2) = 0 OR rangeToDelete.cells(1,3) = 0 Then rangeToDelete.delete end if next i end sub

 

Thanks a lot from your help. I ran your macro, but I'm not sure what it is deleting.. it delets stocks that are both long and short as well... and what should be the next step??

 
Best Response

This should work - it addresses the issue of numerous entries. Please note it will stop on the 1st blank cell in Column A.

Sub DeleteNotLS() Dim Loc As Double Dim Keepers As String Dim Trash As String For c = 1 To 65536 If InStr(Keepers, Range("A" & c)) = 0 And InStr(Trash, Range("A" & c)) = 0 Then If Application.WorksheetFunction.SumIf(Range("A:A"), "=" & Range("A" & c), Range("B:B")) > 0 And Application.WorksheetFunction.SumIf(Range("A:A"), "=" & Range("A" & c), Range("C:C")) > 0 Then Keepers = Keepers & Range("A" & c) & "11111" Else Trash = Trash & Range("A" & c) & "11111" End If End If If Range("A" & c) = "" Then Exit For Next c Loc = 1 For c = 1 To 65536 If InStr(Keepers, Range("A" & Loc)) = 0 Then Rows(Loc).Delete Else Loc = Loc + 1 End If If Range("A" & Loc) = "" Then Exit For Next c End Sub

Hope that helps.

 

Libero quaerat expedita in voluptatem illum necessitatibus. Dolores doloremque provident dolor qui laudantium aliquid. Cumque ut ex reiciendis quo error quo sapiente ullam.

Adipisci et iure sit dolore quo voluptatem. Dolor dolor itaque beatae expedita eveniet numquam. Atque sed perferendis impedit et omnis. Possimus quasi reprehenderit iusto in. Reprehenderit reiciendis eius dolores corrupti optio dolorum. Necessitatibus quia aliquid deleniti maiores.

Sapiente voluptates et enim natus necessitatibus dolores sit eius. Libero et architecto atque voluptate non consequatur consequatur. A est cupiditate aperiam quidem. Quaerat magnam mollitia ex natus autem. Excepturi nisi quia aut rerum ab nulla minus. Adipisci unde eum officia veniam quia.

Id ipsa debitis suscipit recusandae. Facilis et facere libero omnis quia sit occaecati. Dolor quae omnis dolor aut est aut consequuntur. Vel ratione nisi doloremque debitis illo alias cumque dicta. Accusamus error iure ad ab et quia.

Career Advancement Opportunities

September 2026 Investment Banking

  • Evercore 01 99.5%
  • Moelis & Company 01 98.9%
  • JPMorgan 01 98.4%
  • Guggenheim Partners 02 97.9%
  • Morgan Stanley 06 97.4%

Overall Employee Satisfaction

September 2026 Investment Banking

  • Moelis & Company No 99.5%
  • Morgan Stanley 02 98.9%
  • Evercore 01 98.4%
  • Banco Santander 02 97.9%
  • BMO Capital Markets 12 97.4%

Professional Growth Opportunities

September 2026 Investment Banking

  • Evercore 01 99.5%
  • Moelis & Company 01 98.9%
  • Morgan Stanley 05 98.4%
  • Goldman Sachs 01 97.9%
  • JPMorgan No 97.4%

Total Avg Compensation

September 2026 Investment Banking

  • Vice President (16) $429
  • Associates (56) $261
  • 3rd+ Year Analyst (8) $210
  • 2nd Year Analyst (28) $184
  • Intern/Summer Associate (16) $161
  • 1st Year Analyst (84) $151
  • Intern/Summer Analyst (76) $101
notes
16 IB Interviews Notes

“... there’s no excuse to not take advantage of the resources out there available to you. Best value for your $ are the...”

Leaderboard

1
redever's picture
redever
99.2
2
Secyh62's picture
Secyh62
99.0
3
kanon's picture
kanon
99.0
4
BankonBanking's picture
BankonBanking
99.0
5
GameTheory's picture
GameTheory
98.9
6
dosk17's picture
dosk17
98.9
7
CompBanker's picture
CompBanker
98.9
8
DrApeman's picture
DrApeman
98.9
9
Betsy Massar's picture
Betsy Massar
98.9
10
bolo up's picture
bolo up
98.8
success
From 10 rejections to 1 dream investment banking internship

“... I believe it was the single biggest reason why I ended up with an offer...”