Consulting

Results 1 to 2 of 2

Thread: Generating an incremented workbook file & save as variable

  1. #1

    Exclamation Generating an incremented workbook file & save as variable

    I have a big report to complete in VBA that automates the entire process of import products into our ecommerce site.

    The purchasing manager requires (and I mean demands) that we record the prices changes accordingly.

    The only problem is, I save the new report as per below:

    [vba]
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=reportLoc & Format(Time, "hh-mm_") & Format(Date, "dd-mm-yyyy") & "_Price_REPORT.csv", FileFormat:=xlCSV, CreateBackup:=False 'This saves an autoincremented file report to the destination folder

    reportWB = Application.ActiveWorkbook ' THIS REFUSES TO WORK
    [/vba]

    Saving the new generated file to a variable to pick up later on, as per the below macro.

    [vba]
    Sub comparevalues()

    Application.DisplayAlerts = False

    Dim fileLoc As String
    Dim reportLoc As String

    Dim lastCSV As String
    Dim prevCSV As String


    fileLoc = ThisWorkbook.Path & "\" 'Saves location of import macro to variable

    reportLoc = ThisWorkbook.Path & "\PriceREPORT\"

    Cells(21, 5).Select 'selects E21 (last CSV processed)
    lastCSV = ActiveCell.Value 'Saves value as variable

    Cells(22, 5).Select
    prevCSV = ActiveCell.Value

    Application.Workbooks.Open (fileLoc & "\" & lastCSV) 'Opens last generated CSV file!

    Application.Workbooks.Open (fileLoc & "\" & prevCSV) 'Opens one before the last generated to compare values!


    Workbooks.Add 'Adds a workbook (general adding like file new!)
    'filePath = ThisWorkbook.Path & "\" ' location of this file ALREADY DONE PREVIOUSLY?

    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=reportLoc & Format(Time, "hh-mm_") & Format(Date, "dd-mm-yyyy") & "_Price_REPORT.csv", FileFormat:=xlCSV, CreateBackup:=False 'This saves an autoincremented file report to the destination folder

    reportWB = Application.ActiveWorkbook


    Application.Workbooks(lastCSV).Activate

    Range("B:C").Select
    Selection.Copy

    Application.Workbooks("report.xls").Activate

    Cells(1, 1).Select

    ActiveSheet.Paste

    Application.Workbooks(lastCSV).Activate

    Range("M:M").Select
    Selection.Copy


    Application.Workbooks("report.xls").Activate

    Cells(1, 3).Select
    ActiveSheet.Paste

    Application.Workbooks(prevCSV).Activate

    Range("M:M").Select
    Selection.Copy

    Application.Workbooks("report.xls").Activate

    Cells(1, 4).Select

    ActiveSheet.Paste

    'Loop to do calculation
    Cells(3, 5).Select

    Do
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-1])"
    ActiveCell.Offset(1, 0).Select
    Loop Until IsEmpty(ActiveCell.Offset(0, -1))


    Workbooks(lastCSV).Close SaveChanges:=False
    Workbooks(prevCSV).Close SaveChanges:=False

    Call ifValue

    End Sub

    Sub ifValue()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = True

    End With

    Application.Workbooks("report.xls").Activate

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

    'We select the sheet so we can change the window view
    .Select

    'If you are in Page Break Preview Or Page Layout view go
    'back to normal view, we do this for speed
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView

    'Turn off Page Breaks, we do this for speed
    .DisplayPageBreaks = False

    'Set the first and last row to loop through
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

    'We loop from Lastrow to Firstrow (bottom to top)
    For Lrow = Lastrow To Firstrow Step -1

    'We check the values in the A column in this example
    With .Cells(Lrow, "E")

    If Not IsError(.Value) Then

    If .Value = "0" Then .EntireRow.Delete
    'This will delete each row with the Value "ron"
    'in Column A, case sensitive.

    End If

    End With

    Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
    End With

    Cells(1, 1).Select

    Workbooks("report.xls").Close SaveChanges:=True ' NEED TO INCREMENT THE FILE NAME EACH TIME ITS RUN!

    Call subCategoryMakerfindway

    End Sub
    [/vba]

    Its the fact that, I cannot seem to save the new generated file with the time stamp as a variable, does anyone know how to do that?

    Any help would be greatly appreciated thanks.

    Regards,
    Jeremy.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]
    Dim reportWB as Workbook
    set reportWB = WorkBooks(ActiveWorkbook.Name)[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •