I'm trying to put a formula in a cell thru VBA

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
I am trying to put a formula in a cell through VBA. I was trying to use what I learned here, but I keep getting an error. Any help would be appreciated. My code follows. (gen_vlookup is a udf similar to vlookup).

It is bombing on the last line.

fs = "gen_vlookup(E" & far & ",customers,2,5)+0.5*(gen_vlookup(E " & far & ",customers,2,9)"
fs = fs & "*--(gen_vlookup(E" & far & ",customers,2,10)<=A" & far & "))"

'Put formula into proper place
'Sheets("Checking").[d & far].Formula = fs
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
1. What's the formula supposed to look like? Doesn't it start with an equals sign? You have to include it.

fs = "=gen_lookup(E" & blah blah

2. I wish MS never allowed the use of [ ] as a shortcut to properly referencing objects. I assume 'far' in brackets is a numerical variable; does 'd' stand for column D? Also, use Worksheets instead of Sheets. I know it takes more typing, but you should really do it correctly:

ActiveWorkbook.Worksheets.Range("D" & far).Formula = fs
 
Upvote 0
Jon, I tried what you said. It is still bombing on the last line - the error is "object doesn't support property or method. Yes far is a numerical variable. I have included all the code (it isn't really a lot) this time. Thanks for your continued help.


Private Sub CommandButton1_Click()
Dim far As Double 'far = First Available Row
Dim fs As String 'fs = Formula String


'Get First empty row, Use Date Column (A) since it will always be the last row used
'(not true with other columns like debit and credit)
With Sheets("checking")
far = .Range("A" & .Rows.Count).End(xlUp).row + 1
End With

'Enter today's date in Date Column
Range("a" & far).Value = Now

'Copy formula for Balance down in balance column (B)
Range("B" & far - 1).Copy
Range("B" & far).Select
ActiveSheet.Paste

'Create formula string. We can't just copy credit down, because there may not be a
'formula in the last cell (i.e. row # far-1).
fs = "=gen_vlookup(E" & far & ",customers,2,5)+0.5*(gen_vlookup(E " & far & ",customers,2,9)"
fs = fs & "*--(gen_vlookup(E" & far & ",customers,2,10)<=A" & far & "))"

'Put formula into proper place
ActiveWorkbook.Worksheet.Range("checking!d" & far).Formula = fs
End Sub
 
Upvote 0
Change this:

ActiveWorkbook.Worksheet.Range("checking!d" & far).Formula = fs

to this:

ActiveWorkbook.Worksheets("checking").Range("d" & far).Formula = fs

My bad, I left out the sheet name in my earlier post.
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,793
Members
448,994
Latest member
rohitsomani

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