VBA: Variable to reference cell value, run code, then reference cell below

Seba Robles

Board Regular
Joined
May 16, 2018
Messages
73
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello everyone, so here's the information on my workbook and code;

- I have 2 worksheets; Sheet1 where I have a range of values in column A and Sheet2 where my code runs

Here's part of my code

Code:
Dim c As Integer

c = Worksheets("Sheet1").[B]Cells[/B][COLOR=#ff0000][B](16, "A")[/B][/COLOR][B].[/B]Value


Sheets("Sheet2").Select

Rows(c & ":" & c).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
Rows("3:3").Select
Selection.Copy
Rows(c & ":" & c).Select
ActiveSheet.Paste

' Then select Next cell value from List/Range in Sheet1 to place in C variable

My code simply inserts a new row within Sheet2 in row c, copies formulas from row 3, pastes them in newly inserted row c.. and what I need next is for the c variable to select the next cell value from the list I have in Sheet 1, run code again until last cell value from Sheet1 List is completed.

Maybe a For Next or Loop code would help but I don't know how to implement it.

I would appreciate any help you can give, thanks in advance!
 
Ok, how about
Code:
Sub FillProjectionData()
   Dim c As Long
   Dim c2 As Long
   Dim c3 As Long
   Dim Cl As Range
   
   For Each Cl In Worksheets("Sheet1").Range("A2", Worksheets("Sheet1").Range("A2").End(xlDown))
        c = Cl.Value
        c2 = c + 1
        c3 = c + 2
        
        With Sheets("Projection Data")
            .Rows(c).Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            .Rows("3:3").Copy .Rows(c)
            .Rows(c2).Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            .Rows(4).Copy .Rows(c2)
            .Rows(c3).Resize(14).Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            .Rows(c3).Offset(-1).Resize(15).FillDown
        End With
   Next Cl
        
End Sub
 
Upvote 0

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.
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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