Faire une Subroutine avec VB!

lele79

XLDnaute Nouveau
Bonjour a tous,

Je voudrai simplifier mon code en creant une subroutine afin de l'appeller lorsque j'en ai ai besoin.

voici mon code que je voudrai modifier:
Code:
Sub Transfer_AIS_AIC_BDM(wkBkName As String)   ' Copy all data from AIS_AIC_BDM Sheet to AIS_AIC_SMT Sheet

    Dim w1 As Worksheet, w2 As Worksheet
    Dim i As Integer, j As Integer
    Dim Nam As String, Des As String
    Dim Maxi As Long, Mini As Long, lastline As Long, una As String
    Dim Uni As String, pro As String, Typ As String, namlg As String
    Dim Funct As String
    Dim Current_name As String, PITagAttrib As String
    
    
    If ActiveSheet.Name = "AIS_AIC_BDM" Then     ' AIS_AIC_BDM  activesheet
        
	Set w1 = ActiveSheet
        lastline = w1.Cells(65532, 16).End(xlUp).Row
             
        Workbooks(wkBkName).Activate
        Set w2 = Workbooks(wkBkName).Worksheets("AIS_AIC_SMT") 'Filling SMT Workbook Headline 
        Else
           MsgBox "You should activate the right workbook and worksheet", vbInformation
    End If

            i = 4 'BDM template
            j = 1 'SMT template
   
    Do While i <= lastline
             
                    i = i + 1

                'NAME & DESCRIPTION
                    If w1.Cells(i, 6) <> "" Then
                        Nam = w1.Cells(i, 6) 'Name
		    End if
		    If w1.Cells(i, 7) <> "" Then
                        Des = w1.Cells(i, 7) 'Description
		    End if
                    
                'Mini & Maxi
                    if w1.Cells(i, 8) <> "" Then
                        Maxi = w1.Cells(i, 8)   'Max
		    End if
                    
                    if w1.Cells(i, 9) <> "" Then
                        Mini = w1.Cells(i, 9)    'Min
                    end if
                    if w1.Cells(i, 10) <> "" Then
                        Uni = w1.Cells(i, 10)   'Unit
                    End If
                'LOG CONFIGURATION
                    If w1.Cells(i, 14) <> "" Then
                        una = w1.Cells(i, 14)   'Enable
                    End If
                    
                    If w1.Cells(i, 15) = "IO.FilteredSignal.Value" Then
                        pro = w1.Cells(i, 15)   'Property
                        Typ = w1.Cells(i, 16)   'DataType
                        namlg = w1.Cells(i, 17) 'Log Nam
                    End If
                                        
    'WRITE in SMT Sheet
    If Nam <> "" And pro <> "" Then
            j = j + 1
            Current_name = Nam
            PITagAttrib = "_Value"
            Call UpdateDataInBDMSheet(w1.Cells(i, 2), Application.ActiveWorkbook.Name + ":" + Application.ActiveSheet.Name, "PIAttr01", "_Value")
            
           [COLOR="red"] w2.Cells(j, 2) = Current_name & PITagAttrib                 'tag
            w2.Cells(j, 3) = Des                                        'descriptor
            w2.Cells(j, 4) = -5                                         'Display Digit
            w2.Cells(j, 5) = Uni                                        'Engunits
            w2.Cells(j, 6) = Current_name & ":" & pro & "," & namlg     'Instrumenttag
            w2.Cells(j, 7) = 1                                          'Location 1
            w2.Cells(j, 8) = 0                                          'Location 2
            w2.Cells(j, 9) = 0                                          'Location 3
            w2.Cells(j, 10) = 1                                         'Location 4
            w2.Cells(j, 11) = 0                                         'Location 5
            w2.Cells(j, 12) = "OPCHDA"                                  'PointSource
            w2.Cells(j, 13) = "float64"                                 'Pointtype
            w2.Cells(j, 14) = 1                                         'Scan
            w2.Cells(j, 15) = Maxi - Mini                               'Span
            w2.Cells(j, 16) = ((Maxi - Mini) / 2) + Mini                'Typicalvalue
            w2.Cells(j, 17) = Mini                                      'Zero
            pro = ""[/COLOR]     End If
                
        Loop
      '  UserForm1.Show
        Set w1 = Nothing
        Set w2 = Nothing
End Sub

je voudrai creer une subroutine a la place de ce que j'ai surligne en rouge dans le code. C'est a dire qu'a la place de ce que j'ai surligne, j'aurai un code genre:
Code:
Call subroutine
. car j'appelle ce subroutine dans differentes feuilles. MOn souci c'est qu'a chaque fois le W2 n'est pas la meme pour chaques feuille.

voila, en esperant que ce que j'ai ecris est claire.

En vous remerciant par avance,

Cordialement,

lele79
 

lele79

