Loop through range. Use range data as input

cemvula

New Member
Joined
Dec 21, 2017
Messages
4
I am struggling to add further functionality to my code. I would like for my code to run a loop through a range of dates, found in column "E:E", and thereby use this input into cell C2 and then perform the rest of my code - saving the file to CSV (SaveAsCSV()). The spreadsheet is such that it already xlookups information in various sheets to "Sheet1" it just requires that the date, in C2 found on my "input" sheet, be amended.

VBA Code:
Sub SaveAsCSV()
Dim strSourceSheet As String
Dim strFullname As String



strSourceSheet = "Sheet1"
strFullname = "\\H:\Database\Upload Files\"
myfilenamedate = Format(Range("C2"), "yyyyMMdd")
myfilenameindicator = "x"

ThisWorkbook.Sheets(strSourceSheet).Copy
ActiveWorkbook.SaveAs Filename:=strFullname & myfilenameindicator & myfilenamedate & ".csv", _
                      FileFormat:=xlCSV, _
                      CreateBackup:=True, _
                      local:=True
ActiveWorkbook.Close

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about:

VBA Code:
Sub SaveAsCSV()
  Dim strSourceSheet As String
  Dim strFullname As String
  Dim myfilenamedate As String
  Dim myfilenameindicator As String
  Dim sh As Worksheet
  Dim i As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set sh = Sheets("Input")
  strSourceSheet = "Sheet1"
  strFullname = "\\H:\Database\Upload Files\"
  For i = 2 To sh.Range("E" & Rows.Count).End(3).Row
    sh.Range("C2").Value = sh.Range("E" & i).Value
    myfilenamedate = Format(sh.Range("C2"), "yyyyMMdd")
    myfilenameindicator = "x"
    
    ThisWorkbook.Sheets(strSourceSheet).Copy
    ActiveWorkbook.SaveAs Filename:=strFullname & myfilenameindicator & myfilenamedate & ".csv", _
      FileFormat:=xlCSV, CreateBackup:=True, local:=True
    ActiveWorkbook.Close False
  Next
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
How about:

VBA Code:
Sub SaveAsCSV()
  Dim strSourceSheet As String
  Dim strFullname As String
  Dim myfilenamedate As String
  Dim myfilenameindicator As String
  Dim sh As Worksheet
  Dim i As Long
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  Set sh = Sheets("Input")
  strSourceSheet = "Sheet1"
  strFullname = "\\H:\Database\Upload Files\"
  For i = 2 To sh.Range("E" & Rows.Count).End(3).Row
    sh.Range("C2").Value = sh.Range("E" & i).Value
    myfilenamedate = Format(sh.Range("C2"), "yyyyMMdd")
    myfilenameindicator = "x"
   
    ThisWorkbook.Sheets(strSourceSheet).Copy
    ActiveWorkbook.SaveAs Filename:=strFullname & myfilenameindicator & myfilenamedate & ".csv", _
      FileFormat:=xlCSV, CreateBackup:=True, local:=True
    ActiveWorkbook.Close False
  Next
 
  Application.ScreenUpdating = True
End Sub

Will attempt it this evening and will let you know how it goes. Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,051
Members
452,542
Latest member
Bricklin

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