I think I am in way over my head. I have been writing some code to import a new csv file. I have two workbooks of interest. The first workbook is Main Data Entry.xls and this is where the code will be run. The second workbook is Location Details and it contains data entered by the user at another time.
The ultimate goal is to have this new file be placed on a new worksheet (in the Main Data Entry.xls file) named something like Grout 9-30-05 (ie. a location and a date). The user supplies this information via two userforms. One is called CreekSlection the other is called sampledate (both located under the Main Data Entry.xls workbook). Each of these forms aquires its input and then sets a cell on the Main Data Sheet equal to that input (this is the only way that I know of to transfer data from a userform into a module, although I am quite new at all this so I am sure that there is a better way).
This new sheet needs a few things added to it. Like the date the data was created, the location that it came from, and the hard part, information from the Location Details workbook.
A little more about the Location Details workbook before I continue. The location details workbook is a storage of details about the site and specifically info about when this csv file that is to be imported was aquire (it is from a field datalogger). The user enters the information like date data downloaded, did level loger change elevation, etc. This information is then saved to a new sheet using the date the user enters as date data was downloaded. Here is an example of the user created sheet in Location Data:
So as I am sure most of you can see I have quite a few issues to deal with. Such as well what if the dates don't match up in the two workbooks. There would be no sheet to get data from.
The question that I need help with ... atleast right now is this:
Is is possible to ask the user to select the corresponding sheet from the Location Data Workbook that matches the csv file that they are about to import? With this sheet selected how do I call it when copying data from it to the Main Data Entry Workbook, (on the new sheet just created).
Here is my code so far. I have tried to comment it so that I know what I am doing. I have also left a few lines blank where I am trying to implement the user selected data.
Also here is an example of the final sheet that I am after in the Main Data Entry.xls workbook. Originally the csv file contains all the information located in a8:b40 (The 40 rows is arbitrary as it could be bigger or smaller).
Thanks for all your help. I have gone from not knowing much about VBA to writitng all of this in only a matter of days.
The ultimate goal is to have this new file be placed on a new worksheet (in the Main Data Entry.xls file) named something like Grout 9-30-05 (ie. a location and a date). The user supplies this information via two userforms. One is called CreekSlection the other is called sampledate (both located under the Main Data Entry.xls workbook). Each of these forms aquires its input and then sets a cell on the Main Data Sheet equal to that input (this is the only way that I know of to transfer data from a userform into a module, although I am quite new at all this so I am sure that there is a better way).
This new sheet needs a few things added to it. Like the date the data was created, the location that it came from, and the hard part, information from the Location Details workbook.
A little more about the Location Details workbook before I continue. The location details workbook is a storage of details about the site and specifically info about when this csv file that is to be imported was aquire (it is from a field datalogger). The user enters the information like date data downloaded, did level loger change elevation, etc. This information is then saved to a new sheet using the date the user enters as date data was downloaded. Here is an example of the user created sheet in Location Data:
So as I am sure most of you can see I have quite a few issues to deal with. Such as well what if the dates don't match up in the two workbooks. There would be no sheet to get data from.
The question that I need help with ... atleast right now is this:
Is is possible to ask the user to select the corresponding sheet from the Location Data Workbook that matches the csv file that they are about to import? With this sheet selected how do I call it when copying data from it to the Main Data Entry Workbook, (on the new sheet just created).
Here is my code so far. I have tried to comment it so that I know what I am doing. I have also left a few lines blank where I am trying to implement the user selected data.
Code:
Sub ImportCSVa()
'This routine imports the csv file that was just saved from global loggers software.
Dim CurrentDump As Variant
Dim Wb As Workbook
Dim ws As Worksheet
Dim wsNew As Worksheet
Dim location As String
Dim importdate As String
Dim importwb As Worksheet
' This doesn 't update the screen as the program progesses.
Application.ScreenUpdating = False
'set currentdump = filename of selected file (must be csv or txt file)
CurrentDump = Application.GetOpenFilename("Please select a Global Logger CSV File,*.csv; *.txt", _
1, "Please select a Global Logger CSV File", , False)
'Checks to see if user selected a file if they don't it just ends the routine
If TypeName(CurrentDump) = "Boolean" Then Exit Sub
'Calls the userform to determine which creek this data is from
Call UserFormLoad
' Sets the location variables to the name of the creek slected by the user.
location = Workbooks("Main Data Entry.xls").Sheets("main").Cells(3, 16).Value
'Calls the userform to determine the corresponding date
Load sampledate
sampledate.Show
'Sets the variable ldsheetname = to the date selected by the user.
ldsheetname = Workbooks("Main Data Entry.xls").Sheets("main").Range("p4").Value
'USER SELECTION OF WORKSHEET FROM LOCATION DATA HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'Opens the workbook of currentdump and sets it equal to the variable wb
Workbooks.OpenText Filename:=CurrentDump, DataType:=xlDelimited, Comma:=True
Set Wb = ActiveWorkbook
'Sets the ws worksheet vairable to the Wb workbook variable active sheet.
Set ws = Wb.ActiveSheet
'Opens a new workbook and sets it equal to wsnew.
Set wsNew = Workbooks("Main Data Entry.xls").Sheets.Add(After:=Sheets(Sheets.Count))
'Select Sheet main for if statement
'Workbooks("Main Data Entry.xls").Sheets.Select ("main")
'Renames the sheet.
wsNew.Name = Workbooks("Main Data Entry.xls").Sheets("main").Range("P3").Text & " " & Month(Now) & _
"-" & Day(Now) & "-" & Year(Now)
'Clears contents just in case
wsNew.Cells.ClearContents
'Copies info from imported CSV file
ws.Cells.Copy Workbooks("Main Data Entry.xls").Sheets(wsNew.Name).Range("A1")
'Closes New Workbook
Wb.Close
'Below this is the formating of the new worksheet that was just created.
'Insert seven new rows at the top of the sheet
Sheets(wsNew.Name).Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
' Set up the information headings that never change
Sheets(wsNew.Name).Range("a1") = "DATE IMPORTED:"
Range("a2").Value = "LOCATION DETAILS:"
Range("a3").Value = "Gauge Height Before"
Range("a4").Value = "LL Reading Before:"
Range("a5").Value = "Gauge Height After:"
Range("a6").Value = "LL Reading After:"
Range("b9").Value = "Feet Imported"
Range("d8").Value = "DATE AND TIME INFORMATION"
Range("D9").Value = "Year"
Range("e9").Value = "Month"
Range("f9").Value = "Day"
Range("g9").Value = "Time"
'Now the harder part, assigning values that change.
'Sets the date imported today's date
Range("b1").Value = " " & Month(Now) & _
"-" & Day(Now) & "-" & Year(Now)
Range("d2").Value = location & "Creek"
'Aquires the info from the Location Details worksheet.
'Need an if statement to detrmine which info to get.
'CALL DATA FROM USERSELECTED SHEET HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If location = "Grout" Then
Range("b3").Value = Workbooks("Location Details.xls").Sheets(ldsheetname.Value).Range("b9")
Range("b4").Value = Workbooks("Location Details.xls").Sheets(ldsheetname.Value).Range("b10")
Range("b5").Value = Workbooks("Location Details.xls").Sheets(ldsheetname.Value).Range("b11")
Range("b6").Value = Workbooks("Location Details.xls").Sheets(ldsheetname.Value).Range("b12")
Else
End If
'Updates Screen
Application.ScreenUpdating = True
End Function
Also here is an example of the final sheet that I am after in the Main Data Entry.xls workbook. Originally the csv file contains all the information located in a8:b40 (The 40 rows is arbitrary as it could be bigger or smaller).
Thanks for all your help. I have gone from not knowing much about VBA to writitng all of this in only a matter of days.