+ Reply to Thread
Results 1 to 3 of 3

Selecting a Range based in Value and Save it as .CSV

Hybrid View

  1. #1
    Registered User
    Join Date
    04-19-2015
    Location
    Kihei Hawaii
    MS-Off Ver
    Microsoft 365 MSO (Version 2404 Build 16.0.17531.20190) 64-bit
    Posts
    43

    Selecting a Range based in Value and Save it as .CSV

    I have two macro that I would like to combine them into one

    The first Macro select a range of data based on the value in column J

    The second saves the entire worksheet as a csv file.

    I want only to save the selected data as csv. can this be done?

    Thank You


    Sub SelectByCellValue()
    'UpdatebyExtendoffice20161128
        Dim lastrow As Long
        Dim xRg As Range, yRg As Range
        'change Sheet1 to suit
        With ThisWorkbook.Worksheets("UPLOAD")
            lastrow = .Cells(.Rows.Count, "J").End(xlUp).Row
            Application.ScreenUpdating = False
            For Each xRg In .Range("J1:C" & lastrow)
                If UCase(xRg.Text) = "FALSE" Then
                    If yRg Is Nothing Then
                        Set yRg = .Range("A" & xRg.Row).Resize(, 5)
                    Else
                        Set yRg = Union(yRg, .Range("A" & xRg.Row).Resize(, 5))
                    End If
                End If
            Next xRg
            Application.ScreenUpdating = True
        End With
     
        If Not yRg Is Nothing Then yRg.Select
    End Sub

    Sub CopyToCSV()
    Dim MyPath As String
    Dim MyFileName As String
    'The path and file names:
    MyPath = "C:\Temp"
    MyFileName = "Journal" & Format(Date, "ddmmyyhhmmss")
    'Makes sure the path name ends with "\":
    If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
    'Makes sure the filename ends with ".csv"
    If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
    'Copies the sheet to a new workbook:
    Sheets("Upload").Copy
    'The new workbook becomes Activeworkbook:
    With ActiveWorkbook
    'Saves the new workbook to given folder / filename:
        .SaveAs Filename:= _
            MyPath & MyFileName, _
            FileFormat:=xlCSV, _
            CreateBackup:=False
    'Closes the file
        .Close False
    End With
    End Sub

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Selecting a Range based in Value and Save it as .CSV

    See if this is how you wanted.
    Sub SelectByCellValue()
    'UpdatebyExtendoffice20161128
        Dim lastrow As Long
        Dim xRg As Range, yRg As Range, MyFileName As String
        Const MyPath As String = "C:\Temp\"
        MyFileName = "Journal" & Format(Now, "ddmmyyhhmmss")
        Application.ScreenUpdating = False
        'change Sheet1 to suit
        With ThisWorkbook.Worksheets("UPLOAD")
            lastrow = .Cells(.Rows.Count, "J").End(xlUp).Row
            Application.ScreenUpdating = False
            For Each xRg In .Range("J1:C" & lastrow)
                If UCase(xRg.Text) = "FALSE" Then
                    If yRg Is Nothing Then
                        Set yRg = .Range("A" & xRg.Row).Resize(, 5)
                    Else
                        Set yRg = Union(yRg, .Range("A" & xRg.Row).Resize(, 5))
                    End If
                End If
            Next xRg
            Application.ScreenUpdating = True
        End With
        If Not yRg Is Nothing Then
            With ThisWorkbook.Sheets.Add(ThisWorkbook.Sheets(1))
                yRg.Copy .Cells(1)
                .Copy
                Application.DisplayAlerts = False
                ActiveWorkbook.SaveAs MyPath & MyFileName, xlCSV
                ActiveWorkbook.Close False
                .Delete
                Application.DisplayAlerts = True
            End With
        End If
        Application.ScreenUpdating = True
    End Sub

  3. #3
    Registered User
    Join Date
    04-19-2015
    Location
    Kihei Hawaii
    MS-Off Ver
    Microsoft 365 MSO (Version 2404 Build 16.0.17531.20190) 64-bit
    Posts
    43

    Re: Selecting a Range based in Value and Save it as .CSV

    Worked Perfectly.....Thank You!!

+ 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. [SOLVED] Selecting a Range based on known cell positions
    By im00r3 in forum Excel General
    Replies: 5
    Last Post: 04-25-2018, 09:06 PM
  2. [SOLVED] Help with selecting a range based on the value of a cell
    By jaredj34 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2014, 12:22 PM
  3. selecting a range based on name
    By ahg984 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2013, 07:41 AM
  4. Selecting a column range based
    By dems in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-09-2010, 11:47 AM
  5. Selecting Range based on integer
    By odyeiop in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-15-2009, 10:29 PM
  6. Selecting a range based on cell value
    By alpha1980 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-26-2008, 07:19 AM
  7. Selecting range based on values
    By Alan M in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2005, 02:06 PM

Tags for this Thread

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