Code Excel VBA pour transformer une ligne par un nombre variable de colonnes en lignes de longueur fixe

Je voudrais changer cela:

Filer ShareName IPAddress1 IPpaddres2 IPAddress2 ... ... .... ServerA /share1/tools 192.168.1.52 192.168.1.65 192.168.1.158 ServerA /share/library 192.168.1.65 192.168.1.61 192.168.1.155 ServerB /share/tools 192.168.1.158 192.168.1.159 ServerD /share/misc 192.168.1.7 .. ... ..... ....... 

Pour cela, dans une nouvelle feuille de calcul:

 Filer ShareName IPAddress ServerA /share1/tools 192.168.1.52 ServerA /share1/tools 192.168.1.65 ServerA /share1/tools 192.168.1.158 ServerA /share/library 192.168.1.65 ServerA /share/library 192.168.1.61 ServerA /share/library 192.168.1.155 ServerB ... ... ServerD ... ... .. ... ..... ....... 

  1. Accédez à l' onglet développeur et cliquez sur "enregistrer la macro"
  2. Ensuite, ajoutez une nouvelle feuille
  3. Arrêtez-le d'enregistrer
  4. Regardez le code généré en cliquant sur "Visual Basic", il est probablement dans "Module 1"

Répétez pour le prochain problème et utilisez ce code comme base pour ce que vous essayez de créer.

Lorsque vous êtes bloqué, Google est votre problème. Si cela ne le résout pas, publiez le code que vous avez ici et spécifiez exactement ce que vous voulez faire, ce que vous pensez faire et ce que vous pensez être faux ou ne comprend pas.

Code dans VBA / Macro:

 Public Sub distribute() Application.ScreenUpdating = False Dim wkb As Workbook Dim wks1 As Worksheet Dim wks2 As Worksheet Set wkb = ThisWorkbook Set wks1 = wkb.Worksheets("Sheet1") Set wks2 = wkb.Worksheets("Sheet2") wks2.Rows.Clear therow = 1 othersheetrow = 1 morerows = True While morerows morecolumns = True thecolumn = 3 abscolumn = thecolumn While morecolumns a = wks1.Cells(therow, thecolumn) If (a = "") And (thecolumn = abscolumn) Then morerows = False morecolumns = False ElseIf (a = "") And (thecolumn > abscolumn) Then morecolumns = False therow = therow + 1 Else If (therow = 1) Then For i = 1 To abscolumn wks2.Cells(therow, i) = wks1.Cells(therow, i) Next i therow = therow + 1 thecolumn = thecolumn - 1 Else othersheetrow = othersheetrow + 1 For i = 1 To abscolumn - 1 wks2.Cells(othersheetrow, i) = wks1.Cells(therow, i) Next i wks2.Cells(othersheetrow, i) = wks1.Cells(therow, thecolumn) End If End If thecolumn = thecolumn + 1 Wend Wend Application.ScreenUpdating = True themessage = MsgBox("Finished", vbInformation) End Sub 

Ouvrez VBA / Macro con ALT + F11 , puis insérez un nouveau module sous ThisWorkbook et collez le code sur le côté droit.

Il existe une variable appelée la thecolumn qui est initialisée sur 3, ce qui signifie que c'est la première colonne à considérer avant de créer une nouvelle entrée (ligne) sur Sheet2.