Code:Sub ReorgBy3Criteria() Rem 0 error handling On Error GoTo TheEnd: Rem 1 worksheet info Call DieseArbeitsmappe1.FillMeGlobsUpMate ' Global variables filled for example that for open daily Diet Protokol Dim objwinToSort As Object: Set objwinToSort = Windows("" & DieseArbeitsmappe1.ProWb.Name & "") 'Mainly for convenience, but would give possibility of referring to a sheet "quasi" as the active even if you do not have got it "active" in front of you Dim wksToSort As Worksheet: Set wksToSort = DieseArbeitsmappe1.ProWb.Worksheets("" & "Sheet1" & "") Rem 2 Range for sort is based on Window selection. For convenience only rows selection is necerssary Dim StRow As Long, stClm As Long, StpRow As Long, StpClm As Long Let StRow = objwinToSort.Selection.Row: Let stClm = 1 'objwinToSort.Selection.Column ' Select any column or columns of rows to sort Let StpRow = StRow + objwinToSort.Selection.Rows.Count - 1: Let StpClm = 3488 ' 3482 '454 '99 Dim rngToSort As Range: Set rngToSort = wksToSort.Range(CL(stClm) & StRow & ":" & CL(StpClm) & StpRow) ' Use column letter function for column letters Dim ArrrngOrig() As Variant: Let ArrrngOrig() = rngToSort.Value ' This is used as a back up to restore the original range Let Application.EnableEvents = False ' This is necerssary to turn off some event coding which I have which springs into action when anything is done in the worksheet Rem 3 VBA Range.Sort Method ' xlDescending Biggest at Top H Kcal J Fett L eiweiss 'rngToSort.Sort Key1:=wksToSort.Columns("h"), order1:=xlDescending, Key2:=wksToSort.Columns("j"), order2:=xlDescending, Key3:=wksToSort.Columns("l"), order3:=xlDescending 'X Nat 'Standard unter ---- Kcal Highest H ,at Top , second most J Fett , highest at Top , third Natrium X , most at top rngToSort.Sort Key1:=wksToSort.Columns("H"), order1:=xlDescending, Key2:=wksToSort.Columns("J"), order2:=xlDescending, Key3:=wksToSort.Columns("X"), order3:=xlDescending 'X Nat Let Application.EnableEvents = True Rem 4 Msg Box check if all is well, if not put original enties back in. COMMENTED OUT FOR SPEED TESTS Dim Response As Integer 'In VBA Butons "yes is 6, 7 is "no" Let Response = MsgBox(Prompt:="Is all OK?", Buttons:=vbYesNo, Title:="File Check") ' Displays a message box with the yes and no options. If Response = vbYes Then 'Do nothing, that is to say just carry on after End of If ' all is well - carry on after End If Else Let Application.EnableEvents = False Let rngToSort.Value2 = ArrrngOrig() 'Full repair!!Put back as it was Let Application.EnableEvents = True End If Exit Sub ' Routine end if no errors____________________________________________________________________ TheEnd: ' Error handling code section Let Application.EnableEvents = True ' In the Case of an error I want to ensure that I turn back on my normal events. This is necerssary incase the error occured between after a .EnableEvents = False and before a .EnableEvents = True MsgBox Prompt:=Err.Number & vbCr & vbLf & Err.Description End Sub 'ReorgBy3Criteria '




Reply With Quote
Bookmarks