“Pivoting” data with VBA











up vote
1
down vote

favorite












I've attempted to erite some VBA for this this question. The output is OK



enter image description here



but the code is not very elegant. I'm happy-ish with creating unique list of names and certificates, but the rest seems rather ugly. I'd love to learn how to make it more elegant and programmer-like (and less amateur-like-crap).



Sub PivotData()

Dim rng As Range, cll As Range
Dim arr As New Collection, a
Dim var() As Variant
Dim l As Long
Dim lRow As Long, lCol As Long

l = 1

Set rng = Range("A2:C7")

' Create unique list of names
var = Range("A2:A7")
On Error Resume Next
For Each a In var
arr.Add a, a

Next
For l = 1 To arr.Count
Cells(l + 1, 5) = arr(l)
Next
Set arr = Nothing

' Create unique list of certificates
var = Range("B2:B7")
For Each a In var
arr.Add a, a
Next
For l = 1 To arr.Count
Cells(1, 5 + l) = arr(l)
Next
Set arr = Nothing
On Error GoTo 0

' Ugly code, how to make it more elegant?
Range("F2").FormulaArray = _
"=IFERROR(INDEX(R2C3:R7C3,MATCH(1,((R2C1:R7C1=RC5)*(R2C2:R7C2=R1C)),0)),"""")"

With Range("F2")
lRow = .CurrentRegion.Rows.Count
lCol = .CurrentRegion.Columns.Count + 4
End With

Range("F2:F" & lRow).FillDown
Range(Cells(2, 6), Cells(lRow, lCol)).FillRight

End Sub









share|improve this question







New contributor




Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    I've attempted to erite some VBA for this this question. The output is OK



    enter image description here



    but the code is not very elegant. I'm happy-ish with creating unique list of names and certificates, but the rest seems rather ugly. I'd love to learn how to make it more elegant and programmer-like (and less amateur-like-crap).



    Sub PivotData()

    Dim rng As Range, cll As Range
    Dim arr As New Collection, a
    Dim var() As Variant
    Dim l As Long
    Dim lRow As Long, lCol As Long

    l = 1

    Set rng = Range("A2:C7")

    ' Create unique list of names
    var = Range("A2:A7")
    On Error Resume Next
    For Each a In var
    arr.Add a, a

    Next
    For l = 1 To arr.Count
    Cells(l + 1, 5) = arr(l)
    Next
    Set arr = Nothing

    ' Create unique list of certificates
    var = Range("B2:B7")
    For Each a In var
    arr.Add a, a
    Next
    For l = 1 To arr.Count
    Cells(1, 5 + l) = arr(l)
    Next
    Set arr = Nothing
    On Error GoTo 0

    ' Ugly code, how to make it more elegant?
    Range("F2").FormulaArray = _
    "=IFERROR(INDEX(R2C3:R7C3,MATCH(1,((R2C1:R7C1=RC5)*(R2C2:R7C2=R1C)),0)),"""")"

    With Range("F2")
    lRow = .CurrentRegion.Rows.Count
    lCol = .CurrentRegion.Columns.Count + 4
    End With

    Range("F2:F" & lRow).FillDown
    Range(Cells(2, 6), Cells(lRow, lCol)).FillRight

    End Sub









    share|improve this question







    New contributor




    Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I've attempted to erite some VBA for this this question. The output is OK



      enter image description here



      but the code is not very elegant. I'm happy-ish with creating unique list of names and certificates, but the rest seems rather ugly. I'd love to learn how to make it more elegant and programmer-like (and less amateur-like-crap).



      Sub PivotData()

      Dim rng As Range, cll As Range
      Dim arr As New Collection, a
      Dim var() As Variant
      Dim l As Long
      Dim lRow As Long, lCol As Long

      l = 1

      Set rng = Range("A2:C7")

      ' Create unique list of names
      var = Range("A2:A7")
      On Error Resume Next
      For Each a In var
      arr.Add a, a

      Next
      For l = 1 To arr.Count
      Cells(l + 1, 5) = arr(l)
      Next
      Set arr = Nothing

      ' Create unique list of certificates
      var = Range("B2:B7")
      For Each a In var
      arr.Add a, a
      Next
      For l = 1 To arr.Count
      Cells(1, 5 + l) = arr(l)
      Next
      Set arr = Nothing
      On Error GoTo 0

      ' Ugly code, how to make it more elegant?
      Range("F2").FormulaArray = _
      "=IFERROR(INDEX(R2C3:R7C3,MATCH(1,((R2C1:R7C1=RC5)*(R2C2:R7C2=R1C)),0)),"""")"

      With Range("F2")
      lRow = .CurrentRegion.Rows.Count
      lCol = .CurrentRegion.Columns.Count + 4
      End With

      Range("F2:F" & lRow).FillDown
      Range(Cells(2, 6), Cells(lRow, lCol)).FillRight

      End Sub









      share|improve this question







      New contributor




      Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I've attempted to erite some VBA for this this question. The output is OK



      enter image description here



      but the code is not very elegant. I'm happy-ish with creating unique list of names and certificates, but the rest seems rather ugly. I'd love to learn how to make it more elegant and programmer-like (and less amateur-like-crap).



      Sub PivotData()

      Dim rng As Range, cll As Range
      Dim arr As New Collection, a
      Dim var() As Variant
      Dim l As Long
      Dim lRow As Long, lCol As Long

      l = 1

      Set rng = Range("A2:C7")

      ' Create unique list of names
      var = Range("A2:A7")
      On Error Resume Next
      For Each a In var
      arr.Add a, a

      Next
      For l = 1 To arr.Count
      Cells(l + 1, 5) = arr(l)
      Next
      Set arr = Nothing

      ' Create unique list of certificates
      var = Range("B2:B7")
      For Each a In var
      arr.Add a, a
      Next
      For l = 1 To arr.Count
      Cells(1, 5 + l) = arr(l)
      Next
      Set arr = Nothing
      On Error GoTo 0

      ' Ugly code, how to make it more elegant?
      Range("F2").FormulaArray = _
      "=IFERROR(INDEX(R2C3:R7C3,MATCH(1,((R2C1:R7C1=RC5)*(R2C2:R7C2=R1C)),0)),"""")"

      With Range("F2")
      lRow = .CurrentRegion.Rows.Count
      lCol = .CurrentRegion.Columns.Count + 4
      End With

      Range("F2:F" & lRow).FillDown
      Range(Cells(2, 6), Cells(lRow, lCol)).FillRight

      End Sub






      algorithm vba excel






      share|improve this question







      New contributor




      Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 9 hours ago









      Michal Rosa

      1062




      1062




      New contributor




      Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Michal Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Michal Rosa is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207946%2fpivoting-data-with-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Michal Rosa is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Michal Rosa is a new contributor. Be nice, and check out our Code of Conduct.













          Michal Rosa is a new contributor. Be nice, and check out our Code of Conduct.












          Michal Rosa is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207946%2fpivoting-data-with-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python