hey guys,
so first things first, I dont really know what im doing here...
the object is to use this excel file to get a folder location. Within that folder will be a bunch of .txt files (data seperate with tab) having a keyword in them (key 1-6). I want to somehow copy everything except for the first row in the .txt file to a sheet in an excel sheet where the key number corresponds to the sheet number (key 3 found means data goes to sheet 3). It would also be really awesome if all of the data went into a seperate .xlsx file and then got saved in whatever directory.
here is my frankenstein attempt at a general code
its worth noting that I'm pretty nub when it comes to opening, closing, saving other files from a vb macro. If you know of any general location of information about that kind of stuff, I would be very thankful.
Also if you do know how to do this, can you please comment out whatever you do so that I can figure out what on earth is going on?
Thanks Gurus!
so first things first, I dont really know what im doing here...
the object is to use this excel file to get a folder location. Within that folder will be a bunch of .txt files (data seperate with tab) having a keyword in them (key 1-6). I want to somehow copy everything except for the first row in the .txt file to a sheet in an excel sheet where the key number corresponds to the sheet number (key 3 found means data goes to sheet 3). It would also be really awesome if all of the data went into a seperate .xlsx file and then got saved in whatever directory.
here is my frankenstein attempt at a general code
Code:
Sub ReadWrite()
Dim sPath As String
Dim sFilename As String
Dim sToFilename As String
Dim key1, key2, key3, key4, key5, key6 As String
Dim toWkBk As Workbook
Dim sht1, sht2, sht3, sht4, sht5, sht6 As Worksheet
Set toWkBk = Workbooks.Add
Set sht1 = Worksheets.Add
Set sht2 = Worksheets.Add
Set sht3 = Worksheets.Add
Set sht4 = Worksheets.Add
Set sht5 = Worksheets.Add
Set sht6 = Worksheets.Add
key1 = "one"
key2 = "two"
key3 = "three"
key4 = "four"
key5 = "five"
key6 = "blargh"
sPath = Sheets(1).Range("B3").Value
If Right(sPath, 1) <> "\" Then
sPath = sPath & "\"
End If
sFilename = Dir(sPath & "*.txt")
Do Until sFilename = ""
If InStr(1, sFilename, key1, vbTextCompare) > 0 Then
'copy .txt to excel sheet 1 with all but header row
'towkbk.sht1. somethingrather?
Else
If InStr(1, sFilename, key2, vbTextCompare) > 0 Then
'copy .txt to excel sheet 2 with all but header row
Else
If InStr(1, sFilename, key3, vbTextCompare) > 0 Then
'copy .txt to excel sheet 3 with all but header row
Else
If InStr(1, sFilename, key4, vbTextCompare) > 0 Then
'copy .txt to excel sheet 4 with all but header row
Else
If InStr(1, sFilename, key5, vbTextCompare) > 0 Then
'copy .txt to excel sheet 5 with all but header row
Else
'copy .txt to excel sheet 6 with all but header row
End If
End If
End If
End If
End If
Loop
toWkBk.SaveAs Filename:="C:/" & sToFilename
End Sub
its worth noting that I'm pretty nub when it comes to opening, closing, saving other files from a vb macro. If you know of any general location of information about that kind of stuff, I would be very thankful.
Also if you do know how to do this, can you please comment out whatever you do so that I can figure out what on earth is going on?
Thanks Gurus!