Ouput changing column properties using a module

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I have coded using VBA to output a query to a sheet in a protected workbook
and it works fine. I added code to output a 2nd query to another sheet in
the same workbook and when it runs it causes various columns on the first
sheet to change from their format to a date format. If I comment out the
code to output to the 2nd sheet the first sheet goes back to normal.

Here is the code I am using for outputting to the 2nd sheet:

' Write the data to the Termed Detail worksheet.
Set objSht = objWkb.Worksheets("TermedDetails")
objSht.Range("A2").CopyFromRecordset rs1
 
I just ran into something similar and here is how I fixed it. Using your code,

objSht.Select
ActiveSheet.Range("A2").CopyFromRecordset rs1

Apparently, just naming the sheet in your SET statement doesn't actually
activate/select the sheet.
 
Alternatively,

With objWkb.Worksheets("TermedDetails")
.Range("A2").CopyFromRecordset rs1
End With
 
Pendragon,
By just adding the objSht.Select it cured the problem. This was a
great help.

Thanks.
 
Back
Top