vba query

Johnsmith999

New Member
Joined
Dec 27, 2018
Messages
3
I use the following formula to extract postcodes from addresses and it works perfectly.

TRIM(RIGHT(SUBSTITUTE(A2,"",REPT(" ",10)),20))<o:p></o:p>

However, when I insert the same formula in a vba loop, it does not allow me to execute. REPT is highlighted and the dialogue box says "Compile error: Sub or Function not defined"

Does anyone have a solution? the macro I have written is pasted below:

Sub tob_report()


Dim r As Integer


r = 2


Do Until Cells(r, 1) = ""


Columns(2).Select


Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove


Cells(1, 2).Select


ActiveCell.Value = "Postcode"


Cells(2, 2).Select


ActiveCell.Value = Application.WorksheetFunction.Trim(Right(Substitute(Cells(2, 1), "", Rept("", 10)), 20))


r = i + 1


Loop


End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How about
Code:
Sub JohnSmith999()
   Columns(2).Insert xlToRight
   Range("B1").Value = "Postcode"
   With Range("B2", Range("A" & Rows.Count).End(xlUp).Offset(, 1))
      .Value = Evaluate(Replace("If({1},TRIM(RIGHT(SUBSTITUTE(@,"""",REPT("" "",10)),20)))", "@", .Offset(, -1).Address))
   End With
End Sub
Although I think that there is something wrong with the formula you posted.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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