+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    06-03-2009
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Toggling Between Workbooks

    The code below is not executing correctly due to the fact that I believe I am not toggling between the workbook where I am executing the code (wbCodeBook), and where I am copying the gPA worksheet (wbResults). Any ideas where I am going wrong?

    Code:
    For i = 1 To 50
            If FileOrDirExists("X:\2010 BUDGET\" & stateHold(i)) Then
                With Application.FileSearch
                    .NewSearch
                    .LookIn = "X:\2010 BUDGET\" & stateHold(i)
                    .FileType = msoFileTypeExcelWorkbooks
                    .Filename = "*.xls"
                    If .Execute > 0 Then 'Workbooks in folder
                        For lCount = 1 To .FoundFiles.Count ' Loop through all.
                            Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                                wbResults.Activate
                                If IsNumeric(Mid(wbResults.Name, 3, 4)) Then
                                    For Each wks In Worksheets
                                        Select Case wks.Name
                                        Case gPA
                                            wks.Unprotect
                                            wks.Copy
                                            wbCodeBook.Activate
                                            wbCodeBook.Sheets.Add.Name = wbResults.Name & " - GPA"
                                            Sheets(wbResults.Name & " - GPA").Select
                                            Selection.Paste
                                        End Select
                                    Next wks
                                End If
                                wbResults.Activate
                                wbResults.Close SaveChanges:=True
                        Next lCount
                    End If
                End With
            End If
        Next i
    Thanks in advance....

  2. #2
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: Toggling Between Workbooks

    scm24
    you could copy the sheet to the other workbook with
    Code:
    Sheets(1).Copy Before:=Workbooks.(wbCodeBook).Sheets(1)
    then rename sheet
    Code:
    Workbooks.(wbCodeBook).Sheets("Sheet1").Name = "pike"
    hope it helps
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  3. #3
    Valued Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    623

    Re: Toggling Between Workbooks

    Here's some code I found.
    I Don't know if this is what you want, but you can try this.

    Insert this code into each workbook that you want to toggle between.
    Create a button at the top of a sheet within each book. Assign the macro placed into each book to the button. This will allow you to toggle.

    Code:
    Sub NextBk()
    Dim NumBooks As Integer, n As Integer
    Dim ThisBook As Integer, FirstBk As Integer
    
    NumBooks = Workbooks.Count
    If Workbooks(1).Name = "YourOpenWorkbookName.xls" Then
    FirstBk = 2
    Else
    FirstBk = 1
    End If
    
    For n = FirstBk To NumBooks
    If Workbooks(n).Name = ActiveWorkbook.Name Then ThisBook = n
    Next
    
    If ThisBook < NumBooks Then
    ' move to the next books
    Workbooks(ThisBook + 1).Activate
    End If
    
    If ThisBook = NumBooks And NumBooks > FirstBk Then
    ' move back to first book
    Workbooks(FirstBk).Activate
    End If
    
     End Sub
    HTH's

    BDB

Thread Information

Users Browsing this Thread

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

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