Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2004, 03:00 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loading from multiple sheets on another workbook

Hi
I'm trying to extend an existing function I wrote to use different sheets on a different workbook, instead of the active workbook.

The function below did work when using 'local' sheets. But I want to load all my data into a another xls file and load/read from it as and when required. I use multiple sheets a lot and don't want to have to add the same sheets to each of my workbooks each time i want to use the data.

It had now stopped working. It works for a split-second if I manually load the test.xls file, but after a sec it refreshs and returns error values.

Also, as you can see, I'm trying to get it to return "n/a" if it can't find a matching set of values, instead of #VALUE!.

Any ideas anyone? :)


Code:
Public Function lookupref(ref As String, dateref As Date, season As String, reqdcol As String)
    Application.Volatile

   'Load and activate correct season sheet
    Dim wbTest As Workbook
    tmpfile = "c:\test.xls"
    Set wbTest = Workbooks.Open(Filename:=tmpfile)
    wbTest.Activate

    'Get size of dataset
    lastrow = ActiveWorkbook.Worksheets(season).Cells(2, "A").Value

    'Loop data
    lookupref = "n/a"
    For a = 3 To (Int(lastrow) + 2)
        tmpflt1 = UCase(ref)
        tmpflt2 = UCase(ActiveWorkbook.Worksheets(season).Cells(a, "B").Value)
        tmpdate1 = dateref
        tmpdate2 = DateValue(ActiveWorkbook.Worksheets(season).Cells(a, "A").Value)
        'MsgBox tmpflt1 & " " & tmpflt2 & " " & tmpdate1 & " " & tmpdate2
        If (tmpflt1 = tmpflt2) Then
            If (tmpdate1 = tmpdate2) Then
                'MsgBox "Match"
                lookupref = ActiveWorkbook.Worksheets(season).Cells(a, reqdcol).Value
                Exit Function
            Else
                lookupref = "n/a"
            End If
        Else
            lookupref = "n/a"
        End If
    Next a

    ActiveWorkbook.Close
End Function





Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching Multiple Sheets for Data? Bill_L Excel VBA 2 June 20th, 2007 09:14 AM
copy sheets jgrant Beginning VB 6 0 March 26th, 2007 01:42 PM
Problem with loading user control multiple times neha mukerjee ASP.NET 2.0 Basics 0 February 24th, 2006 06:11 AM
Code to populate multiple sheet into workbook kud Excel VBA 0 June 9th, 2004 11:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.