How to write loop to fill the values?

dmadhup

Board Regular
Joined
Feb 21, 2018
Messages
146
Office Version
  1. 365
Hi,

I need help to convert following code with any loop. Can I get any help, please.
thank you

Code:
Dim lastR As Long
lastR = Cells(Rows.count, "N").End(xlUp).Row - 5
Range("O6").Resize(lastR) = "=IF(N6<>"""",N6)"
Range("P6").Resize(lastR) = "=IF(N6<>"""",N6)"
Range("Q6").Resize(lastR) = "=IF(N6<>"""",N6)"
Range("R6").Resize(lastR) = "=IF(N6<>"""",N6)"
Range("S6").Resize(lastR) = "=IF(N6<>"""",N6)"
Range("T6").Resize(lastR) = "=IF(N6<>"""",N6)"
Range("U6").Resize(lastR) = "=IF(N6<>"""",N6)"
 

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).
You do not need a loop to do what that code does; the following should do the same thing
Code:
[table="width: 500"]
[tr]
	[td]Dim lastR As Long
lastR = Cells(Rows.Count, "N").End(xlUp).Row - 5
Range("O6").Resize(lastR, 7) = "=IF(N6<>"""",N6)"[/td]
[/tr]
[/table]
 
Upvote 0
Your question isn't that clear but it sounds like you want to loop instead of having a line of code for each column:

Code:
Dim lastR As Long
Dim C As Long 

lastR = Cells(Rows.count, "N").End(xlUp).Row - 5

For C = Cells(1,"O").Column To Cells(1,"U").column
   Cells(6, C).Resize(lastR) = "=IF(N6<>"""",N6)"
Next C
 
Upvote 0
Thank you both of you!

I was trying to minimize the lines of code. Considering that I prefer to use Rick answer.
 
Upvote 0
I was trying to minimize the lines of code. Considering that I prefer to use Rick answer.
If you want to minimize lines of code, you can replace what I posted earlier with this single line of code...
Code:
[table="width: 500"]
[tr]
	[td]Range("O6").Resize(Cells(Rows.Count, "N").End(xlUp).Row - 5, 7) = "=IF(N6<>"""",N6)"[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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