+ Reply to Thread
Results 1 to 4 of 4

Importing Data from Multiple Excel Forms into Multiple Tabs in New Workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    06-13-2014
    Posts
    7

    Importing Data from Multiple Excel Forms into Multiple Tabs in New Workbook

    I had started a thread for importing data from multiple excel files: http://www.excelforum.com/excel-gene...ml#post3734721

    I figured out that portion, but want to take it a step further. I would like to be able to have a selection on my excel form for which "category" the information belongs to... there are 4 categories "SC" "Todd" "PS" and "Contract" I attempted writing the following code but it only writes to the "contract" tab never to the other regardless of what input I have given for K3 on my form. So my if/else must not be functioning the way I intend. any guidance would be much appreciated.

    Thanks.

    Option Explicit
    
    Sub ImportCellsFromFiles()
    Dim wsMAIN As Worksheet, wb As Workbook
    Dim fPATH As String, fNAME As String, NR As Long
    Dim SheetName As String
    
    SheetName = "Contracts"
    
    Set wsMAIN = ThisWorkbook.Sheets(SheetName)          'the mail merge sheet to put data into
    NR = wsMAIN.Range("A" & Rows.Count).End(xlUp).Row + 1   'next empty row on the Mail Merge sheet
    
    fPATH = "O:\Meeting Agendas, Recaps\EMS Meeting Agendas\EMS Current Service Tickets\"                      'remember the final \ in this string
    
    fNAME = Dir(fPATH & "*.xl*")                        'get the first filename from the FPATH
    
    Do While Len(fNAME) > 0                             'loop one file at a time, stop when no more files
        Set wb = Workbooks.Open(fPATH & fNAME)          'open the currently found file
                                                        'put specific values into Mail merge sheet
                                                        'edit these values B10, D20, A15 to the correct source cells
                                                        'add more rows as needed to pull all the data needed
                                                        
        If wb.Sheets(1).Range("K3").Value = "Contracts" Then
          SheetName = "Contracts"
        ElseIf wb.Sheets(1).Range("K3").Value = "SC" Then
          SheetName = "SC"
        ElseIf wb.Sheets(1).Range("k3").Value = "PS" Then
          SheetName = "PS"
        ElseIf wb.Sheets(1).Range("K3").Value = "Todd" Then
          SheetName = "Todd"
        Else: SheetName = "Contracts"
         End If
        
        wsMAIN.Range("A" & NR).Value = wb.Sheets(1).Range("A3").Value
        wsMAIN.Range("B" & NR).Value = wb.Sheets(1).Range("E3").Value
        wsMAIN.Range("C" & NR).Value = wb.Sheets(1).Range("H3").Value
        wsMAIN.Range("D" & NR).Value = wb.Sheets(1).Range("E43").Value
        wsMAIN.Range("E" & NR).Value = wb.Sheets(1).Range("C6").Value
        wsMAIN.Range("F" & NR).Value = wb.Sheets(1).Range("F6").Value
        wsMAIN.Range("G" & NR).Value = wb.Sheets(1).Range("I6").Value
        wsMAIN.Range("H" & NR).Value = wb.Sheets(1).Range("A9").Value
        wsMAIN.Range("I" & NR).Value = wb.Sheets(1).Range("C13").Value
        wsMAIN.Range("J" & NR).Value = wb.Sheets(1).Range("C15").Value
        wsMAIN.Range("K" & NR).Value = wb.Sheets(1).Range("F13").Value
        wsMAIN.Range("L" & NR).Value = wb.Sheets(1).Range("F15").Value
        wsMAIN.Range("M" & NR).Value = wb.Sheets(1).Range("I13").Value
        wsMAIN.Range("N" & NR).Value = wb.Sheets(1).Range("F34").Value
        wsMAIN.Range("O" & NR).Value = wb.Sheets(1).Range("I34").Value
        wsMAIN.Range("P" & NR).Value = wb.Sheets(1).Range("A36").Value
        wb.Close False                                  'close opened file
        NR = NR + 1                                     'set the next empty Mail Merge row
    
        fNAME = Dir                                     'get the next filename from the FPATH
       
        
    Loop
    
    End Sub

  2. #2
    Registered User
    Join Date
    06-13-2014
    Posts
    7

    Re: Importing Data from Multiple Excel Forms into Multiple Tabs in New Workbook

    Not sure I am describing my intent properly... I am trying to have the files be able to write into multiple sub -category tabs. in other words I want a summary page of all "Sc" forms, all "PS" forms, all "contract" forms, and all "todd" forms but dont want to have to presort them before running the macro. I want the macro to sort the information into the corresponding tabs on its own.

    Sorry that I do not know how to better describe this.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Importing Data from Multiple Excel Forms into Multiple Tabs in New Workbook

    Moved the set wsMain within the loop

    Sub ImportCellsFromFiles()
    Dim wsMAIN As Worksheet, wb As Workbook
    Dim fPATH As String, fNAME As String, NR As Long
    Dim SheetName As String
        
    fPATH = "O:\Meeting Agendas, Recaps\EMS Meeting Agendas\EMS Current Service Tickets\"                      'remember the final \ in this string
        
    fNAME = Dir(fPATH & "*.xl*")                        'get the first filename from the FPATH
        
    Do While Len(fNAME) > 0                             'loop one file at a time, stop when no more files
        Set wb = Workbooks.Open(fPATH & fNAME)          'open the currently found file
                                                        'put specific values into Mail merge sheet
                                                        'edit these values B10, D20, A15 to the correct source cells
                                                        'add more rows as needed to pull all the data needed
        
        If wb.Sheets(1).Range("K3").Value = "Contracts" Then
          SheetName = "Contracts"
        ElseIf wb.Sheets(1).Range("K3").Value = "SC" Then
          SheetName = "SC"
        ElseIf wb.Sheets(1).Range("k3").Value = "PS" Then
          SheetName = "PS"
        ElseIf wb.Sheets(1).Range("K3").Value = "Todd" Then
          SheetName = "Todd"
        Else: SheetName = "Contracts"
         End If
        
        Set wsMAIN = ThisWorkbook.Sheets(SheetName)          'the mail merge sheet to put data into
        NR = wsMAIN.Range("A" & Rows.Count).End(xlUp).Row + 1   'next empty row on the Mail Merge sheet
        
        wsMAIN.Range("A" & NR).Value = wb.Sheets(1).Range("A3").Value
        wsMAIN.Range("B" & NR).Value = wb.Sheets(1).Range("E3").Value
        wsMAIN.Range("C" & NR).Value = wb.Sheets(1).Range("H3").Value
        wsMAIN.Range("D" & NR).Value = wb.Sheets(1).Range("E43").Value
        wsMAIN.Range("E" & NR).Value = wb.Sheets(1).Range("C6").Value
        wsMAIN.Range("F" & NR).Value = wb.Sheets(1).Range("F6").Value
        wsMAIN.Range("G" & NR).Value = wb.Sheets(1).Range("I6").Value
        wsMAIN.Range("H" & NR).Value = wb.Sheets(1).Range("A9").Value
        wsMAIN.Range("I" & NR).Value = wb.Sheets(1).Range("C13").Value
        wsMAIN.Range("J" & NR).Value = wb.Sheets(1).Range("C15").Value
        wsMAIN.Range("K" & NR).Value = wb.Sheets(1).Range("F13").Value
        wsMAIN.Range("L" & NR).Value = wb.Sheets(1).Range("F15").Value
        wsMAIN.Range("M" & NR).Value = wb.Sheets(1).Range("I13").Value
        wsMAIN.Range("N" & NR).Value = wb.Sheets(1).Range("F34").Value
        wsMAIN.Range("O" & NR).Value = wb.Sheets(1).Range("I34").Value
        wsMAIN.Range("P" & NR).Value = wb.Sheets(1).Range("A36").Value
        wb.Close False                                  'close opened file
        NR = NR + 1                                     'set the next empty Mail Merge row
        
        fNAME = Dir                                     'get the next filename from the FPATH
       
        
    Loop
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    06-13-2014
    Posts
    7

    Re: Importing Data from Multiple Excel Forms into Multiple Tabs in New Workbook

    thanks for your help!

+ 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. Replies: 3
    Last Post: 06-16-2014, 04:49 PM
  2. [SOLVED] Importing Multiple text documents into Different Tabs in Excel
    By jzy2101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-08-2014, 09:40 AM
  3. Importing multiple data files to run through excel workbook and save separately
    By Robo25 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-30-2013, 12:25 PM
  4. Importing data from multiple word forms into a single Excel spreadsheet with vba
    By marble2009 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-15-2011, 10:31 AM
  5. importing data from multiple tabs
    By squinne in forum Excel General
    Replies: 6
    Last Post: 05-07-2009, 03:14 PM

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