+ Reply to Thread
Results 1 to 4 of 4

SaveCopyAs macro

Hybrid View

silentcarl SaveCopyAs macro 03-31-2015, 12:52 PM
silentcarl Re: Saving as tab delimited... 04-01-2015, 08:09 AM
silentcarl Re: Saving as tab delimited... 04-01-2015, 08:17 AM
silentcarl Re: Saving as tab delimited... 04-01-2015, 09:02 AM
  1. #1
    Registered User
    Join Date
    03-17-2015
    Location
    South Carolina, USA
    MS-Off Ver
    2010
    Posts
    59

    SaveCopyAs macro

    Hello all, I assumed wrong what was causing the issue and had another thread changing how my values were copied to the sheet.

    My issue is that the code below is outputting a text file with many rows of blank lines after the last line of text. Is there a way to remove the extra lines when it is saved?

    Private Sub CommandButton1_Click()
    Dim sSaveAsFilePath As String
    Dim c As Range
    Dim rng As Range
    Dim sRange1 As String
        Dim sRange2 As String
        Dim sRange3 As String
        Dim sRange4 As String
        Dim sRange5 As String
        
        sRange1 = "C:\Users\" & Environ$("Username") & _
        "\Desktop\"
        sRange2 = Sheets("Calculations").Range("B2")
        sRange3 = "x"
        sRange4 = Sheets("Calculations").Range("B1")
        sRange5 = "rad.g"
        
    Dim ans As Long
    sSaveAsFilePath = sRange1 + sRange2 + sRange3 + sRange4 + sRange5
    
    Application.Sheets("Final Code").Select
    ActiveSheet.Unprotect Password:="UNLOCK"
    Application.Sheets("Final Code").Cells.ClearContents
    
    
     Application.Sheets("G Code").Select
     
     'testing code here
     
     For Each c In Sheets("G Code").Range("B1:B20000")
        If Not Len(c) = 0 Then
            If rng Is Nothing Then
                Set rng = c.Offset(, -1).Resize(1, 8)
            Else
                Set rng = Union(rng, c.Offset(, -1).Resize(1, 8))
            End If
        End If
    Next c
    
    
    
    rng.Copy Sheets("Final Code").Range("A1")
     
     'end test
      
       ' Sheets("G Code").Range("A1:H20000").Copy
        'Sheets("Final Code").Select
       ' ActiveSheet.Range("A1").Select
       ' Selection.PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
        ' Application.DisplayAlerts = False
         
        Sheets("Final Code").Copy
         
         ActiveWorkbook.SaveAs sSaveAsFilePath, xlTextWindows, _
         CreateBackup:=False
         ActiveWorkbook.Save
         ActiveWindow.Close
         Windows("Mill Radius CNC Code Generator").Activate
         Sheets("Final Code").Select
         ActiveSheet.Protect Password:="UNLOCK"
         Sheets("Calculations").Select
         
    
    Application.DisplayAlerts = True
    
    
    End Sub
    Thanks in advance!
    Last edited by silentcarl; 04-01-2015 at 08:10 AM.

  2. #2
    Registered User
    Join Date
    03-17-2015
    Location
    South Carolina, USA
    MS-Off Ver
    2010
    Posts
    59

    Re: Saving as tab delimited text file, getting many blank lines of text.

    I am so very close. Now my issue is saving the workbook. I want to use SaveCopyAs rather then SaveAs, but I get an error with the way I am using it.

    Please help!

    Private Sub CommandButton1_Click()
    Dim sSaveAsFilePath As String
    Dim c As Range
    Dim rng As Range
    Dim sRange1 As String
        Dim sRange2 As String
        Dim sRange3 As String
        Dim sRange4 As String
        Dim sRange5 As String
        
        sRange1 = "C:\Users\" & Environ$("Username") & _
        "\Desktop\"
        sRange2 = Sheets("Calculations").Range("B2")
        sRange3 = "x"
        sRange4 = Sheets("Calculations").Range("B1")
        sRange5 = "rad.g"
        
    Dim ans As Long
    sSaveAsFilePath = sRange1 + sRange2 + sRange3 + sRange4 + sRange5
    
     Application.Sheets("G Code").Select
     
     For Each c In Sheets("G Code").Range("B1:B20000")
        If Not Len(c) = 0 Then
            If rng Is Nothing Then
                Set rng = c.Offset(, -1).Resize(1, 8)
            Else
                Set rng = Union(rng, c.Offset(, -1).Resize(1, 8))
            End If
        End If
    Next c
    
    rng.Select
    Selection.Copy
    Sheets.Add.Name = "Final Code"
    Sheets("Final Code").Select
    ActiveSheet.Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    Application.DisplayAlerts = False
         
       ActiveWorkbook.SaveCopyAs sSaveAsFilePath, xlTextWindows, _
         CreateBackup:=False
         ActiveWorkbook.Save
         
         Application.ScreenUpdating = False
         ActiveWindow.SelectedSheets.Delete
         ActiveWindow.Close
         Application.DisplayAlerts = True
        Application.ScreenUpdating = True
            
        Windows("Mill Radius CNC Code Generator").Activate
    
        Sheets("Calculations").Select
         
    
    End Sub

  3. #3
    Registered User
    Join Date
    03-17-2015
    Location
    South Carolina, USA
    MS-Off Ver
    2010
    Posts
    59

    Re: Saving as tab delimited text file, getting many blank lines of text.

    I am trying to:

    1. Copy a rng
    2. create new temporary sheet
    3. Paste data to new sheet
    4. save the active sheet (i assume a copy is the right way) as txt file
    5. leave my original workbook as the active sheet
    6. delete the new temporary sheet

    If I do a simple SaveAs, it works, but the whole workbook is titled the new name and then I close it in the code and my old workbook is not open any longer.

  4. #4
    Registered User
    Join Date
    03-17-2015
    Location
    South Carolina, USA
    MS-Off Ver
    2010
    Posts
    59

    Re: Saving as tab delimited text file, getting many blank lines of text.

    Private Sub CommandButton1_Click()
    Dim sSaveAsFilePath As String
    Dim sfilename As String
    
    Dim c As Range
    Dim rng As Range
    Dim sRange1 As String
        Dim sRange2 As String
        Dim sRange3 As String
        Dim sRange4 As String
        Dim sRange5 As String
        
        sRange1 = "C:\Users\" & Environ$("Username") & _
        "\Desktop\"
        sRange2 = Sheets("Calculations").Range("B2")
        sRange3 = "x"
        sRange4 = Sheets("Calculations").Range("B1")
        sRange5 = "rad.g"
        
    Dim ans As Long
    sSaveAsFilePath = sRange1 + sRange2 + sRange3 + sRange4 + sRange5
    
    sfilename = sRange2 + sRange3 + sRange4 + sRange5
    
     Application.Sheets("G Code").Select
     
     For Each c In Sheets("G Code").Range("B1:B20000")
        If Not Len(c) = 0 Then
            If rng Is Nothing Then
                Set rng = c.Offset(, -1).Resize(1, 8)
            Else
                Set rng = Union(rng, c.Offset(, -1).Resize(1, 8))
            End If
        End If
    Next c
    
    rng.Select
    Selection.Copy
    Sheets.Add.Name = "Final Code"
    Sheets("Final Code").Select
    ActiveSheet.Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    Application.DisplayAlerts = False
         
       ActiveWorkbook.SaveAs sSaveAsFilePath, xlTextWindows, _
         CreateBackup:=False
         
         ActiveWorkbook.Save
         
         Workbooks.Open ("Mill Radius CNC Code Generator")
         
         
         Windows(sfilename).Activate
         
         Application.ScreenUpdating = False
         ActiveWindow.SelectedSheets.Delete
         ActiveWindow.Close
         Application.DisplayAlerts = True
        Application.ScreenUpdating = True
            
        Windows("Mill Radius CNC Code Generator").Activate
    
        Sheets("Calculations").Select
         
    
    End Sub
    This works.

+ 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] Saving file as tab delimited text - macro
    By Klara86 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2012, 02:02 AM
  2. Saving to a tab-delimited text file
    By BAT22_KM in forum Excel General
    Replies: 8
    Last Post: 08-24-2008, 11:43 AM
  3. Saving as a tab-delimited text file
    By wilderman22 in forum Excel General
    Replies: 2
    Last Post: 04-14-2008, 10:54 AM
  4. saving file as text(tab delimited)...
    By Sevgi in forum Excel General
    Replies: 1
    Last Post: 04-06-2005, 02:06 PM
  5. [SOLVED] saving file as text(tab delimited)...
    By Sevgi in forum Excel General
    Replies: 1
    Last Post: 04-06-2005, 02: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