+ Reply to Thread
Results 1 to 6 of 6

Copying value from 1 cell from various spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Copying value from 1 cell from various spreadsheet

    Hi,

    I have several workbooks that I want to extract the value from cell D1 in each worksheet from and place into a master spreadsheet. The end result is shown on the NOTOK control excel file. The rest of the files of NOTOK represent the data worksheets to be extracted from. Thanks in advance to anyone who can help.
    Attached Files Attached Files
    Last edited by kchm_2000; 10-22-2011 at 02:50 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copying value from 1 cell from various spreadsheet

    Try this:
    Option Explicit
    
    Sub CollectData()
    Dim fPath As String, fName As String
    Dim NR As Long, ws As Worksheet, wb As Workbook
    
    With ThisWorkbook.Sheets("Sheet1")
        .Range("B:D").UsedRange.Offset(1).ClearContents     'clear prior report
        NR = 2                                              'next row to add data
        fPath = .Range("A2")
        If Right(fPath, 1) <> Application.PathSeparator Then _
              fPath = fPath & Application.PathSeparator
    
        fName = Dir(fPath & "*.xls")            'get first filename
        Application.ScreenUpdating = False      'speed up macro execution
        
        Do While Len(fName) > 0                 'abort when all files completed
            Set wb = Workbooks.Open(fPath & fName)  'open the found file
            .Range("B" & NR) = fName            'add filename to report
            For Each ws In wb.Worksheets        'get data from each sheet in wb
                .Range("C" & .Rows.Count).End(xlUp).Offset(1) = ws.Name
                .Range("C" & .Rows.Count).End(xlUp).Offset(, 1).Value = ws.Range("D1").Value
            Next ws
            wb.Close False                      'close the wb, no changes
            NR = .Range("C" & .Rows.Count).End(xlUp).Row + 1    'next row for next wb
            fName = Dir                         'get next filename
        Loop
    
        Application.ScreenUpdating = True
    End With
    
    End Sub
    Attached Files Attached Files
    Last edited by JBeaucaire; 10-20-2011 at 12:23 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Copying value from 1 cell from various spreadsheet

    Hi,

    somehow this code is buggy

    .Range("B:D").UsedRange.Offset(1).ClearContents     'clear prior report

  4. #4
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Copying value from 1 cell from various spreadsheet

    The rest of the code works fine, but i would like to ask how would you adjust the formula such that it avoids running through hidden sheets?

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copying value from 1 cell from various spreadsheet

    Like so:
            For Each ws In wb.Worksheets        'get data from each sheet in wb
              If ws.Visible = xlSheetVisible Then
                .Range("C" & .Rows.Count).End(xlUp).Offset(1) = ws.Name
                .Range("C" & .Rows.Count).End(xlUp).Offset(, 1).Value = ws.Range("D1").Value
              End If
            Next ws

  6. #6
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Copying value from 1 cell from various spreadsheet

    thanks for all your help so far you are really great

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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