+ Reply to Thread
Results 1 to 4 of 4

Problem using a variable to save a workbook as a .csv

Hybrid View

  1. #1
    Registered User
    Join Date
    12-28-2018
    Location
    Batley
    MS-Off Ver
    Office 365 Win 10
    Posts
    21

    Problem using a variable to save a workbook as a .csv

    Hi there..
    I have a script that calls several macros. I have it working to the point where it saves a variable in cell M1.
    I'm trying to get it to save the workbook as a .csv using the M1 Variable. It will be a different value each time I run it.
    I am seeing subscript out of range errors. Is this because the cell has no value when this macro tries to execute?
    Sub exportcsv()
    Application.DisplayAlerts = False
    Sheets("csv").Select
    Dim LIMSnumber
    LIMSnumber = Range("M1")
    ChDir "C:\1_macros\csv"
    ActiveWorkbook.SaveAs Filename:="C:\1_macros\csv\" & LIMSnumber & ".csv", FileFormat:= _
    xlCSV, CreateBackup:=False
    ActiveWorkbook.Close (SaveChanges = True)
    End Sub
    Thank you for your help.

  2. #2
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,483

    Re: Problem using a variable to save a workbook as a .csv

    I think so if cell M1 is empty then LIMSnumber will be empty, so you are trying to save a file without a filename..

    Try testing LIMSnumber after you filled it. and let the sub end if there is no value
    Sub exportcsv()
    Application.DisplayAlerts = False
    Sheets("csv").Select
    Dim LIMSnumber
    LIMSnumber = Range("M1")
    If  Limsnumber is Nothing then exit sub
    ChDir "C:\1_macros\csv"
    ActiveWorkbook.SaveAs Filename:="C:\1_macros\csv\" & LIMSnumber & ".csv", FileFormat:= _
    xlCSV, CreateBackup:=False
    ActiveWorkbook.Close (SaveChanges = True)
    End Sub

  3. #3
    Registered User
    Join Date
    12-28-2018
    Location
    Batley
    MS-Off Ver
    Office 365 Win 10
    Posts
    21

    Re: Problem using a variable to save a workbook as a .csv

    Thanks Roel
    That doesn't help. I made a new document with just the last macro. In M1 I typed Test. Now I'm seeing runtime error 9 subscript out of range Debug identifies: Sheets("csv").Select ??

  4. #4
    Registered User
    Join Date
    12-28-2018
    Location
    Batley
    MS-Off Ver
    Office 365 Win 10
    Posts
    21

    Re: Problem using a variable to save a workbook as a .csv

    I solved it myself !!!!
    Sub saveascsv()
    Dim AC As String: AC = Sheets("Sheet1").Range("M1").Value
    
        ActiveWorkbook.SaveAs Filename:= _
        "C:\1_temp\" & AC & ".csv", FileFormat:= _
    xlCSV, CreateBackup:=False
             
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem when workbook is not save in desire sheet.
    By jp16 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-06-2018, 05:47 AM
  2. [SOLVED] Save a the same workbook but add a variable number at the end
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-15-2015, 09:33 AM
  3. Save workbook as variable/pass argument to another module
    By crossfitciaran in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-06-2015, 07:40 PM
  4. [SOLVED] problem opening workbook and assigning variable
    By fredderf81 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-14-2014, 04:55 PM
  5. Workbook Save Problem - Excel VBA
    By seanmccann_ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-12-2013, 03:03 PM
  6. Save workbook using variable for save as and for a new folder name
    By TheNewGuy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-03-2009, 10:53 AM
  7. [SOLVED] Save Workbook Problem
    By Jim Kobzeff in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-12-2005, 11:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1