VBA IF Help plz !!

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

My if formula is not working plz suggest where its going wrong, and
also, if any other solution then also i am ok. Thanks.

Sub Test()
Dim lr As Long
Dim i As Integer
Dim ws As Worksheet
<strike></strike>

Set ws = ThisWorkbook.Worksheets("sheet1")
lr = ws.Range("A1000").End(xlUp).Row

For i = 1 To lr
ws.Cells(i + 1, 2).Formula = "=if(ws.cells(i+1,2)>5500,""New"",""Historic"")"
'ws.Range("a2:a" & lr).Formula = "=if(ws.cells(i+1,2)>5500,""New"",""Historic"")"

Next i
End Sub




Regards
Mallesh
<strike></strike>
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Please walk us through an actual example of what SHOULD happen.
So, based on your loop, the first iteration is for row 1.
As we loop through for 1, what formula should be placed in what cell?
 
Upvote 0
well one thing i notice is you are writing vba/excel hybrid code :) and attempting to set it to cell values...

Code:
"=if(ws.cells(i+1,2)>5500,""New"",""Historic"")"
that wont work in excel

Code:
"=if(" & ws.cells(i+1,2).Address & ">5500,""New"",""Historic"")"
that might work, try it out
 
Last edited:
Upvote 0
Since you are applying the same formula to a continuous range, this can be done pretty easily without any loops.
I just need to know exactly what the formula is, and how it is related to the cell it is going on.
I think there is flawed logic in the original attempt, as it looks like it would create a circular reference (trying to put a formula in Cells(i + 1, 2) that references Cells(i + 1, 2)).
That is why I want clarification as to what the formula should look like and what cell it goes in.

 
Upvote 0
Joe/cerafani

Thanks for looking in my problem,

Joe:
I have 40000+ number in Column A to compare with one specific number
those 40000 numbers are actually dates, comes in converted number format like 41640 (1-Jan-14)

I Want the result in
sheet1
Column B2:B40200,
My Formula in excel it works=IF(A2>41640,"New","Historic") and I fill down the formula.


Cefafani:

result is showing 0 value in Column B
ws.Cells(i + 1, 2).Formula = "=if(" & ws.Cells(i + 1, 2).Address & ">5500,""New"",""Historic"")"

Thanks again for your precious time !

Thanks
Regards,
Mallesh
 
Upvote 0
Try this in a copy of your workbook
Code:
Sub Test2()
  Dim lr As Long
  Dim ws As Worksheet
  
  Set ws = ThisWorkbook.Worksheets("sheet1")
  With ws
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("B2:B" & lr).Formula = "=IF(A2>41640,""New"",""Historic"")"
  End With
End Sub
 
Upvote 0
Hey Mallesh23, I just noticed the formula I tried to fix for you is a circular reference... the cell formula you are setting is referencing itself ;)
 
Upvote 0
Peter,

I used your code, it worked awsome and also thanks to all for your help and suggestion.

Regards,
Mallesh
 
Upvote 0
Peter,

I used your code, it worked awsome and also thanks to all for your help and suggestion.

Regards,
Mallesh
You are welcome, though I was just implementing what Joe was alluding to. :)

Since you are applying the same formula to a continuous range, this can be done pretty easily without any loops.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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