+ Reply to Thread
Results 1 to 5 of 5

Thread: Sort spreadsheets that meet a certain condition

  1. #1
    Registered User
    Join Date
    02-14-2010
    Location
    Australia, Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Sort spreadsheets that meet a certain condition

    Hi all,
    I want to write a macro which will look at a cell in a spreadsheet (e.g.[A9]) then, based on the value in that cell, save a copy of the spreadsheet into one of two different locations.

    That is, I want to have a macro similar to the IF function:
    IF(A9=2,save a copy to folder 1, save a copy to folder 2)

    Is this possible.
    Any help greatly appreciated

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Sort spreadsheets that meet a certain condition

    Assuming you want to continue working in the active workbook rather than the "to be" saved version you could perhaps use SaveAsCopy method ?

    Sub Example()
    Dim strPath As String
    Select Case UCase(Sheets("Sheet1").Range("A9").Value)
        Case "A"
            strPath = "C:\xyz\abc\"
        Case "B"
            strPath = "C:\aaa\zys\"
    End Select
    If strPath <> "" Then ActiveWorkbook.SaveCopyAs strPath & ThisWorkbook.Name
    End Sub

    If you want to change the current file to the "to be" Saved version then use SaveAs rather than SaveCopyAs.

  3. #3
    Registered User
    Join Date
    02-14-2010
    Location
    Australia, Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Sort spreadsheets that meet a certain condition

    Thanks for that.
    So where exactly do I specify the criteria which tells it how to choose between case A and case B?
    I still don't see how its going to make a decision based on what cell A9 says

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Sort spreadsheets that meet a certain condition

    In short the sample code tests against the value of Sheet1!A9 ... if the value is "A" do x if "B" do y else do nothing.

    For a brief overview of Select Case see: http://www.ozgrid.com/VBA/select-case.htm

  5. #5
    Registered User
    Join Date
    02-14-2010
    Location
    Australia, Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Sort spreadsheets that meet a certain condition

    Works well
    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0