Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Rodolfo Ramirez   on Dec 11 In MS Office Category.

  
Question Answered By: Reginald Thomas   on Dec 11

Very inconvenient for Excel to treat hyphens
in this way, they're unfortunately very common in the data  I'm
working with, and I have numerous look-up routines that rely on
string comparison to optimise performance. I've introduced my own
sorting proc that seems to resolve the problem:

Private Sub MySort(rngRange As Range, intColSort As Integer)

Dim strFormula As String
Dim intColTemp As Integer
Dim blnScreenUpdating As Boolean

'Register current screen updating status
blnScreenUpdating = Application.ScreenUpdating

'Turn off screen updating
Application.ScreenUpdating = False

'Initialise temporary column
intColTemp = intColSort + 1
rngRange.Worksheet.Columns(intColTemp).Insert xlShiftToRight

'Apply formula to force upper case and substitute - with $
strFormula = "=UPPER(SUBSTITUTE(RC[-1], "
strFormula = strFormula & Chr(34) & "-" & Chr(34)
strFormula = strFormula & ", "
strFormula = strFormula & Chr(34) & "$" & Chr(34)
strFormula = strFormula & "))"
rngRange.Worksheet.Cells(rngRange.Rows(1).Row,
intColTemp).FormulaR1C1 = strFormula
rngRange.Worksheet.Cells(rngRange.Rows(1).Row,
intColTemp).AutoFill rngRange.Worksheet.Range(rngRange.Worksheet.Cells
(rngRange.Rows(1).Row, intColTemp), rngRange.Worksheet.Cells
(rngRange.Rows(rngRange.Rows.Count).Row, intColTemp))

'Perform sort using temporary column
rngRange.Sort rngRange.Worksheet.Cells(rngRange.Rows(1).Row,
intColTemp)

'Delete temporary column
rngRange.Worksheet.Columns(intColTemp).Delete xlShiftToLeft

'Reset screen updating
Application.ScreenUpdating = blnScreenUpdating

End Sub

Share: