Using UDF in VBA with arrays

Vonsteiner

New Member
Joined
Apr 14, 2014
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I am having some trouble using a UDF in my VBA code. I am attempting to enter a UDF into a cell within my workbook. Hopefully it is an easy fix. I am not sure if I have entered it into VBA correctly. Here are some snippets of the code I am trying. In the following I am entering some formulas in D and E and attempting to enter the UDFs 'Direction' and 'Street_Name' into F & G. The formulas in D & E work fine, but the UDF lines don't enter anything into the worksheet at all.

VBA Code:
Range("D2").Select
ActiveCell.FormulaR1C1 = "=TRIM(LEFT(RC[-2],FIND("":"",RC[-2])-1))"
Range("D2").Select
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
Range("B2").Select
Selection.End(xlDown).Select
Selection.Offset(0, 3).Select
ActiveCell.FormulaR1C1 = "=TRIM(LEFT(RC[-1],FIND("" "",RC[-1])-1))"
Selection.Copy
Range(Selection, Selection.End(xlUp)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
Range("E1").Value = "House Number"
    
Range("F2").Select
Selection.FormulaArray = "=Direction(RC[-2])"
    Selection.Copy
    Range("F3").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
Range("G2").Select
Selection.FormulaArray = "=Street_Name(RC[-3])"
    Selection.Copy
    Range("G3").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

I have more code to show if needed, but it is mostly the same as this. Any help is greatly appreciated. Thank you.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
UDF's aren't suited for what you are trying to do. You might take a look over here...

 
Upvote 0
I did a test and it works for me.
You can put here the code of the Direction and Street_Name UDFs
By the way, if you are already running a macro to put a UDF, it would be better if the same macro got the results that the UDFs get.


You could explain the following code, because if there is no data in column D the formula is copied to ALL column
VBA Code:
Range("D2").Select
ActiveCell.FormulaR1C1 = "=TRIM(LEFT(RC[-2],FIND("":"",RC[-2])-1))"
Range("D2").Select
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
 
Upvote 0
Solution

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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