Clever-Excel-Forum

Normale Version: Variablen füllen mit Schleife
Du siehst gerade eine vereinfachte Darstellung unserer Inhalte. Normale Ansicht mit richtiger Formatierung.
Seiten: 1 2 3 4 5
Code:
Sub M_snb()
  sp = Split("separatorID ButtonId Caption MacroName  imageMSO isSeparator screentip supertip key tag")
  sn = Split("<menuseparator id_<button id_label_onAction_imageMSO_isSeparator_screentip_supertip_key_tag", "_")

   For j = 1 To 2
     sq = sn
     For jj = 0 To UBound(sq)
          c00 = Chr(65 + jj) & Chr(65 + jj) & Chr(65 + jj)
          If j = 2 And jj = 0 Then c00 = 1
          If jj <> 0 And jj <> 5 And jj <> 7 Or jj & "_" & c00 = "0_1" Then sq(jj) = sq(jj) & IIf(jj < 2, j, "") & "=""" & c00 & IIf(jj < 2, j, "") & IIf(jj < 1, """/>", """")
        Next
        c01 = c01 & vbLf & Join(Filter(sq, "=")) & "/>"
   Next

   MsgBox c01
End Sub
Da steht aber auch eine 1 hinter "id" ("button_id1"), die gehört da nicht in.
Den Code von 21:55 kann ich nicht umsetzen (weiss nicht wie das einzubauen ist).

Kurz zur Erklärung:
Public Function ErstelleMenueInhalt2(strConfigPath As String, strMenuPoint As String, lngFrom As Long, lngTo As Long) As String

  Dim varConfKeyCount As Long
  Dim lngMenuPos      As Long
  Dim varXMLParams    As Variant
  Dim varConfKeyName  As Variant
  Dim varXMLParamsPos As Variant
  Dim varConfKeys     As Variant
  
  varConfKeyName = Split("isSeparator separatorID ButtonId Caption MacroName imageMSO screentip supertip keytip tag")
  varXMLParams = Split("<menuSeparator id_isSeparator_<button id_label_onAction_imageMSO_screentip_supertip_keytip_tag", "_")
  
  For lngMenuPos = lngFrom To lngTo
      varXMLParamsPos = varXMLParams
         For varConfKeyCount = 0 To Ubound(varXMLParamsPos)
             varConfKeys = MenueinhaltEinlesen(strMenuPoint & lngMenuPos, varConfKeyName(varConfKeyCount), strConfigPath)
                If varConfKeys <> "" Then varXMLParamsPos(varConfKeyCount) = varXMLParamsPos(varConfKeyCount) & _
                                  IIf(varConfKeyCount < 2, lngMenuPos, "") & "=""" & varConfKeys & IIf(varConfKeyCount < 1, """/>", "")
         Next varConfKeyCount


         ErstelleMenueInhalt2 = ErstelleMenueInhalt2 & Join(Filter(varXMLParamsPos, "="), """ ") & """/>"

  Next lngMenuPos
  
End Function


VBA/HTML - CodeConverter für Office-Foren, AddIn für Excel/Word 2002-2013 - komplett in VBA geschrieben von Lukas Mosimann. Projektbetreuung durch mumpel

Code erstellt und getestet in Office 16 - mit VBAHTML 12.6.0


Das ist der Code mit Anpassung. Nur erzeugt er eine falsche Ausgabe. Das Anführungszeichen hinter der schließenden Klammer bei menuSeparator ist auch falsch.
<menuSeparator id1="0"/>" isSeparator1="mnu1Separator" <button id="mnu1Button" label="Eintrag 1" on‌Action="onAction_Button" imageMSO="FilesToolAddFiles" tag="Tag1"/>

Richtig wäre:

(das X steht jeweils für den Zähler lngMenuPos)

Wenn "isSeparator=1": <menuSeparator id="mnu1SeparatorX"/> <button id="mnu1ButtonX" label="Eintrag 1" on‌Action="onAction_Button" imageMSO="FilesToolAddFiles" tag="Tag1"/>

Wenn "isSeparator=0": <button id="mnu1ButtonX" label="Eintrag 1" on‌Action="onAction_Button" imageMSO="FilesToolAddFiles" tag="Tag1"/> (das X steht jeweils für den Zähler lngMenuPos)
Nicht ganz so gut, aber funktioniert (und gefällt mir besser als mein bisheriger Code). Vielleicht gibt es daran ja noch etwas zu optimieren.

 Public Function ErstelleMenueInhalt(strConfigPath As String, strMenuPoint As String, lngFrom As Long, lngTo As Long) As String
 
  Dim varConfKeyCount As Long
  Dim lngMenuPos      As Long
  Dim varXMLParams    As Variant
  Dim varConfKeyName  As Variant
  Dim varXMLParamsPos As Variant
  Dim varConfKeys     As Variant
  Dim varSepCount     As Variant
  Dim varSeparator    As Variant
  Dim strSepKeys      As String
  Dim strSeparator    As String
  
  varConfKeyName = Split("isSeparator separatorID ButtonId Caption MacroName imageMSO screentip supertip keytip tag")
  varXMLParams = Split("<menuSeparator id_isSeparator_<button id_label_onAction_imageMso_screentip_supertip_keytip_tag", "_")
  

  For lngMenuPos = lngFrom To lngTo
      strSeparator = ""
      strSepKeys = ""
      
      varXMLParamsPos = varXMLParams
         For varConfKeyCount = 0 To 1
             varConfKeys = MenueinhaltEinlesen(strMenuPoint & lngMenuPos, varConfKeyName(varConfKeyCount), strConfigPath)
             strSepKeys = strSepKeys & varConfKeys & ","
         Next varConfKeyCount
         varSeparator = Split(strSepKeys, ",")
         If varSeparator(0) = "1" Then strSeparator = varXMLParams(0) & "=""" & varSeparator(1) & lngMenuPos & """ />"
                
         For varConfKeyCount = 2 To Ubound(varConfKeyName)
             varConfKeys = MenueinhaltEinlesen(strMenuPoint & lngMenuPos, varConfKeyName(varConfKeyCount), strConfigPath)
                If varConfKeys <> "" Then varXMLParamsPos(varConfKeyCount) = varXMLParamsPos(varConfKeyCount) & _
                                          "=""" & varConfKeys & IIf(varConfKeyCount < 1, """/>", IIf((varConfKeyCount = 2), lngMenuPos & "", ""))
         Next varConfKeyCount

       
         ErstelleMenueInhalt = ErstelleMenueInhalt & strSeparator & Join(Filter(varXMLParamsPos, "="), """ ") & """ />"

  Next lngMenuPos
         
End Function


VBA/HTML-CodeConverter, AddIn für Office 2002-2016 - in VBA geschrieben von Lukas Mosimann. Projektbetreuung:RMH Software & Media

Code erstellt und getestet in Office 16 - mit VBAHTML 12.6.0

Kann 'ziemlich' optimiert werden:


Code:
Public Function ErstelleMenueInhalt2(strConfigPath As String, strMenuPoint As String, lngFrom As Long, lngTo As Long) As String
  sp = Split("separatorID isSeparator ButtonId Caption MacroName  imageMSO  screentip supertip keytip tag")
  sn = Split("<menuseparator id_isSeparator_<button id_label_onAction_imageMSO_screentip_supertip_keytip_tag", "_")
 
  For j = lngFrom To lngTo
      sq = sn
      For jj = 0 To UBound(sq)
         c00 = MenueinhaltEinlesen(strMenuPoint & j, sp(jj), strConfigPath)
         If jj = 1 Then
              If c00 <> "1" Then sq(0) = ""
              c00 = ""
         End If
         If c00 <> "" Then sq(jj) = sq(jj) & "=""" & c00 & IIf(jj = 0 Or jj = 2, j, "") & IIf(jj = 0, """/>", """")
      Next
      ErstelleMenueInhalt2 = ErstelleMenueInhalt2 & Join(Filter(sq, "=")) & "/>"
  Next
End Function
So gefällt mir das. Nurnoch 23 Zeilen anstatt 64. :28: Musste zwar ein paar Leerzeichen in "sp" entfernen, sonst macht der Code Fehler, aber es funktioniert. Und man lernt etwas über Schleifen. Danke!
Code:
If c00 <> "" Then sq(jj) = sq(jj) & "=""" & c00 & IIf(jj = 0 Or jj = 2, j, "") & IIf(jj = 0, """/>", """")

kann verkürzt werden bis:

Code:
If c00 <> "" Then sq(jj) = sq(jj) & "=""" & c00 & IIf(jj <3, j, "") & IIf(jj = 0, """/>", """")
Das Wichtigste ist erledigt.


Ein kleines "Gimmick" wäre vielleicht noch gut. Später kommen mal weitere Attribute hinzu. Dies sind Attribute die sich gegenseitig ausschließen (z.B.: label und getLabel, screentip und getScreentip, supertip und getSupertip). Wenn jetzt z.B. jemand label und getLabel füllt soll nur getLabel aufgenommen werden, label soll dann ignoriert werden.
Nun kannst du sehen wie wichtig sytematische Programmierung ist:
Ich habe die Reihenfolge der Attributen etwas geändert.

Code:
Public Function ErstelleMenueInhalt2(strConfigPath As String, strMenuPoint As String, lngFrom As Long, lngTo As Long) As String
  sp = Split("separatorID isSeparator ButtonId MacroName imageMSO tag Caption screentip supertip keytip getLabel getScreentip getSupertip")
  sn = Split("<menuseparator id_isSeparator_<button id_onAction_imageMSO_tag_label_screentip_supertip_keytip_getLabel_getScreentip_getSupertip", "_")
 
  For j = lngFrom To lngTo
      sq = sn
      For jj = 0 To UBound(sq)
         c00 = MenueinhaltEinlesen(strMenuPoint & j, sp(jj), strConfigPath)
         If jj = 1 Then
              If c00 <> "1" Then sq(0) = ""
              c00 = ""
         End If
         If c00 <> "" Then sq(jj) = sq(jj) & "=""" & c00 & IIf(jj = 0 Or jj = 2, j, "") & IIf(jj = 0, """/>", """")
      Next
      for jj =ubound(sq)-2 to ubound(sq)
        if instr(sq(jj),"=") then sq(jj-4)=""
      next
      ErstelleMenueInhalt2 = ErstelleMenueInhalt2 & Join(Filter(sq, "=")) & "/>"
  Next
End Function
Jetzt nach dem ich die letzte Schleife verstanden habe konnte ich ein weiteres Ausschlußkriterium hinzufügen (imageMso und getImage schließen sich ebenfalls gegenseitig aus). Danke nochmal!
Seiten: 1 2 3 4 5