Use a row number (not entire row) in excel formula via VBA

ZeGekko

New Member
Joined
Apr 28, 2013
Messages
11
Hello.I have two columns of data. The first one has empty cells, the second one is full. What I am trying to do is check if the first (H) column's n-th row is empty or not. If it is, do nothing in the third (J) column (leave it blank or just assign zero). If the cell is not empty, in the third (J) column subtract the first (H) column's value from the second (I) column's. This is how it looks like:H I J1,8166 0,7055475 -1,1110524 0,5334240 0,5795186 1,8545 0,2895624 -1,5649375 0,3019480 1,8814 0,7747401 -1,1066599
Code:
Sub op()    Dim r As Integer         For r = 4 To Cells(Rows.Count, 7).End(xlUp).Row        If Not IsEmpty(Range("H" & r)) Then        Range("J" & r).Formula = "=(I&r - H&r)"        Else        If IsEmpty(Range("H" & r)) Then Range("J" & r) = ""        End If    Next rEnd Sub
When I run the above code, r is taking the entire row. For example, if it is 5th row, in the formula it is showing up as 5:5.
Code:
=(I&4:4 - H&4:4)
Can you help me out please? Thanks.
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

ZeGekko

New Member
Joined
Apr 28, 2013
Messages
11
I am terribly sorry about the text format. I have tried to edit it out, but it is not changing.
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,376
Try this...
Code:
[COLOR=darkblue]Sub[/COLOR] op()
    [COLOR=darkblue]Dim[/COLOR] r      [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    [COLOR=darkblue]For[/COLOR] r = 4 [COLOR=darkblue]To[/COLOR] Cells(Rows.Count, "I").End(xlUp).Row
        [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] IsEmpty(Range("H" & r)) [COLOR=darkblue]Then[/COLOR]
            Range("J" & r).FormulaR1C1 = "=RC[-2]+RC[-1]"
        [COLOR=darkblue]Else[/COLOR]
            Range("J" & r) = ""
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] r
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Or better yet...
Code:
[COLOR=darkblue]Sub[/COLOR] op2()
    [COLOR=darkblue]With[/COLOR] Range("J4:J" & Cells(Rows.Count, "I").End(xlUp).Row)
        .FormulaR1C1 = "=IF(RC[-2]="""","""",RC[-2]+RC[-1])"
        .Value = .Value
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 

ZeGekko

New Member
Joined
Apr 28, 2013
Messages
11
AF, awesome! Thanks for the help.By the way, do you know what the issue is with the text format?
 

Watch MrExcel Video

Forum statistics

Threads
1,122,233
Messages
5,594,963
Members
413,954
Latest member
mrsandy

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
Top