XLDnaute Nouveau
Re : Faire une Subroutine avec VB!

bonsoir Herve, le forum,

effectivement on ne voit pas le surlignage en rouge! Mais je pense qu'elle ne marche pas.
Voici en fait ce que je voudrai mettre dans la Subroutine.

w2.Cells(j, 2) = Current_name & PITagAttrib 'tag
w2.Cells(j, 3) = Des 'descriptor
w2.Cells(j, 4) = -5 'Display Digit
w2.Cells(j, 5) = Uni 'Engunits
w2.Cells(j, 6) = Current_name & ":" & pro & "," & namlg 'Instrumenttag
w2.Cells(j, 7) = 1 'Location 1
w2.Cells(j, 8) = 0 'Location 2
w2.Cells(j, 9) = 0 'Location 3
w2.Cells(j, 10) = 1 'Location 4
w2.Cells(j, 11) = 0 'Location 5
w2.Cells(j, 12) = "OPCHDA" 'PointSource
w2.Cells(j, 13) = "float64" 'Pointtype
w2.Cells(j, 14) = 1 'Scan
w2.Cells(j, 15) = Maxi - Mini 'Span
w2.Cells(j, 16) = ((Maxi - Mini) / 2) + Mini 'Typicalvalue
w2.Cells(j, 17) = Mini 'Zero
pro = ""


cordialement,

Lele79
 

ROGER2327

XLDnaute Barbatruc
Re : Faire une Subroutine avec VB!

Bonsoir à tous
Une proposition, non testée faute de support :
Code:
Sub Transfer_AIS_AIC_BDM(wkBkName As String)   ' Copy all data from AIS_AIC_BDM Sheet to AIS_AIC_SMT Sheet

    Dim w1 As Worksheet, w2 As Worksheet
    Dim i As Integer, j As Integer
    Dim Nam As String, Des As String
    Dim Maxi As Long, Mini As Long, lastline As Long, una As String
    Dim Uni As String, pro As String, Typ As String, namlg As String
    Dim Funct As String
    Dim Current_name As String, PITagAttrib As String
   
   
    If ActiveSheet.Name = "AIS_AIC_BDM" Then     ' AIS_AIC_BDM  activesheet
       
    Set w1 = ActiveSheet
        lastline = w1.Cells(65532, 16).End(xlUp).Row
             
        Workbooks(wkBkName).Activate
        Set w2 = Workbooks(wkBkName).Worksheets("AIS_AIC_SMT") 'Filling SMT Workbook Headline
        Else
           MsgBox "You should activate the right workbook and worksheet", vbInformation
    End If

            i = 4 'BDM template
            j = 1 'SMT template
   
    Do While i <= lastline
             
                    i = i + 1

                'NAME & DESCRIPTION
                    If w1.Cells(i, 6) <> "" Then
                        Nam = w1.Cells(i, 6) 'Name
            End If
            If w1.Cells(i, 7) <> "" Then
                        Des = w1.Cells(i, 7) 'Description
            End If
                   
                'Mini & Maxi
                    If w1.Cells(i, 8) <> "" Then
                        Maxi = w1.Cells(i, 8)   'Max
            End If
                   
                    If w1.Cells(i, 9) <> "" Then
                        Mini = w1.Cells(i, 9)    'Min
                    End If
                    If w1.Cells(i, 10) <> "" Then
                        Uni = w1.Cells(i, 10)   'Unit
                    End If
                'LOG CONFIGURATION
                    If w1.Cells(i, 14) <> "" Then
                        una = w1.Cells(i, 14)   'Enable
                    End If
                   
                    If w1.Cells(i, 15) = "IO.FilteredSignal.Value" Then
                        pro = w1.Cells(i, 15)   'Property
                        Typ = w1.Cells(i, 16)   'DataType
                        namlg = w1.Cells(i, 17) 'Log Nam
                    End If
                                       
    'WRITE in SMT Sheet
    If Nam <> "" And pro <> "" Then
            j = j + 1
            Current_name = Nam
            PITagAttrib = "_Value"
            Call UpdateDataInBDMSheet(w1.Cells(i, 2), Application.ActiveWorkbook.Name + ":" + Application.ActiveSheet.Name, "PIAttr01", "_Value")
            
            Call SubRoutine(w2, Current_name, PITagAttrib, Des, Uni, pro, namlg, Maxi, Mini, j)
            pro = ""
            
    End If
               
        Loop
      '  UserForm1.Show
        Set w1 = Nothing
        Set w2 = Nothing
End Sub

