Changing the cell referenced in a loop in VBA

mickerlad

New Member
Joined
Jun 5, 2015
Messages
2
Hi Folks

I'm wondering is it possible in VBA to write a piece of code which will keep the formula the same, but change the cell it is referencing each time it loops through?

For example, I have a sheet and wish to create a macro that will:
- Open a .csv file from a list of files in the input tab
- Copy and paste the contents into a new tab in the original spreadsheet
- Rename the tab to the name of the .csv file
- close the .csv file then move onto the next file in the list and repeat until all files are pasted in

I have the following code:

Sub Test1()

Dim x As Integer
' Set numrows = number of rows of data.
NumRows = Range("B11", Range("B11").End(xlDown)).Rows.Count
' Select cell.
Range("B11").Select
' Establish "For" loop to loop "numrows" number of times.
For x = 1 To NumRows
'Create New Tab
Sheets.Add After:=Sheets(Sheets.Count)
'Rename the tab
ActiveSheet.Name = Sheets("Sheet1").Range("B11").Value
'Opens the csv file
Dim CSV_FILE As Workbook
Set CSV_FILE = Workbooks.Open(Filename:="(filepath of csv files)" & "\" & Sheets("Sheet1").Range("B7").Value & "\" & Sheets("Sheet1").Range("B11").Value & ".csv")
'Copy all values in sheet
ActiveSheet.Cells.Select
Selection.Copy
'Paste values into new tab & clear clipboard
Windows("Macro.xlsm").ActiveSheet.Paste
Application.CutCopyMode = False
'Close spreadsheet
CSV_FILE.Close

'Selects cell down 1 row from active cell.
Range("B11").Select
ActiveCell.Offset(1, 0).Select

Next

End Sub


The value in cell 'B7' is the date which will change monthly. The list of .csv files starts at cell 'B11' in 'Sheet1' and these will be the same month on month.

So I would want the 'B11' to change to 'B12' after the first tab is created, then paste the data from the .csv referenced in 'B12', then 'B12' change to 'B13' etc and keep looping through until all the .csv files listed are pasted into new tabs.

Any help would be greatly appreciated. Thanks in advance!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi and welcome to the MrExcel Message Board,

Adding an Offset to the range should work.

Code:
Sub Test1()

    Dim x As Integer
    ' Set numrows = number of rows of data.
    NumRows = Range("B11", Range("B11").End(xlDown)).Rows.Count
    ' Select cell.
    Range("B11").Select
    ' Establish "For" loop to loop "numrows" number of times.
    
    For x = 1 To NumRows
        'Create New Tab
        Sheets.Add After:=Sheets(Sheets.Count)
        'Rename the tab
        ActiveSheet.Name = Sheets("Sheet1").Range("B11").Offset(x - 1).Value
        'Opens the csv file
        Dim CSV_FILE As Workbook
        Set CSV_FILE = Workbooks.Open(Filename:="(filepath of csv files)" & "\" & Sheets("Sheet1").Range("B7").Value & "\" & Sheets("Sheet1").Range("B11").Offset(x - 1).Value & ".csv")
        'Copy all values in sheet
        ActiveSheet.Cells.Select
        Selection.Copy
        'Paste values into new tab & clear clipboard
        Windows("Macro.xlsm").ActiveSheet.Paste
        Application.CutCopyMode = False
        'Close spreadsheet
        CSV_FILE.Close
        
        'Selects cell down 1 row from active cell.
        Range("B11").Offset(x - 1).Select
        ActiveCell.Offset(1, 0).Select
    Next

End Sub

Please use the CODE tags to post code. All you need to do is select your code then hit the "#" button, thanks.
Also, indenting it will reveal the "shape" of the code more easily.
 
Upvote 0
Hi Rick

Thanks that worked perfectly. I'll keep in mind the code tags and indentation in future!

Thanks!
 
Upvote 0

Forum statistics

Threads
1,203,101
Messages
6,053,530
Members
444,670
Latest member
laurenmjones1111

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