Here's another situation I'm unable to resolve.
In the course of coding a subroutine in an Access 2010 DB, I'm trying to manipulate an Excel spreadsheet so I can extract the data I need for the DB.
I first opened an Excel spreadsheet and began recording a macro to enter a formula into a cell which references other cells, then copy that formula down a column, and finally replacing the original column with the modified data.
When I stopped the recording, I attempted to port that code to the Access DB code window. I ran into at least one problem. Since I didn't get past that one, I don't yet know if others will crop up.
Here's a code snippet. I want to extract the gender info and convert it to just one letter, M or F. The spreadsheet contains the full identifiers: Male and Female. Then I want to copy the modified data to the original column as paste value only. The Access table I need to enter this data into defines the gender column as text with one character.
The gender data in the spreadsheet is located in column K which has a header. I want to work in an unused column in the spreadsheet. In this case it's column AN.
The code fails at the line colored red. The error message is:
In the course of coding a subroutine in an Access 2010 DB, I'm trying to manipulate an Excel spreadsheet so I can extract the data I need for the DB.
I first opened an Excel spreadsheet and began recording a macro to enter a formula into a cell which references other cells, then copy that formula down a column, and finally replacing the original column with the modified data.
When I stopped the recording, I attempted to port that code to the Access DB code window. I ran into at least one problem. Since I didn't get past that one, I don't yet know if others will crop up.
Here's a code snippet. I want to extract the gender info and convert it to just one letter, M or F. The spreadsheet contains the full identifiers: Male and Female. Then I want to copy the modified data to the original column as paste value only. The Access table I need to enter this data into defines the gender column as text with one character.
The gender data in the spreadsheet is located in column K which has a header. I want to work in an unused column in the spreadsheet. In this case it's column AN.
Code:
Dim impfile as object
Set impfile = CreateObject("Excel.Sheet")
impfile.Application.Workbooks.Open filepathXL, 0, False [COLOR=#4E9A06]'Where filepathXL is the full path and name of the Excel file[/color]
impfile.Application.ActiveWorkbook.ActiveSheet.Range("AN2").Select
impfile.Application.ActiveWorkbook.ActiveSheet.Range("AN2").Formula = "=IF(RC[-29]=""Male"",""M"",""F"")"
[COLOR=#EF2929]impfile.Application.ActiveWorkbook.ActiveSheet.Selection.AutoFill Destination:=Range("AN2:AN1220"), Type:=xlFillDefault[/color]
impfile.Application.ActiveWorkbook.ActiveSheet.SelectionRange("AN2:AN1220").Select
impfile.Application.ActiveWorkbook.ActiveSheet.SelectionSelection.Copy
impfile.Application.ActiveWorkbook.ActiveSheet.SelectionRange("K2").Select
impfile.Application.ActiveWorkbook.ActiveSheet.SelectionSelection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone,SkipBlanks:=False, Transpose:=False
impfile.Application.ActiveWorkbook.ActiveSheet.SelectionRange("AN2:AN1220").Select
impfile.Application.ActiveWorkbook.ActiveSheet.SelectionSelection.Delete
impfile.Application.ActiveWorkbook.Save
impfile.Application.Quit
End Sub
Run-time error '1004':
Method 'Range' of object '_Global' failed
Method 'Range' of object '_Global' failed