How can I concatenate column B contents if the cell under autofilter identifier is blank?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am using autofilter to look up a criteria in column A in Sheet1 and return the corresponding value from column B in a table, however I want to be able to make it concatenate if column B contents are in two cells. Column A is blank under the identifier in cases like this.



Sub ReturnTIResults()

Dim r As Range

Application.ScreenUpdating = True

With Worksheets("Sheet1") ' reference results sheet
If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
' if A1 is empty, put a "dummy" header to make AutoFilter work properly

.AutoFilterMode = False
With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
.SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
.AutoFilter Field:=1, Criteria1:="=TI"
On Error Resume Next
Set r = .Resize(.Rows.Count - 1, 1).Offset(1,1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not r Is Nothing Then r.Copy Worksheets("Search Results").Range("B7")
.Parent.AutoFilterMode = False

.SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
If .Range("A1").Value = "dummy header" Then
.Range("A1").ClearContents ' remove any "dummy" header
End With
End With

Application.ScreenUpdating = True

End Sub


Image










share|improve this question































    0















    I am using autofilter to look up a criteria in column A in Sheet1 and return the corresponding value from column B in a table, however I want to be able to make it concatenate if column B contents are in two cells. Column A is blank under the identifier in cases like this.



    Sub ReturnTIResults()

    Dim r As Range

    Application.ScreenUpdating = True

    With Worksheets("Sheet1") ' reference results sheet
    If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
    ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

    .AutoFilterMode = False
    With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
    .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
    .AutoFilter Field:=1, Criteria1:="=TI"
    On Error Resume Next
    Set r = .Resize(.Rows.Count - 1, 1).Offset(1,1).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not r Is Nothing Then r.Copy Worksheets("Search Results").Range("B7")
    .Parent.AutoFilterMode = False

    .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
    If .Range("A1").Value = "dummy header" Then
    .Range("A1").ClearContents ' remove any "dummy" header
    End With
    End With

    Application.ScreenUpdating = True

    End Sub


    Image










    share|improve this question



























      0












      0








      0








      I am using autofilter to look up a criteria in column A in Sheet1 and return the corresponding value from column B in a table, however I want to be able to make it concatenate if column B contents are in two cells. Column A is blank under the identifier in cases like this.



      Sub ReturnTIResults()

      Dim r As Range

      Application.ScreenUpdating = True

      With Worksheets("Sheet1") ' reference results sheet
      If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
      ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

      .AutoFilterMode = False
      With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
      .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
      .AutoFilter Field:=1, Criteria1:="=TI"
      On Error Resume Next
      Set r = .Resize(.Rows.Count - 1, 1).Offset(1,1).SpecialCells(xlCellTypeVisible)
      On Error GoTo 0
      If Not r Is Nothing Then r.Copy Worksheets("Search Results").Range("B7")
      .Parent.AutoFilterMode = False

      .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
      If .Range("A1").Value = "dummy header" Then
      .Range("A1").ClearContents ' remove any "dummy" header
      End With
      End With

      Application.ScreenUpdating = True

      End Sub


      Image










      share|improve this question
















      I am using autofilter to look up a criteria in column A in Sheet1 and return the corresponding value from column B in a table, however I want to be able to make it concatenate if column B contents are in two cells. Column A is blank under the identifier in cases like this.



      Sub ReturnTIResults()

      Dim r As Range

      Application.ScreenUpdating = True

      With Worksheets("Sheet1") ' reference results sheet
      If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
      ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

      .AutoFilterMode = False
      With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
      .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
      .AutoFilter Field:=1, Criteria1:="=TI"
      On Error Resume Next
      Set r = .Resize(.Rows.Count - 1, 1).Offset(1,1).SpecialCells(xlCellTypeVisible)
      On Error GoTo 0
      If Not r Is Nothing Then r.Copy Worksheets("Search Results").Range("B7")
      .Parent.AutoFilterMode = False

      .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
      If .Range("A1").Value = "dummy header" Then
      .Range("A1").ClearContents ' remove any "dummy" header
      End With
      End With

      Application.ScreenUpdating = True

      End Sub


      Image







      excel vba excel-vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 16:24









      Emily Alden

      436214




      436214










      asked Jan 4 at 16:08









      NoobNoob

      34




      34
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Untested, I basically just inserted some code within your If branch.



          Few things to note:





          • Application.Transpose (used below to convert 2-d array to 1-d array) can only handle arrays of length ~65.5k. So if you have more filtered items than that, not all of them may get concatenated.

          • Cells have a character limit of ~32.8k I think. If the result of your concatenation violates this limit, you may get an error when trying to assign the result.


          But other than that, should work fine. Also, both of your Application.ScreenUpdating assignments seem to be True. You might want to look into that.



          Option Explicit

          Sub ReturnTIResults()

          Dim r As Range

          Application.ScreenUpdating = True

          With Worksheets("Sheet1") ' reference results sheet
          If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
          ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

          .AutoFilterMode = False
          With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
          .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
          .AutoFilter Field:=1, Criteria1:="=TI"
          On Error Resume Next
          Set r = .Resize(.Rows.Count - 1, 1).Offset(1, 1).SpecialCells(xlCellTypeVisible)
          On Error GoTo 0
          If Not r Is Nothing Then
          If r.Rows.Count > 1 Then
          Dim toConcatenate As Variant
          toConcatenate = Application.Transpose(r.Value2)
          toConcatenate = VBA.Strings.Join(toConcatenate, ", ") ' <-- Change to whatever delimiter you want
          Worksheets("Search Results").Range("B7").Value2 = toConcatenate
          Else
          Worksheets("Search Results").Range("B7").Value2 = r.Value2
          End If
          End If
          .Parent.AutoFilterMode = False

          .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
          If .Range("A1").Value = "dummy header" Then .Range("A1").ClearContents ' remove any "dummy" header
          End With
          End With

          Application.ScreenUpdating = True

          End Sub





          share|improve this answer
























          • @Noob I don't know what you mean by result, I can't see your sheet. Try stepping through the code with F8

            – chillin
            Jan 8 at 18:23












          Your Answer






          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: "1"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042466%2fhow-can-i-concatenate-column-b-contents-if-the-cell-under-autofilter-identifier%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Untested, I basically just inserted some code within your If branch.



          Few things to note:





          • Application.Transpose (used below to convert 2-d array to 1-d array) can only handle arrays of length ~65.5k. So if you have more filtered items than that, not all of them may get concatenated.

          • Cells have a character limit of ~32.8k I think. If the result of your concatenation violates this limit, you may get an error when trying to assign the result.


          But other than that, should work fine. Also, both of your Application.ScreenUpdating assignments seem to be True. You might want to look into that.



          Option Explicit

          Sub ReturnTIResults()

          Dim r As Range

          Application.ScreenUpdating = True

          With Worksheets("Sheet1") ' reference results sheet
          If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
          ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

          .AutoFilterMode = False
          With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
          .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
          .AutoFilter Field:=1, Criteria1:="=TI"
          On Error Resume Next
          Set r = .Resize(.Rows.Count - 1, 1).Offset(1, 1).SpecialCells(xlCellTypeVisible)
          On Error GoTo 0
          If Not r Is Nothing Then
          If r.Rows.Count > 1 Then
          Dim toConcatenate As Variant
          toConcatenate = Application.Transpose(r.Value2)
          toConcatenate = VBA.Strings.Join(toConcatenate, ", ") ' <-- Change to whatever delimiter you want
          Worksheets("Search Results").Range("B7").Value2 = toConcatenate
          Else
          Worksheets("Search Results").Range("B7").Value2 = r.Value2
          End If
          End If
          .Parent.AutoFilterMode = False

          .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
          If .Range("A1").Value = "dummy header" Then .Range("A1").ClearContents ' remove any "dummy" header
          End With
          End With

          Application.ScreenUpdating = True

          End Sub





          share|improve this answer
























          • @Noob I don't know what you mean by result, I can't see your sheet. Try stepping through the code with F8

            – chillin
            Jan 8 at 18:23
















          0














          Untested, I basically just inserted some code within your If branch.



          Few things to note:





          • Application.Transpose (used below to convert 2-d array to 1-d array) can only handle arrays of length ~65.5k. So if you have more filtered items than that, not all of them may get concatenated.

          • Cells have a character limit of ~32.8k I think. If the result of your concatenation violates this limit, you may get an error when trying to assign the result.


          But other than that, should work fine. Also, both of your Application.ScreenUpdating assignments seem to be True. You might want to look into that.



          Option Explicit

          Sub ReturnTIResults()

          Dim r As Range

          Application.ScreenUpdating = True

          With Worksheets("Sheet1") ' reference results sheet
          If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
          ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

          .AutoFilterMode = False
          With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
          .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
          .AutoFilter Field:=1, Criteria1:="=TI"
          On Error Resume Next
          Set r = .Resize(.Rows.Count - 1, 1).Offset(1, 1).SpecialCells(xlCellTypeVisible)
          On Error GoTo 0
          If Not r Is Nothing Then
          If r.Rows.Count > 1 Then
          Dim toConcatenate As Variant
          toConcatenate = Application.Transpose(r.Value2)
          toConcatenate = VBA.Strings.Join(toConcatenate, ", ") ' <-- Change to whatever delimiter you want
          Worksheets("Search Results").Range("B7").Value2 = toConcatenate
          Else
          Worksheets("Search Results").Range("B7").Value2 = r.Value2
          End If
          End If
          .Parent.AutoFilterMode = False

          .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
          If .Range("A1").Value = "dummy header" Then .Range("A1").ClearContents ' remove any "dummy" header
          End With
          End With

          Application.ScreenUpdating = True

          End Sub





          share|improve this answer
























          • @Noob I don't know what you mean by result, I can't see your sheet. Try stepping through the code with F8

            – chillin
            Jan 8 at 18:23














          0












          0








          0







          Untested, I basically just inserted some code within your If branch.



          Few things to note:





          • Application.Transpose (used below to convert 2-d array to 1-d array) can only handle arrays of length ~65.5k. So if you have more filtered items than that, not all of them may get concatenated.

          • Cells have a character limit of ~32.8k I think. If the result of your concatenation violates this limit, you may get an error when trying to assign the result.


          But other than that, should work fine. Also, both of your Application.ScreenUpdating assignments seem to be True. You might want to look into that.



          Option Explicit

          Sub ReturnTIResults()

          Dim r As Range

          Application.ScreenUpdating = True

          With Worksheets("Sheet1") ' reference results sheet
          If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
          ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

          .AutoFilterMode = False
          With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
          .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
          .AutoFilter Field:=1, Criteria1:="=TI"
          On Error Resume Next
          Set r = .Resize(.Rows.Count - 1, 1).Offset(1, 1).SpecialCells(xlCellTypeVisible)
          On Error GoTo 0
          If Not r Is Nothing Then
          If r.Rows.Count > 1 Then
          Dim toConcatenate As Variant
          toConcatenate = Application.Transpose(r.Value2)
          toConcatenate = VBA.Strings.Join(toConcatenate, ", ") ' <-- Change to whatever delimiter you want
          Worksheets("Search Results").Range("B7").Value2 = toConcatenate
          Else
          Worksheets("Search Results").Range("B7").Value2 = r.Value2
          End If
          End If
          .Parent.AutoFilterMode = False

          .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
          If .Range("A1").Value = "dummy header" Then .Range("A1").ClearContents ' remove any "dummy" header
          End With
          End With

          Application.ScreenUpdating = True

          End Sub





          share|improve this answer













          Untested, I basically just inserted some code within your If branch.



          Few things to note:





          • Application.Transpose (used below to convert 2-d array to 1-d array) can only handle arrays of length ~65.5k. So if you have more filtered items than that, not all of them may get concatenated.

          • Cells have a character limit of ~32.8k I think. If the result of your concatenation violates this limit, you may get an error when trying to assign the result.


          But other than that, should work fine. Also, both of your Application.ScreenUpdating assignments seem to be True. You might want to look into that.



          Option Explicit

          Sub ReturnTIResults()

          Dim r As Range

          Application.ScreenUpdating = True

          With Worksheets("Sheet1") ' reference results sheet
          If IsEmpty(.Range("A1")) Then .Range("A1").Value = "dummy header"
          ' if A1 is empty, put a "dummy" header to make AutoFilter work properly

          .AutoFilterMode = False
          With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).Offset(, -1) 'reference referenced sheet column A range from row 1 down to column B last not empty cell
          .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" ' fill referenced range blank cells with the same value as the not empty cell above
          .AutoFilter Field:=1, Criteria1:="=TI"
          On Error Resume Next
          Set r = .Resize(.Rows.Count - 1, 1).Offset(1, 1).SpecialCells(xlCellTypeVisible)
          On Error GoTo 0
          If Not r Is Nothing Then
          If r.Rows.Count > 1 Then
          Dim toConcatenate As Variant
          toConcatenate = Application.Transpose(r.Value2)
          toConcatenate = VBA.Strings.Join(toConcatenate, ", ") ' <-- Change to whatever delimiter you want
          Worksheets("Search Results").Range("B7").Value2 = toConcatenate
          Else
          Worksheets("Search Results").Range("B7").Value2 = r.Value2
          End If
          End If
          .Parent.AutoFilterMode = False

          .SpecialCells(xlCellTypeFormulas).ClearContents ' clear cell with formulas
          If .Range("A1").Value = "dummy header" Then .Range("A1").ClearContents ' remove any "dummy" header
          End With
          End With

          Application.ScreenUpdating = True

          End Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 20:50









          chillinchillin

          1,671134




          1,671134













          • @Noob I don't know what you mean by result, I can't see your sheet. Try stepping through the code with F8

            – chillin
            Jan 8 at 18:23



















          • @Noob I don't know what you mean by result, I can't see your sheet. Try stepping through the code with F8

            – chillin
            Jan 8 at 18:23

















          @Noob I don't know what you mean by result, I can't see your sheet. Try stepping through the code with F8

          – chillin
          Jan 8 at 18:23





          @Noob I don't know what you mean by result, I can't see your sheet. Try stepping through the code with F8

          – chillin
          Jan 8 at 18:23




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042466%2fhow-can-i-concatenate-column-b-contents-if-the-cell-under-autofilter-identifier%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas