Ordinamento VBA di Excel.
Domanda
Sono nuovo per Excel VBA.Sembra che dovrebbe essere semplice però. Ho bisogno di ordinare un foglio di calcolo da un database di accesso.
Ecco il mio codice.
Attualmente ottengo un errore 1004."Metodo di intervallo di oggetto _global fallito" su "myrange= range (selezione)"
Qualsiasi aiuto è molto apprezzato.
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
. Soluzione
Normalmente è inutile selezionare nulla in VBA.Questo è ciò che fanno le macro registrate, ma non è il modo efficiente.Provare: Set myrange= foglio1.range ("B1", Sheet1.range ("B1"). END (XLDown) .End (XLtoright))
Il problema che stai vedendo potrebbe effettivamente essere la mancanza di un "set" nella linea in cui si verifica l'errore.Senza un "set" di Excel tenta di lavorare sui contenuti della gamma, con "set" funziona sugli oggetti della gamma stessi.
Altri suggerimenti
Tenendo conto dei commenti e della pratica migliore che ho re-scritto il tuo codice con commenti:
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
.