Setting formula into a cell using variables

Steve001

Board Regular
Joined
Apr 13, 2017
Messages
86
Office Version
  1. 365
  2. 2021
  3. 2013
Platform
  1. Windows
Hi All,

Bit stuck with this one..

How do i set a forumla into a cell using variables

Example

=MIN(Colum, Row : Colum, last row)

I am trying to use something like this

Cells(Row, Colum).Formula = "=MIN(" & Cells(firstrow & ":" & last)

but get errors

Steve
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this:

VBA Code:
Sub testingformula()
  Dim nRow As Long, nCol As Long
  Dim firstRow As Long, lastRow As Long
 
  'Data to put the result in the cell, for example in cell B3
  nRow = 2
  nCol = 2
 
  'Data where the values for the MIN formula are found, for example range A2:A10
  firstRow = 2
  lastRow = 10
 
  Cells(nRow, nCol).Formula = "=MIN(A" & firstRow & ":A" & lastRow & ")"
End Sub

Example after run the macro:
DANTE AMOR
ABC
1VALUESMIN
253
38
48
55
64
76
89
93
107
11
Hoja2
Cell Formulas
RangeFormula
B2B2=MIN(A2:A10)





🤗
 
Upvote 0
Hi Dante,

That works, is it possable to have the "A" as a variable ?


I have tried this but i get a 1004 run time error

Cells(nRow, nCol).Formula = "=MIN(" & colum & firstRow & ":" & colum & last & ")"

Steve
 
Upvote 0
That works, is it possable to have the "A" as a variable ?


Try:

VBA Code:
Sub testingformula()
  Dim nRow As Long, nCol As Long
  Dim firstRow As Long, lastRow As Long
  Dim sCol As String
  
  'Data to put the result in the cell, for example in cell B3
  nRow = 2
  nCol = 2
  
  'Data where the values for the MIN formula are found, for example range A2:A10
  firstRow = 2
  lastRow = 10
  
  sCol = "A"
  
  Cells(nRow, nCol).Formula = "=MIN(" & sCol & firstRow & ":" & sCol & lastRow & ")"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,226,453
Messages
6,191,136
Members
453,642
Latest member
jefals

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