• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Whats wrong in my code

Hi Friends,

I am trying to run a formula from column m2 to the last known row in that date.
while doing so the following error is being prompted ...

Apprecaite your help on the same.

upload_2017-9-2_12-16-4.png
 

Attachments

  • sample.xlsm
    20.7 KB · Views: 3
You were close

try the following code

Code:
Sub fry3()
Range("A2").Select
Selection.End(xlDown).Select
lastrow = ActiveCell.Row
For i = 2 To lastrow
  Sheets("Sheet1").Range("M" & CStr(i)).Formula = "=IF(MEDIAN(TODAY()-WEEKDAY(TODAY()-2)-7,H" & CStr(i) & ",TODAY()-WEEKDAY(TODAY()-2)-7+5)=H" & CStr(i) & ",""Last Week"","""")"
Next i
End Sub
 
First thing, you don't need a loop for this. Here's how I do it.
- Apply formula in Excel as normal.
- Press F2 and copy formula to Visual Basic Editor.
- Select Text and then do Find (") and replace("") with option "Selected Text"
- And then wrap it with quotes.
Code:
Public Sub ApplyFormula()
Range("M2:M" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=IF(MEDIAN(TODAY()-WEEKDAY(TODAY()-2)-7,H2,TODAY()-WEEKDAY(TODAY()-2)-7+5)=H2,""Last Week"","""")"
End Sub
 
Back
Top