+ Reply to Thread
Results 1 to 3 of 3

Specifying a specific sheet in a "FindString" command

Hybrid View

jbrooks1988 Specifying a specific sheet... 03-29-2016, 06:03 AM
walruseggman Re: Specifying a specific... 03-29-2016, 10:07 AM
jbrooks1988 Re: Specifying a specific... 03-29-2016, 10:18 AM
  1. #1
    Registered User
    Join Date
    01-05-2016
    Location
    Southampton
    MS-Off Ver
    Excel 2013
    Posts
    40

    Specifying a specific sheet in a "FindString" command

    I have the following VBA code to retrieve data from hundred of spreadsheets. The data is located in sheet 1, named "Sheet1". A new sheet has been added to all the spreadsheets (not relevant to the data this macro retrieves) but each spreadsheet has been saved whilst the new sheet is active, so the command is looking in this sheet for the data "Total Purchased" rather than Sheet1 which was always the default active sheet. How can I amend my code to look in the correct sheet?

    I hope this is clear! Thank you

    Sub Tickets_Unsold()
    Dim inFiles() As Variant
    Dim InData() As Variant
    Dim inRng As Range
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim wb1 As Workbook, wb2 As Workbook, wb3 As Workbook
    Dim srow As Long, lrow As Long
    Dim lastrow1 As Long, Lastrow As Long
    Dim lastcol As Long
    Dim r As Long, rr As Long, row As Long
    Dim wsname As String, wbname As String
    Dim FilePath As String
    Dim FindString As String
    Dim var As Variant
    
    ThisWorkbook.Activate
    
    Set wb1 = ThisWorkbook
    
    FilePath = Range("C1")
    
    wbname = "Unsold Tickets.xlsx"
    '
    ' Open the "Unsold Tickets" file if not open
    '
    If Not WorkbookIsOpen(wbname) Then
        Workbooks.Open Filename:= _
        FilePath & wbname
    End If
    
    Set wb2 = Workbooks(wbname)
    rr = 2
    
    wb1.Activate
    
    With wb1
    '
    '  Find last row in Column A
    '
        lastrow1 = Cells(Rows.Count, 1).End(xlUp).row
        
        Set inRng = Range(Cells(3, 1), Cells(lastrow1, 1))
        '
        '  Assign data to in-core array (speedier processing)
        '
        inFiles = inRng
        '
        '  Loop through all the files
        '
        For r = 1 To UBound(inFiles, 1)
        
            wbname = inFiles(r, 1)
            '
            ' Open workbook
            '
            If Not WorkbookIsOpen(wbname) Then
                Workbooks.Open Filename:= _
                FilePath & wbname
            End If
            
            Set wb3 = Workbooks(wbname)
            
            wb3.Activate
            
            With wb3
            
                FindString = "Total Purchased"
                var = Application.Match(FindString, Range("B1:B1000"), 0)
                
                If IsError(var) Then
                   MsgBox "End of table (""Total Purchased"") not found: exit program"
                   Exit Sub
                End If
                
                Lastrow = var
                lastcol = Cells(7, Columns.Count).End(xlToLeft).Column
                
                Set inRng = Range(Cells(1, 2), Cells(Lastrow, lastcol))
        '
        '  Assign data to in-core array (speedier processing)
        '
                 InData = inRng
                
                 For row = 8 To UBound(InData, 1)
                 '
                 ' find unsold seats
                 '
                    If IsNumeric(InData(row, 12)) And InData(row, 12) > 0 Then
                                                   
                        wb2.Sheets("Sheet1").Cells(rr, 4) = InData(row, 1)
                        wb2.Sheets("Sheet1").Cells(rr, 6) = InData(row, 3)
                        wb2.Sheets("Sheet1").Cells(rr, 7) = InData(row, 4)
                        wb2.Sheets("Sheet1").Cells(rr, 8) = InData(row, 5)
                        wb2.Sheets("Sheet1").Cells(rr, 9) = InData(row, 10)
                        wb2.Sheets("Sheet1").Cells(rr, 11) = InData(row, 12)
                        wb2.Sheets("Sheet1").Cells(rr, 12) = InData(row, 13)
                        wb2.Sheets("Sheet1").Cells(rr, 13) = InData(row, 14)
                        wb2.Sheets("Sheet1").Cells(rr, 14) = InData(row, 18)
                        wb2.Sheets("Sheet1").Cells(rr, 1) = InData(1, 1)
                        wb2.Sheets("Sheet1").Cells(rr, 2) = InData(2, 1)
                        wb2.Sheets("Sheet1").Cells(rr, 3) = Trim(Mid(InData(3, 1), InStr(1, InData(3, 1), ",") + 1, 50))
                        rr = rr + 1
                                   
                    End If
                    
                Next row
            
            End With
            
            Workbooks(wbname).Close SaveChanges:=False
            
         rr = rr + 1
         
         wb1.Activate
         
         Next r
             
     End With
    
    wb2.Activate
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
    
       
    End Sub
    Private Function WorkbookIsOpen(wbname) As Boolean
    '   Returns TRUE if the workbook is open
        Dim x As Workbook
        On Error Resume Next
        Set x = Workbooks(wbname)
        If Err = 0 Then WorkbookIsOpen = True _
            Else WorkbookIsOpen = False
    End Function

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Specifying a specific sheet in a "FindString" command

    Something like this:

    wb1.Activate
    Set ws1 = wb1.Sheets("Total Purchased")
    With ws1
    and then all your Ranges refering to that sheet inside that with statement need to look like:

    lastrow1 = .Cells(Rows.Count, 1).End(xlUp).row
        
        Set inRng = .Range(.Cells(3, 1), .Cells(lastrow1, 1))
    etc. etc.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Registered User
    Join Date
    01-05-2016
    Location
    Southampton
    MS-Off Ver
    Excel 2013
    Posts
    40

    Re: Specifying a specific sheet in a "FindString" command

    Hi, I dont understand? Do I just add the line "Set ws1 = wb1.Sheets("Total Purchased")" into the macro after "wb1.Activate"??

    Apologies, I didnt write this code and am a complete VBA novice..

+ 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] :confused: Auto pick "specific data" and put it in "specific cells" with date
    By pipsmultan in forum Excel Formulas & Functions
    Replies: 29
    Last Post: 08-27-2014, 03:31 AM
  2. Auto Pick Only "Specific Day & Date" & put in "specific cells" Vertically
    By pipsmultan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-25-2014, 01:24 AM
  3. Need help creating specific "add-row" macro using a command button
    By rmtreadw in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-28-2014, 10:01 PM
  4. Replies: 3
    Last Post: 05-31-2013, 05:16 AM
  5. Create a Command Button to "Save As" and "Close" an Excel Workbook
    By thedunna in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-26-2013, 05:38 PM
  6. How to change a Command Button caption from "Enable" to "Disable"?
    By Infinity in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2007, 12:14 AM
  7. [SOLVED] inserting a conditional "go to" command on a excel "if" function
    By velasques in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-10-2006, 04:20 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