+ Reply to Thread
Results 1 to 5 of 5

how do I get the first array value of the loop?

Hybrid View

bbmak how do I get the first array... 05-29-2014, 08:34 PM
shg Re: how do I get the first... 05-29-2014, 08:41 PM
AlphaFrog Re: how do I get the first... 05-29-2014, 08:46 PM
bbmak Re: how do I get the first... 05-30-2014, 01:43 PM
shg Re: how do I get the first... 05-30-2014, 01:51 PM
  1. #1
    Registered User
    Join Date
    05-21-2014
    Posts
    30

    how do I get the first array value of the loop?

    I use this vba to split my excel sheets into multiple files, however, the first sheet into a file usually the original file, and it will be delete.
    Right now I am hard coding it with Kill mypath & "\sheet1.xlsx"
    Are there anyway to make it dynamic or a string?

    can I do something like sht.Name(0) ??? Get the first value of the array?

    Sub Splitbook()
    
    mypath = ActiveWorkbook.Path
    For Each sht In ActiveWorkbook.Sheets
    sht.Copy
    ActiveSheet.Cells.Copy
    ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
    ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
    ActiveWorkbook.SaveAs _
    Filename:=mypath & "\" & sht.Name & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
    Next sht
    Kill mypath & "\sheet1.xlsx"
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: how do I get the first array value of the loop?

    You should get in the habit of declaring your variables.

    Sub Splitbook()
        Dim sPath       As String
        Dim i           As Long
    
        sPath = ActiveWorkbook.Path
    
        For i = 2 To ActiveWorkbook.Worksheets.Count
            Worksheets(i).Copy
            With ActiveSheet
                .UsedRange.Value = .UsedRange.Value
                .Parent.SaveAs Filename:=sPath & "\" & .Name & ".xlsx"
                .Parent.Close SaveChanges:=False
            End With
        Next i
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: how do I get the first array value of the loop?

    This avoids copying the 1st sheet.

    Sub Splitbook()
        Dim i As Long, mypath As String
        mypath = ThisWorkbook.Path
        Application.ScreenUpdating = False
        For i = 2 To ThisWorkbook.Sheets.Count
            ThisWorkbook.Sheets(i).Copy
            ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
            ActiveWorkbook.SaveAs _
                    Filename:=mypath & "\" & ActiveSheet.Name & ".xlsx"
            ActiveWorkbook.Close SaveChanges:=False
        Next i
        Application.ScreenUpdating = True
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    05-21-2014
    Posts
    30

    Re: how do I get the first array value of the loop?

    Thank Guys, it works.

    @shg
    yes, i am a bad vba programmer.... I really need to get that into practice.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: how do I get the first array value of the loop?

    You're welcome.

+ 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] Array size unknown until the first "loop", how to correction dim/redim the array
    By menichols74 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-15-2013, 10:04 AM
  2. Loop new messages containing a table, populate a dynamic array, paste array to Excel
    By laripa in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2013, 07:20 AM
  3. [SOLVED] array loop that loops through another array
    By Bob1980 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-19-2013, 02:42 PM
  4. [SOLVED] Using array in for next loop
    By ybortony in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 10-21-2012, 07:12 PM
  5. Loop array
    By Greg1234 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-04-2007, 07:25 AM

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