Inserting formula by button

tyr443

New Member
Joined
Mar 4, 2016
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to add a formula to a newly created cell via button using the below:




Private Sub CommandButton1_Click()
Dim rowNum As Integer
On Error Resume Next
rowNum = Application.InputBox(Prompt:="Enter Row Number where you want to add a row:", _
Title:="Int Invoice", Type:=1)
Rows(rowNum & ":" & rowNum).Insert Shift:=xlDown

Sheets("Sheet2").Range("c" & rowNum).Select
ActiveCell.Formula = "=VLOOKUP(A" & rowNum & ",Sheet1!$A$2:$B$23,2,FALSE)"

Sheets("Sheet2").Range("d" & rowNum).Select
ActiveCell.Formula = "=ROUNDDOWN(IF(B" & rowNum & "<>"",LEN(SUBSTITUTE(B" & rowNum & ",", ",""))/5)*(VLOOKUP(A" & rowNum & ",Sheet1!$A$2:$C$23,3,FALSE)),0)"

End Sub
at this point:



ActiveCell.Formula ="=ROUNDDOWN(IF(B" & rowNum & "<>"",LEN(SUBSTITUTE(B" & rowNum & ",", ",""))/5)*(VLOOKUP(A" & rowNum & ",Sheet1!$A$2:$C$23,3,FALSE)),0)"

It tells me there is a compile error "expected end of statement".

Can anyone see what I am doing wrong?

When I get it to work without a compile error the formula doesn't go into the cell.

Thanks for any help
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You need to double up all the quotes in the formual like
VBA Code:
ActiveCell.Formula = "=ROUNDDOWN(IF(B" & RowNum & "<>"""",LEN(SUBSTITUTE(B" & RowNum & ","", "",""""))/5)*(VLOOKUP(A" & RowNum & ",Sheet1!$A$2:$C$23,3,FALSE)),0)"
 
Upvote 0
Thanks so much, I have another question if you don't mind.

How would I change the fill colour using the above button for one of the particular cells?

I have the hex code for the colour: #F2F2F2

Thanks again!
 
Upvote 0
Maybe
VBA Code:
activecell.Interior.Color= 15921906
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

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