VBA Populate column with formula

Rymare

New Member
Joined
Apr 20, 2018
Messages
37
I have a dynamic range, it changes all the time, and every time a value looks like so: #####-### (or in my formula I put ?????-???), I need this formula: =IF(COUNTIF(A2, "?????-???")>0,MID(A2,5,1),"") to be placed in the corresponding row in column C, because I need to extract the 5th number in the pattern (it corresponds to a year). I need "A2" to automatically adjust. I've attempted to hit "Record macro" which I'm generally loathe to do and then edit the corresponding code by inputting the dynamic range instead of the static one the record button usually gives. That didn't work. So far, this is what I've come up with and it also doesn't work. At all. Not even a pity paste into any of the cells, it just runs and stays blank. I know that this can be done manually but for the purposes of this excel file--nothing can be manual, it needs to be programmatic.

Code:
Sub comparedate()

Dim r_Sheet As Worksheet: Set r_Sheet = Sheets("Review")
Dim Lastrow As Integer
Dim rng As Range
Dim r As Variant
Dim form As String




Lastrow = r_Sheet.Range("A" & Rows.Count).End(xlUp).Row
Set rng = r_Sheet.Range("A2:A" & Lastrow)
For Each r In rng
    form = "=IF(COUNTIF(" & r_Sheet.Range("A" & r.Row).Value & ", ""?????-???"")>0,MID(" & r_Sheet.Range("A" & r.Row).Value & ",5,1),"""")"
    If r.Value <> "" Then
        r.Offset(0, 3).Value = form
    End If
Next r






End Sub

Any help is greatly appreciated!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Since you are using VBA, you don't really need the following.
Code:
form = "=IF(COUNTIF(" & r_Sheet.Range("A" & r.Row).Value & ", ""?????-???"")>0,MID(" & r_Sheet.Range("A" & r.Row).Value & ",5,1),"""")"

Can't you simply use:

Code:
IF COUNTIF(r_Sheet.Range("A" & r.Row).Value,"?????-???")>0 THEN
form = MID("A"&r.Row,5,1)
END IF
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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