Running counter and referencing adjacent column

shutterback

New Member
Joined
Sep 15, 2017
Messages
3
I have a dataset that looks like this for 1400 rows:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Buy[/TD]
[TD]273[/TD]
[/TR]
[TR]
[TD]Buy[/TD]
[TD]263[/TD]
[/TR]
[TR]
[TD]Sell_short[/TD]
[TD]340[/TD]
[/TR]
[TR]
[TD]Sell_long[/TD]
[TD]209[/TD]
[/TR]
[TR]
[TD]Buy[/TD]
[TD]271[/TD]
[/TR]
</tbody>[/TABLE]

When the left column is buy, the right column gets added to the account. When the left column is sell_short or sell_long, the right column gets detracted from the account. When the left column is sell_long, the counter after selling should be a positive number, and when the left column says sell_short, the counter after selling should be a negative number. I want to run a for loop through all of the rows, adding to the counter when left is buy and detracting when left is sell, and if the counter is negative but the left column says long, highlight the cell in red. If the counter is positive and the left column says short, highlight the cell in red. Otherwise, do nothing.

So far, I have

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 ; background-color: #fffb00}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; background-color: #ffffff ; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 ; background-color: #ffffff }p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #011993 ; background-color: #ffffff }span.s1 {color: #011993 }span.s2 {color: #000000 }</style>Sub counter()


Dim x As Range
Dim counter As Integer
counter = 0


For Each x In Range("D2", Range("D" & Rows.Count).End(xlUp))
If x.Offset(0, -1) = "Buy" Then
counter = counter + x
ElseIf x.Offset(0, -1).Value Like "*Sell*" Then
counter = counter - x
End If
Next x




End Sub






Need advice for where to go from here!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Welcome to Mr Excel

Maybe this
You didn't say where you want the results, so i assumed results in column E.

Code:
Sub counter()
    Dim x As Range, counter As Long
    
    counter = 0
    For Each x In Range("D2", Range("D" & Rows.Count).End(xlUp))
        If x.Offset(0, -1) = "Buy" Then
            counter = counter + x
        ElseIf x.Offset(0, -1).Value Like "*Sell*" Then
            counter = counter - x
            If InStr(1, x.Offset(, -1), "long", vbTextCompare) And counter < 0 Then
                x.Offset(, 1).Interior.Color = vbRed
            ElseIf InStr(1, x.Offset(, -1), "short", vbTextCompare) And counter > 0 Then
                x.Offset(, 1).Interior.Color = vbRed
            End If
        End If
        x.Offset(, 1) = counter
    Next x
End Sub

Hope this helps

M.
 
Upvote 0
Thanks so much for your help Marcelo!
This is perfect. I'm now trying to make a new column called Error and just saying yes in the rows that contain an error, instead of doing the highlighting. Don't do it for me, but could you tell me how I can print "yes" in x.offset(,5) in the error rows? Thanks so much, first day doing VBA.
 
Upvote 0
Welcome to Mr Excel

Maybe this
You didn't say where you want the results, so i assumed results in column E.

Code:
Sub counter()
    Dim x As Range, counter As Long
    
    counter = 0
    For Each x In Range("D2", Range("D" & Rows.Count).End(xlUp))
        If x.Offset(0, -1) = "Buy" Then
            counter = counter + x
        ElseIf x.Offset(0, -1).Value Like "*Sell*" Then
            counter = counter - x
            If InStr(1, x.Offset(, -1), "long", vbTextCompare) And counter < 0 Then
                x.Offset(, 1).Interior.Color = vbRed
            ElseIf InStr(1, x.Offset(, -1), "short", vbTextCompare) And counter > 0 Then
                x.Offset(, 1).Interior.Color = vbRed
            End If
        End If
        x.Offset(, 1) = counter
    Next x
End Sub

Hope this helps

M.


Thanks so much for your help Marcelo!
This is perfect. I'm now trying to make a new column called Error and just saying yes in the rows that contain an error, instead of doing the highlighting. Don't do it for me, but could you tell me how I can print "yes" in x.offset(,5) in the error rows? Thanks so much, first day doing VBA.
 
Upvote 0
Thanks so much for your help Marcelo!
This is perfect. I'm now trying to make a new column called Error and just saying yes in the rows that contain an error, instead of doing the highlighting. Don't do it for me, but could you tell me how I can print "yes" in x.offset(,5) in the error rows? Thanks so much, first day doing VBA.

Maybe...
Instead of
x.Offset(, 1).Interior.Color = vbRed

try
x.Offset(, 5).Value = "Yes"

M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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