transform a very simple macro into a udf ?

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
510
Office Version
  1. 365
Platform
  1. Windows
hi,
so i have randbetween generate random numbers,
and i need to copy the results as values only,
so i thought recording a simple macro
and modify it to an udf
like:
Excel Formula:
=CopyValOnly(a1:f1)
with a range of my choosing
so i can place the formula/udf which consistently copy the random numbers as values only
hope i explained right
can anyone help me out?

VBA Code:
Sub copyval()
'
' copy macro1
'

'
    Range("A1:D5").Select
    Selection.Copy
    Range("F1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Note that a UDF is really just a formula, and as such, can ONLY return values to the cell it resides in (like any other formula).
So you can either have a formula or a hard-coded value in a cell, but never both at the same time, and no formula (or UDF) will automatically convert itself from a formula to a hard-coded formula.
A UDF really is just a formula, so it behaves like any othe built-in Excel formula.

What you need is a Sub Procedure (aka "Macro") that you can run.
You can either have it run on the selected cells, or prompt the user to input the range.
Here is one that runs on the currently selected cells and changes to formulas to hard-coded values:
VBA Code:
Sub MyCopyValues()
    Selection.Value = Selection.Value
End Sub
 
Upvote 0
thanks joe, i'm not sure i understand correctly,
i need the 'formula' to run like MyCopyValues(a1:d5) at F1 (example) and return the same value as in a1:d5 (which is random numbers),
and what you saying it isn't possible?
i tried your macro, it worked fine on the selected range, a1:d5, but not with "MyCopyValues(a1:d5)", but also i need to keep the a1:d5 formulas intact
 
Upvote 0
ok, did a little googling and found this:


Code:
Function Copy_Only_Values(Rng As Range)

Dim Output() As Variant
ReDim Output(Rng.Rows.Count - 1, Rng.Columns.Count - 1)

For i = 1 To Rng.Rows.Count '
    For j = 1 To Rng.Columns.Count
        Output(i - 1, j - 1) = Rng.Cells(i, j)
    Next j
Next i

Copy_Only_Values = Output

End Function


thanks joe!
 
Upvote 0
ok, did a little googling and found this:


Code:
Function Copy_Only_Values(Rng As Range)

Dim Output() As Variant
ReDim Output(Rng.Rows.Count - 1, Rng.Columns.Count - 1)

For i = 1 To Rng.Rows.Count '
    For j = 1 To Rng.Columns.Count
        Output(i - 1, j - 1) = Rng.Cells(i, j)
    Next j
Next i

Copy_Only_Values = Output

End Function


thanks joe!
Have you tried it out?
Does that do what you want it to?
When I tried it out, if I changed the values in my original range, the values in the destination range seemed to change also.

I think I misunderstood your original question, but here is a solution that I came up with where the copied over values will not change when the values in the original range change:
It is a procedure that when you run, will prompt your what range you want to copy from, and where you want to paste it to:
VBA Code:
Sub MyCopyValues()
    
    Dim rng1 As Range, rng2 As Range

'   Prompt user what range to copy from
    Set rng1 = Application.InputBox( _
      Title:="Copy From Range", _
      Prompt:="Select the range you want to copy values from", _
      Type:=8)
      
'   Prompt user what range to copy to
    Set rng2 = Application.InputBox( _
      Title:="Copy To Range", _
      Prompt:="Select the FIRST cell of the range you want to copy values to", _
      Type:=8)
      
'   Make sure selection is exactly one cell
    If rng2.CountLarge <> 1 Then
        MsgBox "You have not selected a single cell in Copy To prompt", vbOKOnly, "PLEASE TRY AGAIN!"
    Else
'       Paste values
        rng1.Copy
        rng2.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    End If
      
End Sub
 
Upvote 0
Solution
thanks again joe, it's actually good,
can it be like what i wanted? to write it as a formula, instead of the need to run it from menu, and enter the range in 'external' window?
 
Upvote 0
example
test
ABCDEFGHI
17276joe's-->6656
284741471
311523567
443648172
512423275
6
7googling-->7276
88474
91152
104364
111242
test
Cell Formulas
RangeFormula
A1:D5A1=RANDBETWEEN(1,8)
F7:I11F7=Copy_Only_Values(A1:D5)
Dynamic array formulas.
 
Upvote 0
The issue is that when you use "RANDBETWEEN", anytime there is a new entry or recalculation anywhere on your sheet, all those values are re-calculated and can change.
So the method that you found on-line does NOT do a Copy/Paste Special Values. It dynamically changes as the underlying data changes (as shown in your example), where mine will copy the hard-coded values, at that particular point in time, and will NOT change as the underlying values in the original range change.

can it be like what i wanted? to write it as a formula, instead of the need to run it from menu, and enter the range in 'external' window?
I repeat, and cannot stress this enough, Excel formulas (regardless of whether they are native Excel functions or UDFs) can only return values to the cells that the formulas are placed in (keeping in mind that some new functions have the ability to "spill" over into neighboring cells). And formulas, as you can see, are NOT hard-coded values, so their values are not set in stone and can change as the values they reference change. You cannot "hard-code" any number using an Excel formula, regardless of whether it is a UDF or not.
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,210
Members
449,090
Latest member
bes000

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