Inserting formula into cell using vba

arehman1289

New Member
Joined
Dec 10, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hello

Im trying to insert a formula in a cell, with a mix of strings and integer values but am unsuccessfull.

nr = 41
For k = 1 To 21
If k = 3 Then
Cells((nr + k), "D").Value = "VLOOKUP('User Input'!$C(nr+k),'Work process list'!$A$2:$B$11,2,FALSE)"
End If
Next k

All it does is paste "VLOOKUP('User Input'!$C(nr+k),'Work process list'!$A$2:$B$11,2,FALSE)" as a string.

In this part of the formula C(nr+k)

I would like it to iterate through the rows of the C Column of the worksheet User Input.

Please help
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try:
VBA Code:
Cells((nr + k), "D").Formula = "=VLOOKUP('User Input'!$C" & nr + k & ",'Work process list'!$A$2:$B$11,2,FALSE)"
 
Upvote 0
It should be
VBA Code:
Cells(nr + k, "D").Formula = "=VLOOKUP('User Input'!$C" & nr + k & ",'Work process list'!$A$2:$B$11,2,FALSE)"
But the loop is irrelevant as it will only put that formula in D44
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0
I am writing a basic code as:

Range("A20:E40").Copy Cells(nr, "A")
Cells(nr, "A").Interior.ColorIndex = 35
Cells(nr, "D").Formula = "bla"

In this case line 3 isnt executed, but when I change nr in this line to an integer it works as:

Range("A20:E40").Copy Cells(nr, "A")
Cells(nr, "A").Interior.ColorIndex = 35
Cells(43, "D").Formula = "bla"

It is quite strange since the variable nr has been declared globally and if this were the problem with the variable the first two lines wouldnt execute. What could it be ?

Thanks
 
Upvote 0
As this is a totally different question, you will need to start a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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