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
Bookmarks