Code instead of formula?

deep6

Board Regular
Joined
Apr 20, 2004
Messages
55
Hello All,

Could someone explain to me how I would write VBA code that would accomplish the same thing as the following formula?

=IF(AND(ISTEXT(A2),H2=1),"Completed",IF(AND(ISTEXT(A2),H2=0),"In Progress",""))

Ideally, this would return "Completed", "In Progress", or just be left blank in Column W, and would need to address rows 2 through 65536.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Sub macro()
Dim cell As Range
Range("w2:w" & [a65536].End(xlUp).Row).Select
Selection.ClearContents
For Each cell In Selection
If Len(cell.Offset(0, -22)) > 0 Then 'this may need to change
If cell.Offset(0, -15).Value = 1 Then cell.Formula = "Completed"
If cell.Offset(0, -15).Value = 0 And Len(cell.Offset(0, -15).Text) > 0 Then cell.Formula = "In progress"
End If
Next cell
MsgBox ("Done")
End Sub
 
Upvote 0
Thank you, tactps!

Your suggestion works perfectly. I'd like to learn something here, so could I trouble you to explain the following portion of your code? I can follow the rest of it, but the use of "Len" is a little "fuzzy" to me.
Code:
Len(cell.Offset(0, -15).Text)
Thank you again!
 
Upvote 0
This checks how many characters are in the output in the cell.

Therefore if the cell is blank, it will have 0 characters. If it is not blank, it will be >0.

This does not look to see whether it is a number or whether it is text, only that the result shown in that cell is not "".

So:
If cell.Offset(0, -15).Value = 0 And Len(cell.Offset(0, -15).Text) > 0
If you had a blank cell, its value would be zero, so I am checking whether it is zero, plus whether it not blank.
 
Upvote 0
Thank you for the explanation. Now the wheels are spinning in my head about how I could have used that approach to some other things that I've recently worked on.
 
Upvote 0
Spinning head is good - it either means you're thinking or drinking.

Good either way (lol)
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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