Consulting

Results 1 to 8 of 8

Thread: How to export part of a sheet

  1. #1
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location

    How to export part of a sheet

    Hi
    I've already learnt how to export a sheet to a new workbook and email it.
    But What I wish to do is to export only the first 25 rows.

    What changes do I need to do in the code below?

    [VBA]Private Sub CommandButton4_Click()

    msg = "Do you wish to create a new workbook?"
    If MsgBox(msg, vbQuestion + vbYesNo, "ATTENTION") = vbNo Then
    Exit Sub
    End If

    Dim sFile As String
    Dim avLink As Variant
    Dim vLink As Variant
    Dim strDate As String

    strDate = Format(Date, "dd mmmm yyyy") & " " & Format(Time, "hh:mm:ss")
    Worksheets(Array("MONTH RESUME")).Copy

    With ActiveWorkbook
    sFile = Application.GetSaveAsFilename( _
    FileFilter:="Excel Files (*.xls), *.xls", _
    Title:="Specify Location for Copy:")

    If sFile <> "False" Then
    avLink = .LinkSources(xlExcelLinks)
    For Each vLink In avLink
    .BreakLink Name:=vLink, Type:=xlLinkTypeExcelLinks
    Next vLink

    .SaveAs Filename:=sFile
    End If
    .SendMail "abcde@gmail.com", _
    "MONTH REPORT" & " " & strDate
    .Close SaveChanges:=False
    End With
    End Sub[/VBA]

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not export the whole sheet, and delete anything past row 25? Seems the simplest way to me.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Not well tested, but try tacking in:
    [vba]
    Sheets(1).Rows("26:" & Rows.Count).Delete
    [/vba]
    ...right above .SaveAs ...

    Hope that helps,

    Mark

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO
    Not well tested, but try tacking in:
    [vba]
    Sheets(1).Rows("26:" & Rows.Count).Delete
    [/vba]
    ...right above .SaveAs ...

    Hope that helps,

    Mark
    Don't you mean Rows.Count - 25?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location
    Sorry, I forgot a relevant point:
    Three Sheets must be exported but only one will export a part ("MOVEMENTS")

    [VBA]Worksheets(Array("MONTH RESUME", "MOVEMENTS", "BASE")).Copy
    [/VBA]

    How do I implement suggestions above? Just as the below?

    [VBA]Dim ws1, ws2, ws3 As Worksheet
    ws1=Worksheets("MONTH RESUME")
    ws2=Worksheets("MOVEMENTS")
    ws3=Worksheets("BASE")
    ...
    Worksheets(Array(ws1, ws2.Rows.Count-25, ws3)).Copy
    ...
    [/VBA]

    Thanks

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Delete the part from just the one, what is the issue?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Contributor
    Joined
    Mar 2009
    Location
    Porto, Portugal
    Posts
    180
    Location
    Quote Originally Posted by xld
    Delete the part from just the one, what is the issue?
    Sorry, could you be more specific?

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by xld
    Don't you mean Rows.Count - 25?
    Ehhh... Well, I am afraid that if that is what I should have meant, I must be having a 'face too close to the tree, can't see the forest' day

    Full admission to testing awfully quick, but given the OP's orig request to leave 25 rows, I was thinking that after the Copy, we would have a new/one sheet/active workbook, and that 'Sheets(1).Rows("26:65536").Delete' would leave the first 25 rows.

    Sorry, I'm still staring and just missing it?

    Mark

Posting Permissions

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