I'm looking for a bit of best practice advice on Excel VBA code. I have a report built in Excel, with a control sheet at the front where the user selects a number of variables from drop down boxes and data is compiled based on the selection via SQL through VBA.
The code I have is very simple it copies the data into a worksheet and formats it. However when building the sheet this worked fine as I was stepping through the sheets to makes sure the code was doing what I wanted. Now it is finished I want to perform the code but without it jumping around the worksheets. It looks so much better if it stays on the control sheet until the code has finished.
However I can't seem to perform the same task without referencing the sheet that's being formatted?
Below is the code used on one worksheet to copy data and format. It works up until I need it to select Range("B5:K5").Select
then it performs this on my Control sheet.
On Error Resume Next
Sheets("Account Details").ShowAllData
Sheets("Account Details").Range("B5:K7500").Cells.ClearContents
Sheets("Account Details").Range("B5:K7500").Borders.LineStyle = xlNone
Sheets("Account Details").Range("B5").CopyFromRecordset rst2
Sheets("Account Details").Range("B5:K5").Select
Sheets("Account Details").Range(Selection, Selection.End(xlDown)).Select
Sheets("Account Details").Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Sheets("Account Details").Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Sheets("Account Details").Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous
Sheets("Account Details").Selection.Borders(xlEdgeRight).LineStyle = xlContinuous
Sheets("Account Details").Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
Sheets("Account Details").Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
Sheets("Account Details").Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
Sheets("Account Details").Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
Sheets("Account Details").Range("A1").Select
The second part I wanted to ask was on VBA best practice. Not that I know how, but I can't help thinking that keep referencing the worksheet is a little messy and not really needed. I've tried a couple of With statements but with no joy.
Any help would be much appreciated.
Thanks in advance!