The loop I have is contained within a "with" block, however the .application you mention is contained with an If because I do not need to use it on every single cell, just the ones that are numeric. Do you suggest that I use a with block just for that line?
I can see that. But it's not good practice. If you're going to nest things, maybe like this
Code:
With Me.txtSomething
.backcolor = vbRed
.backstyle = transparent
.etc
If Me.txtNothing = "test" Then
do some stuff
End If
.etc
End With
Not
Code:
With Me.txtSomething
.backcolor = vbRed
.backstyle = transparent
If Me.txtNothing = "test" Then
do some stuff
.etc (part of the With block - it's orphaned because it's inside an If)
End If
End With
I try very hard not to nest things like If within With, but would nest a With inside an If or Select Case - hope you see the difference. It's quite possible that your problem went away not because of the explicit reference, but because the new code eliminated the issue of an incomplete block. For example, if you do a For loop and incorrectly nest an If or With, you can get a Next Without For error even though the Next is there somewhere. I have used Automation like this with Excel and didn't have to use the full reference when I've already declared a variable for it. Example
Code:
Set xls = xlw.Worksheets(shtName)
With xls
'delete data in the ranges in case fewer records are being entered
.Range(rnge1).ClearContents
.Range(rnge2).ClearContents
End With
Set xlc = xls.Range(cellStart) ' this is the top left cell into which data goes
rst.MoveFirst
Notice when xlc is set, it does not use the full reference for the range - it uses the xls object variable. Think about it - why would you have to declare an object then not be able to use it somewhere? Because there's a problem with the syntax, not the object.
With your new code, you took care of some of the things I would have suggested. For what it's worth, here's my take. Not that there's anything wrong with what you have now - just showing another approach. Some notes in there & if I had used the CurrentDb.Execute method, it would be different again.
Code:
Private Sub IAprep()
Dim fromname As String, toname As String '[I wouldn't bother with this as the names are not too long, qryname As String]
Dim objXLApp As Object, objXLWb As Object, objXLWs As Object
Dim expfile1 As String, expfile2 As String, strsql As String
Dim strsql1 As String, currval As String
Dim rst As DAO.Recordset, rst1 As DAO.Recordset
'move data file to linked location
fromname = "C:\Users\jslay\Desktop\HDL to Outlet movement process folder\Consolidated " & Format(Date, "YY-MM-DD") & ".xlsx"
toname = "C:\Users\jslay\Desktop\HDL to Outlet movement process folder\Linked files\Consolidatedprep.xlsx"
FileCopy fromname, toname
KillProperly (fromname)
'create data tables
DoCmd.SetWarnings False
DoCmd.OpenQuery "qry_10_mktbl_processdata", acNormal, acEdit 'if this is an action query, I'd use the .Execute method of the CurrentDb Object, then warnings don't have to be cycled on/off, can't remain off if an error causes abnormal execution, plus the method generates an error which can be customized.
DoCmd.OpenQuery "qry_11_mktbl_totalsdata", acNormal, acEdit
KillProperly (toname)
DoCmd.SetWarnings True
'create working files from templates
fromname = "C:\Users\jslay\Desktop\HDL to Outlet movement process folder\Templates\Consolidated Template.xlsx"
expfile1 = "C:\Users\jslay\Desktop\HDL to Outlet movement process folder\HDL to Outlet process data - Final - " & Format(Date, "YY-MM-DD") & ".xlsx"
FileCopy fromname, expfile1
fromname = "C:\Users\jslay\Desktop\HDL to Outlet movement process folder\Templates\Consolidated Totals Template.xlsx"
expfile2 = "C:\Users\jslay\Desktop\HDL to Outlet movement process folder\HDL to Outlet Totals - " & Format(Date, "YY-MM-DD") & ".xlsx"
FileCopy fromname, expfile2
'set and create data sets
strsql = "SELECT tbl_01_hdltooutletprocessdata.* FROM tbl_01_hdltooutletprocessdata;"
strsql1 = "SELECT tbl_02_totals.* FROM tbl_02_totals;"
Set rst = CurrentDb.OpenRecordset(strsql, dbOpenSnapshot)
Set rst1 = CurrentDb.OpenRecordset(strsql1, dbOpenSnapshot)
'open excel for first file and copy data
Set objXLApp = CreateObject("Excel.Application")
objXLApp.Application.Visible = True
Set objXLWb = objXLApp.Workbooks.Open(expfile1)
Set objXLWs = objXLWb.Worksheets("Sheet1")
objXLWs.Range("A2").CopyFromRecordset rst
'apply data format changes
objXLApp.ActiveSheet.Range("H2").Select
Do While objXLApp.Application.ActiveCell.Value <> ""
currval = objXLApp.Application.ActiveCell.Value
If IsNumeric(currval) Then
currval = CLng(currval)
objXLApp.Application.ActiveCell.Value = currval
End If
objXLApp.Application.ActiveCell.Offset(1, 0).Select
Loop
objXLApp.Application.Range("A2").Select
objXLApp.Application.Range("A:J").AutoFilter Field:=8, Criteria1:="Invalid STS Item Number"
objXLWb.Save
objXLWb.Close
'open second file and copy data
Set objXLWb = objXLApp.Workbooks.Open(expfile2)
Set objXLWs = objXLWb.Worksheets("Sheet1")
objXLWs.Range("A3").CopyFromRecordset rst1
objXLWb.Save
objXLWb.Close
objXLApp.Quit
Exit_iaprep:
Set objXLApp = Nothing
Set objXLWb = Nothing
Set objXLWs = Nothing
rst.Close
rst1.Close
Set rst = Nothing 'don't need to check if it's Nothing & no reason it would be - won't cause an error anyway
Set rst1 = Nothing
Exit Sub 'why this and the line label Exit_iaprep (unless you have an error handler in this sub that you didn't post)?
End Sub