Excel VBA Tri
Question
Je suis nouveau pour exceller VBA.Cela semble que cela devrait être simple cependant. J'ai besoin de trier une feuille de calcul d'une base de données d'accès.
Voici mon code.
Actuellement, je reçois une erreur 1004."Méthode de plage d'objet _global a échoué" sur "myRange= plage (sélection)"
Toute aide est grandement appréciée.
Sub sortBacklog()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim myWorkSheet As Worksheet
Dim myRange As Range
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open("C:\Users\gephilli\Desktop\Dispatch\SAP_Backlog.xls")
Set myWorkSheet = myWorkbook.Sheets(1)
myWorkSheet.Activate
myWorkSheet.Select
Range("B1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
myRange = Range(Selection)
myWorkSheet.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("PLS Depot Backlog Report").Sort.SortFields.Add Key _
:=Range("F2:F20491"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("PLS Depot Backlog Report").Sort
.SetRange Range(myRange)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Save
ActiveWorkbook.Close
appExcel.Quit
Set myWorkSheet = Nothing
Set myWorkbook = Nothing
Set appExcel = Nothing
End Sub
La solution
Il est normalement inutile de sélectionner quoi que ce soit dans VBA.C'est ce que font les macros enregistrées, mais ce n'est pas la manière efficace.Essayer: Set MyRange= Stade1.Range ("B1", feuille1.Range ("B1"). End (Xlowown) .end (xltoright))
Le problème que vous voyez pourrait réellement être le manque d'un "jeu" dans la ligne où l'erreur se produit.Sans «Set» Excel tente de travailler sur des contenus sur la plage, avec «Set», cela fonctionne sur les objets de la plage elles-mêmes.
Autres conseils
Prendre en compte des commentaires et une meilleure pratique J'ai ré-écrit votre code avec des commentaires:
Sub sortBacklog()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Changed to late binding - no need to set reference to Excel '
'and not reliant on a specific version of Excel. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim appExcel As Object
Dim myWorkbook As Object
Dim myWorkSheet As Object
Dim myRange As Object
Set appExcel = CreateObject("Excel.Application")
'appExcel.Visible = True
Set myWorkbook = appExcel.Workbooks.Open("C:\Users\gephilli\Desktop\Dispatch\SAP_Backlog.xls")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'You may want to change this to look at a named sheet.
'e.g. myWorkbook.Sheets("PLS Depot Backlog Report") '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set myWorkSheet = myWorkbook.Sheets(1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Everything between 'With' and 'End With' that '
'starts with a . (period) will apply to myWorksheet. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''
With myWorkSheet
'''''''''''''''''''''''''''''''''''''''''''
'Get range from B1 to last cell on sheet. '
'''''''''''''''''''''''''''''''''''''''''''
Set myRange = .Range(.Cells(1, 2), .Cells(.Cells.Find("*", , , , 1, 2).Row, .Cells.Find("*", , , , 2, 2).Column))
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Everything between 'With' and 'End With' that '
'starts with a . (period) will apply to myWorksheet.Sort. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
With .Sort
.SortFields.Clear
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'As using late binding Access won't understand Excel values so: '
'xlSortOnValues = 0 xlYes = 1 '
'xlAscending = 1 xlTopToBottom = 1 '
'xlSortNormal = 0 xlPinYin = 1 '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
.SortFields.Add _
Key:=myRange.offset(, 6).Resize(, 1), _
SortOn:=0, _
Order:=1, _
DataOption:=0
.SetRange myRange
.Header = 1
.MatchCase = False
.Orientation = 1
.SortMethod = 1
.Apply
End With
End With
With myWorkbook
.Save
.Close
End With
appExcel.Quit
Set myWorkSheet = Nothing
Set myWorkbook = Nothing
Set appExcel = Nothing
End Sub