I have a database form that is set up as an interface for importing data from an excel worksheet (the worksheet is generated from a different/larger system with a lot of data I do not want imported). When the user selects the appropriate file (me.admin_link) and clicks an 'import data' button, the code should open the excel file, perform the excel data clean-up functions, then import the data from the sheet into the related Access table.
The following is the 'clean-up' coding that I had initially set up as a macro in Excel. I would like to run this code from Access instead (this way eliminating the need for a separate macro outside the database). This code actually ran very well the first time I tried it, but now I am getting possible corruption errors about not having a For before a Next, etc.
Can anyone help me 'clean-up this code'? I believe the c.value reference is what is causing the problems...but any ideas would be great.
Code:
Dim strFileName As String
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Workbooks.Open Me.Admin_Link
For Each c In Worksheets("Data").Range("A2:A1000").Cells
If (Mid(c.Value, 8) = "") Then c.Value = ""
Next
xlApp.Columns("B:B").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("F:F").Select
xlApp.ActiveWindow.LargeScroll ToRight:=1
xlApp.Columns("F:Q").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.ActiveWindow.SmallScroll ToRight:=3
xlApp.Columns("H:M").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("J:J").Select
xlApp.ActiveWindow.SmallScroll ToRight:=3
xlApp.Columns("J:N").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("L:S").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("N:N").Select
xlApp.ActiveWindow.SmallScroll ToRight:=4
xlApp.Columns("N:V").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("P").Select
xlApp.Sheets("AbstractExtract").Select
xlApp.Columns("F:F").Select
xlApp.Selection.Copy
xlApp.Sheets("Data").Select
xlApp.Columns("P").Select
xlApp.ActiveSheet.Paste
xlApp.Sheets(Array("AbstractExtract", "ReportCriteria", "Extract3", "Sheet5", _
"R&R Stage Definitions")).Select
xlApp.Sheets("AbstractExtract").Activate
xlApp.ActiveWindow.SelectedSheets.Delete
xlApp.Columns("B").Select
For Each c In Selection
If c.Hyperlinks.Count > 0 Then
txt = c.Hyperlinks(1).Address
c.Value = txt
Next
For Each c In Worksheets("Data").Range("B").Cells
If c.Value = "N/A" Then c.Value = ""
Next
For Each c In Worksheets("Data").Range("F2:F1000").Cells
If c.Value > "" Then c.Value = StrConv(c, vbProperCase)
Next
For Each c In Worksheets("Data").Range("J2:J1000").Cells
If c.Value > "" Then c.Value = StrConv(c, vbProperCase)
Next
xlApp.ActiveWorkbook.Save
xlApp.ActiveWorkbook.Close
Set xlApp = Nothing
Thanks!!
The following is the 'clean-up' coding that I had initially set up as a macro in Excel. I would like to run this code from Access instead (this way eliminating the need for a separate macro outside the database). This code actually ran very well the first time I tried it, but now I am getting possible corruption errors about not having a For before a Next, etc.
Can anyone help me 'clean-up this code'? I believe the c.value reference is what is causing the problems...but any ideas would be great.
Code:
Dim strFileName As String
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Workbooks.Open Me.Admin_Link
For Each c In Worksheets("Data").Range("A2:A1000").Cells
If (Mid(c.Value, 8) = "") Then c.Value = ""
Next
xlApp.Columns("B:B").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("F:F").Select
xlApp.ActiveWindow.LargeScroll ToRight:=1
xlApp.Columns("F:Q").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.ActiveWindow.SmallScroll ToRight:=3
xlApp.Columns("H:M").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("J:J").Select
xlApp.ActiveWindow.SmallScroll ToRight:=3
xlApp.Columns("J:N").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("L:S").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("N:N").Select
xlApp.ActiveWindow.SmallScroll ToRight:=4
xlApp.Columns("N:V").Select
xlApp.Selection.Delete Shift:=xlToLeft
xlApp.Columns("P").Select
xlApp.Sheets("AbstractExtract").Select
xlApp.Columns("F:F").Select
xlApp.Selection.Copy
xlApp.Sheets("Data").Select
xlApp.Columns("P").Select
xlApp.ActiveSheet.Paste
xlApp.Sheets(Array("AbstractExtract", "ReportCriteria", "Extract3", "Sheet5", _
"R&R Stage Definitions")).Select
xlApp.Sheets("AbstractExtract").Activate
xlApp.ActiveWindow.SelectedSheets.Delete
xlApp.Columns("B").Select
For Each c In Selection
If c.Hyperlinks.Count > 0 Then
txt = c.Hyperlinks(1).Address
c.Value = txt
Next
For Each c In Worksheets("Data").Range("B").Cells
If c.Value = "N/A" Then c.Value = ""
Next
For Each c In Worksheets("Data").Range("F2:F1000").Cells
If c.Value > "" Then c.Value = StrConv(c, vbProperCase)
Next
For Each c In Worksheets("Data").Range("J2:J1000").Cells
If c.Value > "" Then c.Value = StrConv(c, vbProperCase)
Next
xlApp.ActiveWorkbook.Save
xlApp.ActiveWorkbook.Close
Set xlApp = Nothing
Thanks!!