This seems incredibly simple and straight forward, but no matter how much testing and searching I do I am unable to resolve. I currently have scripting that turns on AutoFilter without selecting anything, but I thought I could copy the data and paste into a new worksheet without the data being selected. However, after it completes the action it does show columns A:K as selected. I have been unable to figure out how to deselect those columns on the worksheet nor have I been able to find out how to copy and paste without them being selected in the first place. Any guidance is appreciated. Thanks.
Sub RunScript()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Data")
Sheets.Add.Name = "copy"
Dim galreqws As Worksheet
Set galreqws = ThisWorkbook.Sheets("copy")
Sheets("Data").Select
Range("A:K").AutoFilter
ws1.Range("A1:K1000").Copy Destination:=Sheets("copy").Range("A1:K1000")
galreqws.Delete
'Sheets("Data").Select
Range("A:K").AutoFilter
Range("A:K").Select
Sheets("Buttons").Select
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
It's generally bad practice to use .Select
unless you really intend the user to see a selected range. As far as I know there are no actions where .Select
is required.
The following is a suggestion of a few things you could do to improve your code:
Sub RunScript()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wsCurrent As Worksheet
Set wsCurrent = ActiveSheet
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Data")
Dim wsGalreq As Worksheet
' Note that .Add will activate the new sheet so we'll
' need to reactivate the worksheet that was previously active
'Set wsGalreq = Worksheets.Add
'wsGalreq.Name = "copy"
Set wsGalreq = Worksheets("copy")
wsData.Range("A:K").AutoFilter 1, "Supplier A"
' Find the used range in columns A to K and copy over starting
' at cell A1 of wsGalreq
Intersect(wsData.UsedRange, wsData.Range("A:K")).Copy Destination:=wsGalreq.Range("A1")
' Doubt deleting the new sheet was intentional?
' wsGalreq.Delete
' Now change the view to the desired worksheet, uncomment one:
'Worksheets("Buttons").Select
'wsCurrent.Activate
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub