+ Reply to Thread
Results 1 to 6 of 6

Thread: Using VB to Access Excel Data

  1. #1
    Registered User
    Join Date
    08-27-2010
    Location
    Yakima, WA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Thumbs up Using VB to Access Excel Data

    I need to write a VB program that will open a spreadsheet, get data from one or more worksheets and then put the data into a database for additional processing and report generation. I have written programs to get data out of spreadheets before and put data into a database so I am not worried about how to do that. The challenge is to find the data in the one or more worksheets in a workbook.

    I am working with estimators that use spreadsheets to estimate jobs. They have a template worksheet that they use. Within this worksheet is a range of cells that have all of the data that I need. The problem is that the summary data is never in the same location. The more complex the job, the more rows in the spreadsheet which moves the summary data further down. To further complicate things, a simple job may have one worksheet while complicated ones will have several. I need to find the summary data in each worksheet within the workbook.

    I thought that I could name the range and would be able to access the name in each worksheet but I found that when I tried to define a range in a worksheet with the same name as another worksheet, it simply updated the range.

    I need to make this as simple as possible. Is there a way to define a range in a worksheet that I could find in multiple worksheets within a workbook?

    How do I get a list of worksheets within a workbook?

    How do I find the range of cells that I am looking for within each worksheet? The range will always be the same size but it will be in a different location within each worksheet.

    Any help or tips in the right direction will be appreciated.

    Thanks

    John

  2. #2
    Valued Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: Using VB to Access Excel Data

    Hi

    The following code will copy each named range in a specified (source) workbook into the master workbook. It's basic, but it's a start...

    I've attached the 2 files fyi.

    Dion


    Sub ImportRanges()
    
    Dim wbSource As Object
    Dim varNameRange As Name
    Dim wsTarget As Integer
    Dim varTrgSheetName As String
    
    Workbooks.Open "C:\Documents and Settings\Source File.xlsx"
    
    Set wbSource = ActiveWorkbook
    wsTarget = 1
    
    For Each varNameRange In wbSource.Names
        varTrgSheetName = Mid$(varNameRange, 2, 6)
        Sheets(varTrgSheetName).Select
    
        Range(varNameRange).Select
        Selection.Copy
    
        ThisWorkbook.Activate
        Sheets(wsTarget).Select
        Range("A1").Select
        ActiveSheet.Paste
        
        wsTarget = wsTarget + 1
        wbSource.Activate
    Next varNameRange
    
    wbSource.Close False
    
    Set wbSource = Nothing
    
    End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    08-27-2010
    Location
    Yakima, WA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Using VB to Access Excel Data

    I can't get this code to work at all. I added the Excel interop reference but it doesn't help very much. Just looking at the code shows a bunch of variables that don't have any reference.

    Sorry, but I don't see how this code works.

  4. #4
    Forum Guru
    Join Date
    01-03-2006
    Location
    Taranaki, New Zealand
    MS-Off Ver
    2007 (work & home)
    Posts
    2,242

    Re: Using VB to Access Excel Data

    hi,

    I'm using 2007 now but I have had the same difficulty trying to use the same name on multiple sheets in the same workbook. Some techniques that (I think) provided a work around in Excel 2003 were:
    1) Use JKP's name manager (see below link) to easily "localise the names to each sheet".
    http://www.jkp-ads.com/officemarketplacenm-en.asp
    2) Change the "scope" of the name in code to be specific to the sheet, but I can't remember the 2003 code.
    3) Prefix the name of the Named Range with a brief string to identify the sheet & concatenate it with your original Name, then perform the same concatenation within your code as you loop through the sheets.

    hth
    Rob
    Last edited by broro183; 08-28-2010 at 03:45 PM. Reason: added the link
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: Using VB to Access Excel Data

    If you want to create the name Test locally to three sheets called Sheet1, Sheet2 and Sheet3, you would actually just enter the names using Sheet1!Test, Sheet2!Test and Sheet3!Test

  6. #6
    Forum Guru
    Join Date
    01-03-2006
    Location
    Taranaki, New Zealand
    MS-Off Ver
    2007 (work & home)
    Posts
    2,242

    Re: Using VB to Access Excel Data

    Thanks RomperStomper,

    I knew it was easy but had forgotten just how easy it is to create local names :-)

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

+ Reply to Thread

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