Sub SubRoutine(sh As Worksheet, CurName$, Attrib$, Ds$, Un$, Pr$, Nlg$, Mxi&, Mni&, ind%)
    With sh
      .Cells(ind, 2) = CurName & Attrib                          'tag
      .Cells(ind, 3) = Ds                                        'descriptor
      .Cells(ind, 4) = -5                                        'Display Digit
      .Cells(ind, 5) = Un                                        'Engunits
      .Cells(ind, 6) = CurName & ":" & Pr & "," & Nlg            'Instrumenttag
      .Cells(ind, 7) = 1                                         'Location 1
      .Cells(ind, 8) = 0                                         'Location 2
      .Cells(ind, 9) = 0                                         'Location 3
      .Cells(ind, 10) = 1                                        'Location 4
      .Cells(ind, 11) = 0                                        'Location 5
      .Cells(ind, 12) = "OPCHDA"                                 'PointSource
      .Cells(ind, 13) = "float64"                                'Pointtype
      .Cells(ind, 14) = 1                                        'Scan
      .Cells(ind, 15) = Mxi - Mni                                'Span
      .Cells(ind, 16) = ((Mxi - Mni) / 2) + Mni                  'Typicalvalue
      .Cells(ind, 17) = Mni                                      'Zero
    End With
End Sub
ROGER2327
#4746


Samedi 14 Sable 138 (, )
24 Frimaire An CCXIX
2010-W50-2T22:30:10Z
 

Staple1600

XLDnaute Barbatruc
Re : Faire une Subroutine avec VB!

Bonsoir à tous

lele79
Tu nous disais (en octobre dernier)
Je ne suis pas famillier avec les langages de programmation, encore moins avec VB mais la je suis dans l'obligation de comprendre VB et de l'utiliser dans le cadre de mon travail.
Tous ce que je sais faire de VB c'est de faire des macros avec l'enregistreur macros.

Ce code semble visiblement n'être pas le tien ;)
(Ou alors tu progresses de manière fulgurante)
Contactes l'auteur du code, il saura t'aider plus aisément non ?
 
Dernière édition:

lele79

XLDnaute Nouveau
Re : Faire une Subroutine avec VB!

Bonjour Roger2327, Staple1600, le forum,

Staple1600, l'auteur de ce code n'est autre que moi avec l'aide bien sur du Forum et j'en remercie au passage les personnes qui m'ont aide.

Roger2327, je teste et je reviens .

Merci,

Lele79
 

Staple1600

XLDnaute Barbatruc
Re : Faire une Subroutine avec VB!

Re


Il me semblait que sur XLD nous étions francophones.
Donc comme je le disais tu fulgures à vitesse stratosphérique ;)

En octobre, tu débutes VBA
En novembre , tu as déjà fais de très grands pas

Donc bravo si c'est véridique
Sinon bravo l'artiste

PS: tu es anglophone ?

PS2: je préfère prévenir: il faut voir ici un mix d'humour et d'ironie, le tout restant bon enfant et amical.
Donc inutile de perdre sa zénitude :)
 
Dernière édition:

lele79

XLDnaute Nouveau
Re : Faire une Subroutine avec VB!

Bonsoir le Forum,

Kool Roger 2327, Ca marche nickel! merci bcp pour l'aide. Sinon j'ai juste une question de curiosite, pourquoi mettre , $,&, % a la fin des differents arguments de la Subroutine?
Sub SubRoutine(sh As Worksheet, CurName$, Attrib$, Ds$, Un$, Pr$, Nlg$, Mxi&, Mni&, ind%)

Staple600: non je suis Francais mais je travail en Australie, "That's Why".
J'ai encore un peu de mal avec la langue de sheakspear. C'est pour ca que je me suis dirige vers un forum francophone. D'ailleurs c'est mieux pour moi afin de mieux comprendre le VB. Et comme je l'ai ecris lors de ma premiere discussion, j'etais oblige de comprendre le VB. Donc je me suis plonge dedans durant tout le mois d'Octobre et debut novembre. J'ai assimile des notions mais je suis encore loin de tout assimiler! la preuve je demande de l'aide ! et les subroutines....... j'en suis loin du compte. J'avais fais des tests mais la je vois ce que Roger a fais!....je suis admiratif! ca prouve qu'il faut encore apprendre......!

PS2: ne vous inquietez pas je suis un fervant pratiquant de la Zenattitude!

Encore merci,

Lele79
 

ROGER2327

XLDnaute Barbatruc
Re : Faire une Subroutine avec VB!

Re…
Bonsoir Staple1600, le forum,

Je crois que j'ai tout ce qui me faut!....encore merci le coup de main!

Lele79
Parfait !
(J'avais quand même un petit doute : écrire à la volée du code sans savoir ce qu'il est censé faire et sans support pour le tester est toujours un peu Rock'n Roll !)​
ROGER2327
#4748


Dimanche 15 Sable 138 (Khurmookum du Dr Faustroll, SS)
25 Frimaire An CCXIX
2010-W50-3T00:30:01Z
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 440
Messages
2 088 450
Membres
103 853
dernier inscrit
roukhou