Petites fonctions de conversion de coordonnées Excel assez robustes.
1 > Fonction Cell2Col(coordonnées cellule) : renvoie le numéro de la colonne de la cellule
ex : Cell2Col("AB59") renverra 28
2 > Fonction Cell2Row(coordonnées cellule) : renvoie le numéro de la ligne de la cellule
ex : Cell2Col("AB59") renverra 59
3 > Fonction Coord2Cell(colonne,ligne) : renvoie la coordonnée de la cellule correspondante
ex : Coord2Cell(5,3) renverra "E3"
NB :
- Elles permettent d'éliminer les espaces blancs et charactères indésirables automatiquement. Par exemple " 'A3' " sera interprété comme "A3".
- En cas de dépassement de capacité ou tout autre problème, elles renvoient toutes -1
Notes de l'auteur :
J'ai créé ces fonctions, car je n'avais rien vu de satisfaisant auparavant pour faire ça. Voilà, j'éspère que ça aidera certains d'entre vous. Bonne prog à tous.
Cyrus the great
Source / Exemple :
Option Explicit
Private Const ALPHABETLETTERS = 26
Private Const ASCIICODE_A = 65
Private Const ASCIICODE_Z = 91
Private Const ASCIICODE_0 = 48
Private Const ASCIICODE_9 = 57
Private Const LONGTYPEMAXIMUM = 2147483647
Private Const RETURNERROR = -1
Rem *********************************************
Rem * *
Rem * Excel coords conversions (robust version) *
Rem * *
Rem *********************************************
Rem * Dev : Cyrus the great * 19/07/2002 *
Rem *********************************************
Public Function Cell2Col(ByVal cellString As String) As Long
Dim i As Integer
Dim weight As Integer
Dim result As Long
' Return an error if the entry is void
If Len(cellString) = 0 Then
Cell2Col = RETURNERROR
Exit Function
End If
' Remove left invalid characters
While Len(cellString) > 1 And (Asc(Left(cellString, 1)) < ASCIICODE_A Or Asc(Left(cellString, 1)) > ASCIICODE_Z)
cellString = Right(cellString, Len(cellString) - 1)
Wend
' Remove right invalid characters
While Len(cellString) > 1 And (Asc(Right(cellString, 1)) < ASCIICODE_A Or Asc(Right(cellString, 1)) > ASCIICODE_Z)
cellString = Left(cellString, Len(cellString) - 1)
Wend
' Return an error if the entry is not valid
For i = 1 To Len(cellString)
If Asc(Mid(cellString, i, 1)) < ASCIICODE_A Or Asc(Mid(cellString, i, 1)) > ASCIICODE_Z Then
Cell2Col = RETURNERROR
Exit Function
End If
Next
' Convert column string to number
weight = 0
result = 0
While Len(cellString) > 0
' Return an error in case of an overflow
If result + (Asc(Right(cellString, 1)) - (ASCIICODE_A - 1)) * ALPHABETLETTERS ^ weight > LONGTYPEMAXIMUM Then
Cell2Col = RETURNERROR
Exit Function
End If
' Computate each letter value with the weights method
result = result + (Asc(Right(cellString, 1)) - (ASCIICODE_A - 1)) * ALPHABETLETTERS ^ weight
weight = weight + 1
cellString = Mid(cellString, 1, Len(cellString) - 1)
Wend
' Return result
Cell2Col = result
End Function
Public Function Cell2Row(ByVal cellString As String) As Long
Dim i As Integer
Dim result As Long
' Return an error if the entry is void
If Len(cellString) = 0 Then
Cell2Row = RETURNERROR
Exit Function
End If
' Remove left invalid characters
While Len(cellString) > 1 And (Asc(Left(cellString, 1)) < ASCIICODE_0 Or Asc(Left(cellString, 1)) > ASCIICODE_9)
cellString = Right(cellString, Len(cellString) - 1)
Wend
' Remove right invalid characters
While Len(cellString) > 1 And (Asc(Right(cellString, 1)) < ASCIICODE_0 Or Asc(Right(cellString, 1)) > ASCIICODE_9)
cellString = Left(cellString, Len(cellString) - 1)
Wend
' Return an error if the entry is not valid
If Val(cellString) <= 0 Or Val(cellString) > LONGTYPEMAXIMUM Then
Cell2Row = RETURNERROR
Exit Function
End If
' Convert column string to number
result = Val(cellString)
' Return result
Cell2Row = result
End Function
Public Function Coord2Cell(ByVal col As Integer, ByVal row As Integer) As String
Dim puissance As Integer
Dim quotient As Integer
Dim result As String
' Return an error if the entry is not valid
If col <= 0 Or col > LONGTYPEMAXIMUM Or row <= 0 Or row > LONGTYPEMAXIMUM Then
Coord2Cell = RETURNERROR
Exit Function
End If
' Convert column number to string
result = ""
quotient = col
puissance = 1
' Determinate each letter from the given value
While Int((quotient - 1) / 26 ^ puissance) > 26
puissance = puissance + 1
Wend
Do
If (quotient - 1) / 26 ^ puissance >= 1 Then
result = result + Chr((ASCIICODE_A - 1) + Int((quotient - 1) / 26 ^ puissance))
quotient = quotient - Int((quotient - 1) / 26 ^ puissance) * 26 ^ puissance
puissance = puissance - 1
End If
Loop While quotient > 26
result = result + Chr((ASCIICODE_A - 1) + quotient Mod (ALPHABETLETTERS + 1))
' Convert row number to string
result = result + Format(Str(row), "#")
' Return result
Coord2Cell = result
End Function
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.