Inserting formula into cell using vba

arehman1289

New Member
Joined
Dec 10, 2020
Messages
20
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
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,108
Try:
VBA Code:
Cells((nr + k), "D").Formula = "=VLOOKUP('User Input'!$C" & nr + k & ",'Work process list'!$A$2:$B$11,2,FALSE)"
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

arehman1289

New Member
Joined
Dec 10, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
thank you so much. it works and forgive me for my amatuer questions
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Glad we could help & thanks for the feedback.
 

arehman1289

New Member
Joined
Dec 10, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,898
Office Version
  1. 365
Platform
  1. Windows
As this is a totally different question, you will need to start a new thread. Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,122,456
Messages
5,596,234
Members
414,048
Latest member
wnied1

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
Top