How do I create simple arithmetic in VBA?

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
Hi Guys,
I want to make a VBA loop until the value in column B is blank.
I want it to do this calculation in K only if there is a value in column F:

if G+ H - I < .5 then "yes"
if G + H - I not < .5 then "no"

I could maybe work out the loop, but I don't know how to do simple maths in vba! Lame... i know.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello oliviar,

Here is one method:
Code:
Sub LoopMacro()

  Dim Cell As Range
  Dim Rng As Range
  
    Set Rng = Range("B1", Cells(Rows.Count).End(xlUp))
    
      For Each Cell In Rng
        If Cell.Offset(0, 4) <> "" Then
          If Cell.Offset(0, 6) + Cell.Offset(0, 7) - Cell.Offset(0, 8) < 0.5 Then
             Cell.Offset(0, 10) = "yes"
          Else
             Cell.Offset(0, 10) = "no"
          End If
        End If
      Next Cell
      
End Sub
Sincerely,
Leith Ross
 
Upvote 0
I get an error that says type mismatch on this line:


Code:
  If Cell.Offset(0, 6) + Cell.Offset(0, 7) - Cell.Offset(0, 8) < 0.5 Then

:(
 
Upvote 0
Hello Oliviar,

I think I counted my offsets wrong. Try this...
Code:
Sub LoopMacro()

  Dim Cell As Range
  Dim Rng As Range
  
    Set Rng = Range("B1", Cells(Rows.Count).End(xlUp))
    
      For Each Cell In Rng
        If Cell.Offset(0, 4) <> "" Then
          If Cell.Offset(0, 5) + Cell.Offset(0, 6) - Cell.Offset(0, 7) < 0.5 Then
             Cell.Offset(0, 9) = "yes"
          Else
             Cell.Offset(0, 9) = "no"
          End If
        End If
      Next Cell
      
End Sub
Sincerely,
Leith Ross
 
Upvote 0
Hello Oliviar,

The only reason I can think of why VBA would object is the cells don't contain number values. If the values are text then they need to be converted, like below..
Code:
      For Each Cell In Rng
        If Cell.Offset(0, 4) <> "" Then
          If CDbl(Cell.Offset(0, 5)) + CDbl(Cell.Offset(0, 6)) - CDbl(Cell.Offset(0, 7)) < 0.5 Then
             Cell.Offset(0, 9) = "yes"
          Else
             Cell.Offset(0, 9) = "no"
          End If
        End If
      Next Cell
Sincerely,
Leith Ross
 
Upvote 0
They are dates and times, but they are valid according to excel, and I don't care how they are represented in the end. I am trying to find the values that are separated by less than half a day, hence the .5
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

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