IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Concaténer un poids fort et faible pour former un Long [XL-2003]


Sujet :

Macros et VBA Excel

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Architecte technique
    Inscrit en
    Décembre 2010
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2010
    Messages : 34
    Points : 26
    Points
    26
    Par défaut Concaténer un poids fort et faible pour former un Long
    Bonjour,

    J'ai 2 valeurs des 5 caractères, ces deux valeurs représentent un LONG de 10 caractères.

    J'ai réalisé le code suivant en VB :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Dim Resultat As Long
    Dim lpFort As Long, lpFaible As Long
    Dim var1 As Integer, var2 As Integer
     
    lpFort = var1 << 16
    lpFaible = var2 And &HFFFF
    Resultat = lpFort Or lpFaible
    Il fonctionne mais j'ai besoin de coder la même chose en VBA.
    Je ne trouve pas l''instruction de décalage "<< 16"

    Pouvez-vous m'aider
    Merci

  2. #2
    Expert éminent
    Avatar de Qwazerty
    Homme Profil pro
    La très haute tension :D
    Inscrit en
    Avril 2002
    Messages
    3 906
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : La très haute tension :D
    Secteur : Service public

    Informations forums :
    Inscription : Avril 2002
    Messages : 3 906
    Points : 8 539
    Points
    8 539
    Par défaut
    Salut
    Pense a mettre une balise code en sélectionnant ton code et en cliquant sur le bouton # dans la barre d’édition.

    Regarde du coté des fonctions shl et shr.

    [Edit]
    Zut je confonds avec Delphi :s
    Trouvé sur le net :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    '***********************************************************
    '                    SHL
    '  Makes Shift Left N times in a Long Unsigned A
    '************************************************************
    Function Shl(A As Long, N As Byte) As Long
    Dim Erro As Integer
     
    Shl = 0
    If N >= 32 Then Exit Function ' SHL with N bits >=32 is always 0
     
    ' Auxiliary Array Initialization
    On Error Resume Next
    If UBound(CutLeft) = 0 Then N = N
    Erro = Err.Number
    On Error GoTo 0
    If Erro <> 0 Then IniShift
     
    If A And OneBitOn(N) <> 0 Then ' Bit (32-N) Is On? (32 is the most significant bit)
      '
      ' a) Turn off the first N+1 bits
      ' b) Multiply by 2^N
      ' c) Turn on the sign bit (the most significant bit)
      '
       A = ((A And CutLeft(N + 1)) * OneBitOn(31 - N)) Or &H80000000
    Else
     '
     ' a) Turn off the first N bits
     ' b) Multiply by 2^N
     '
       A = A And CutLeft(N) * OneBitOn(31 - N)
    End If
    Shl = A
    End Function
    '***********************************************************
    '                   SHR
    '  Make Shift Right N times in a Unsigned Long A
    '************************************************************
    Function Shr(A As Long, N As Byte) As Long
    Dim Erro As Integer
    Dim I As Integer
    Shr = 0
    If N >= 32 Then Exit Function  ' SHR with N bits >=32 is always 0
     
    ' Auxiliary Array Initialization
    On Error Resume Next
    If UBound(CutLeft) = 0 Then N = N
    Erro = Err.Number
    On Error GoTo 0
    If Erro <> 0 Then IniShift
     
    If A < 0 Then  ' If Bit 32 From "Unsigned" Long is On
     
       ' a) Turn off Bit 32 (Most Significant Bit)
       ' b) Make Integer Division by 2^N
       ' c) Turn on Bit 32-N (N+1o. Bit from Left)
     
       A = ((A And &H7FFFFFFF) \ OneBitOn(31 - N)) Or OneBitOn(N)
    Else
       ' a) Just Make Integer Division by 2^N
       A = A \ OneBitOn(31 - N)
    End If
    Shr = A
    End Function
    [/Edit]

    ++
    Qwaz

  3. #3
    pgz
    pgz est déconnecté
    Expert éminent Avatar de pgz
    Homme Profil pro
    Développeur Office VBA
    Inscrit en
    Août 2005
    Messages
    3 692
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Office VBA
    Secteur : Conseil

    Informations forums :
    Inscription : Août 2005
    Messages : 3 692
    Points : 6 591
    Points
    6 591
    Par défaut
    Bonjour.

    Citation Envoyé par Toto_le_héros38 Voir le message
    J'ai 2 valeurs des 5 caractères, c'est deux valeurs représentes un LONG de 10 caractères.
    C'est certainement assez simple, mais je ne comprends pas le pb. Peux-tu être plus explicite? Un petit exemple, peut-être.

    Cordialement,

    PGZ

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Architecte technique
    Inscrit en
    Décembre 2010
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2010
    Messages : 34
    Points : 26
    Points
    26
    Par défaut
    Bonjour PGZ,

    Voici 2 valeurs qui me sont descendues d'un automate programmable (12857 et -7842) ces 2 valeurs représente le poids fort et faible de la valeur suivante : 1236132190.
    Dans l'application en VB6, j'utilise le code montré précédemment.
    Mais je fais également un export en Excel et il me faut réécrire ce même code en VBA.
    L'instruction de décalage "<< 16" n'est pas comprise par le VBA d'Excel.

    J'espère avoir été plus claire

    Merci par avance pour tes lumières

  5. #5
    pgz
    pgz est déconnecté
    Expert éminent Avatar de pgz
    Homme Profil pro
    Développeur Office VBA
    Inscrit en
    Août 2005
    Messages
    3 692
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Office VBA
    Secteur : Conseil

    Informations forums :
    Inscription : Août 2005
    Messages : 3 692
    Points : 6 591
    Points
    6 591
    Par défaut
    Bonsoir.

    Je ne m'y retrouve pas avec tes chiffres, mais ce que tu as essayé de faire devrait s'écrire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    lpFort = (var1 And (2^16-1))* 2^16
    lpFaible = var2 And (2^16-1)
    Resultat = lpFort + lpFaible
    Ou qqc dans ce goût là.

    PGZ

  6. #6
    Modérateur
    Avatar de AlainTech
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Mai 2005
    Messages
    4 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 70
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2005
    Messages : 4 235
    Points : 24 327
    Points
    24 327
    Par défaut
    Quand j'applique l'interprétation de ton code aux nombres décimaux 12857 (&H3239) et -7842 (&HE15E si on ne garde que les 4 derniers octets) , j'obtiens &H3239E15E soit le nombre décimal 842654046.

    Es-tu certain de tes données?
    Comment peux-tu être certain d'obtenir un nombre (décimal?) de 10 chiffres?

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Architecte technique
    Inscrit en
    Décembre 2010
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2010
    Messages : 34
    Points : 26
    Points
    26
    Par défaut Voici la Solution à mon problème
    Bonjour,

    Merci à tous pour votre aide , avec vos différentes réponses voici la solution à mon problème :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Sub macro1()
     
    Dim Resultat As Long
    Dim lpFort As Long, lpFaible As Long
     
    lpFaible = Ranger("A3")                'A3 = -7842
    lpFaible = lpFaible And 65535
     
    lpFort = shr(Ranger("A4"), 16)       'A4 = 18861
     
    Resultat = lpFort Or lpFaible          '=> 1236132190
     
    End Sub
     
     
    Public Function shr(ByVal Value As Long, Shift Value As Byte) As Long
        shr = Value
        If Shift > 0 Then
             shr = Int (shr * (2 ^ Shift))
        End If
    End Function


    Bonne soirée

    @+

  8. #8
    pgz
    pgz est déconnecté
    Expert éminent Avatar de pgz
    Homme Profil pro
    Développeur Office VBA
    Inscrit en
    Août 2005
    Messages
    3 692
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Office VBA
    Secteur : Conseil

    Informations forums :
    Inscription : Août 2005
    Messages : 3 692
    Points : 6 591
    Points
    6 591
    Par défaut
    Bonsoir.

    Cette fois nous trouvons le même résultat. Le poids fort était 18861 et non pas 12857.

    Cordialement,

    PGZ

  9. #9
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2011
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2011
    Messages : 141
    Points : 414
    Points
    414
    Par défaut Union typée en Excel VBA
    Union typée en Excel VBA

    Voici une solution possible sans puissance de 2, ni multiplication / division, ni shift dans Excel VBA toute version.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Option Explicit
    Type Long32Type
        value As Long
    End Type
    Type Long2x16Type
        value16 As Integer
        value32 As Integer
    End Type
    Function IntToLong(ByVal msw As Integer, ByVal lsw As Integer) As Long
    Dim long2x16 As Long2x16Type, long32 As Long32Type
        long2x16.value16 = lsw
        long2x16.value32 = msw
        LSet long32 = long2x16
        IntToLong = long32.value
    End Function
    Notez l'affectation via LSet entre deux variables de types déclarés par l'utilisateur alignés sur un Long.

    Dans la fenêtre d'Exécution immédiate (Ctrl+G) du VBE d'Excel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ? IntToLong(18861, -7842)
    1236132190

  10. #10
    Expert éminent
    Avatar de Qwazerty
    Homme Profil pro
    La très haute tension :D
    Inscrit en
    Avril 2002
    Messages
    3 906
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : La très haute tension :D
    Secteur : Service public

    Informations forums :
    Inscription : Avril 2002
    Messages : 3 906
    Points : 8 539
    Points
    8 539
    Par défaut
    Salut
    ...toutes versions
    Je ne suis pas chez moi mais

    Citation Envoyé par Msdn - LSet
    This page is specific to the Visual Basic for Applications (VBA) Language Reference for Office 2010.


    ++
    Qwaz

  11. #11
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2011
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2011
    Messages : 141
    Points : 414
    Points
    414
    Par défaut LSet
    LSet fonctionne également dans Excel97.

  12. #12
    Expert éminent
    Avatar de Qwazerty
    Homme Profil pro
    La très haute tension :D
    Inscrit en
    Avril 2002
    Messages
    3 906
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : La très haute tension :D
    Secteur : Service public

    Informations forums :
    Inscription : Avril 2002
    Messages : 3 906
    Points : 8 539
    Points
    8 539
    Par défaut
    Salut
    De retour chez moi, en effet, géniale cette instruction

    ++
    Qwaz

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment avoir l'octet de poids fort et faible d'un nombre
    Par azerty25 dans le forum Général Python
    Réponses: 7
    Dernier message: 02/12/2015, 16h41
  2. Réponses: 3
    Dernier message: 12/01/2011, 11h51
  3. Concaténer une chaine et un nombre pour former un nom de variable ?
    Par debie1108 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 30/04/2007, 07h38
  4. Récupérer poid fort / faible d'un float
    Par Nergaahl dans le forum C++
    Réponses: 5
    Dernier message: 13/04/2006, 17h15
  5. Concaténation de deux integer pour former une clé primaire
    Par stoukou dans le forum Décisions SGBD
    Réponses: 2
    Dernier message: 08/09/2005, 10h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo