I have code that is sorting data and putting it in various cells on different sheets in Excel. I created the code in a test database and it worked like a charm. However, putting it into the database that will distributed for use it errors. The relevant code is below.
I get a Type Mismatch Error when trying to run this and it makes no sense. I defined rData as a Range, if I understand CurrentRegion it returns a Range so I don't understand the issue. Like I said I copy and pasted the code exactly as it stands in the test database that works. The whole code being used is over 800 lines so if you need more information I will provide as much as I can.
Code:
Dim xl As Excel.Application
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim rRow As Range
Dim cntr As Integer
Dim rData As Range
Set xl = New Excel.Application
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qryCharts")
Set rst = qdf.OpenRecordset()
With xl
Dim wkbook As Workbook
'.Visible = True
.UserControl = True
Set wkbook = .Workbooks.Open("R:\0.2.2 Procurement Dept - Shared\3.0 Groups\SQD\FTTQ Charts-Copy.xlsx")
.Worksheets("ChartData").Range("A2").CopyFromRecordset rst
'.Worksheets("ChartData").Range("A:D").RemoveDuplicates Columns:=4
Dim OutSheet As Worksheet
On Error Resume Next
.DisplayAlerts = False
wkbook.Worksheets("ChartDataFiltered").Delete
Err.Clear
On Error GoTo 0
Set OutSheet = wkbook.Sheets.Add
OutSheet.Name = "ChartDataFiltered"
Set rData = .Sheets("ChartData").Cells(1, 2).CurrentRegion 'Receive Error on this line
I get a Type Mismatch Error when trying to run this and it makes no sense. I defined rData as a Range, if I understand CurrentRegion it returns a Range so I don't understand the issue. Like I said I copy and pasted the code exactly as it stands in the test database that works. The whole code being used is over 800 lines so if you need more information I will provide as much as I can.