+ Reply to Thread
Results 1 to 2 of 2

How can I use a VBA code on an example ?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-10-2011
    Location
    Bucharest, RO
    MS-Off Ver
    Excel 2021
    Posts
    141

    How can I use a VBA code on an example ?

    I have the following VBA code (thanks for this code: admin - excelfox.com forum):

    Option Explicit
    
    Function CONCATIFS(ByVal ConcatCol As Variant, ByVal Delim As String, ParamArray ParamA() As Variant) As String
        
        '//ParamA=each pair should be Criteria range followed by it's criteria
        
        Dim lngLoopC        As Long
        Dim lngLoopR        As Long
        Dim lngLoop         As Long
        Dim lngCount        As Long
        Dim lngCounter      As Long
        Dim lngIndex        As Long
        Dim lngCase         As Long
        Dim varOP()         As Variant
        Dim strMatch        As String
        Dim blnTranspose    As Boolean
        
        If TypeOf ConcatCol Is Range Then
            If ConcatCol.Columns.Count > 1 And ConcatCol.Rows.Count = 1 Then
                blnTranspose = True
                ConcatCol = Application.Transpose(Application.Transpose(ConcatCol.Value2))
            ElseIf ConcatCol.Columns.Count = 1 And ConcatCol.Rows.Count > 1 Then
                ConcatCol = Application.Transpose(ConcatCol.Value2)
            End If
        End If
        
        For lngLoop = LBound(ParamA) To UBound(ParamA)
            If TypeOf ParamA(lngLoop) Is Range Then
                If blnTranspose Then
                    ParamA(lngLoop) = Application.Transpose(Application.Transpose(ParamA(lngLoop).Value2))
                Else
                    ParamA(lngLoop) = Application.Transpose(ParamA(lngLoop).Value2)
                End If
            End If
        Next
        
        ReDim varOP(1 To UBound(ConcatCol))
        lngCount = (1 + UBound(ParamA)) \ 2
        For lngLoopR = LBound(ConcatCol) To UBound(ConcatCol)
            lngCounter = 0
            For lngLoopC = LBound(ParamA) To UBound(ParamA) Step 2
                If LCase(ParamA(lngLoopC)(lngLoopR)) = LCase(ParamA(lngLoopC + 1)) Then
                    lngCounter = lngCounter + 1
                End If
            Next
            If lngCount = lngCounter Then
                If Len(Trim(ConcatCol(lngLoopR))) Then
                    If InStr(1, strMatch & "|", "|" & ConcatCol(lngLoopR) & "|", lngCase) = 0 Then
                        lngIndex = lngIndex + 1
                        varOP(lngIndex) = ConcatCol(lngLoopR)
                        strMatch = strMatch & "|" & ConcatCol(lngLoopR)
                    End If
                End If
            End If
        Next
        If lngIndex Then
            ReDim Preserve varOP(1 To lngIndex)
            CONCATIFS = Join(varOP, Delim)
        End If
        
    End Function
    Which has the following function:

    =CONCATIFS(
    the rows can be concatenated;
    concatenation separator;
    the rows of criterion 1;the value you are looking for in criterion 1;
    the rows of criterion 2;the value you are looking for in criterion 2)

    Example: =CONCATIFS($C$2:$C$50,", ",$A$2:$A$50,F$1,$B$2:$B$50,$E2)

    Now, I would like to use CONCATIFS (see the attached file below).
    So, in this file are three columns: Motivation, Names and A + B. Since Column B is filled randomly, depending on the B values, column C will be automatically populated. In the example from the file, now in cell F2, I would like to list (separated by ";") only the content of C2, C3 and C4 cells. How can I do this?
    Attached Files Attached Files

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: How can I use a VBA code on an example ?

    If I understand you well, you would like to use:
    Formula: copy to clipboard
    =CONCATIFS(C2:C11,"; ",B2:B11,"<>")
    but the function do not work with "<>" as for instance COUNTIF etc.

    Simple solution would be by modifying this :
                If LCase(ParamA(lngLoopC)(lngLoopR)) = LCase(ParamA(lngLoopC + 1)) Then
                    lngCounter = lngCounter + 1
                End If
    by adding "is not empty" check:
                If (ParamA(lngLoopC + 1) = "<>" And Len(ParamA(lngLoopC)(lngLoopR)) > 0) Or _
                  LCase(ParamA(lngLoopC)(lngLoopR)) = LCase(ParamA(lngLoopC + 1)) Then
                    lngCounter = lngCounter + 1
                End If
    See attached file. (Note that it stil do not work as expected with such parameters as ">10" etc.)
    Attached Files Attached Files
    Best Regards,

    Kaper

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 01-07-2016, 12:25 PM
  2. [SOLVED] Need help with VBA code. Tons of code seperated in two, second part of code doesn't work.
    By FragaGeddon in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 11-30-2015, 11:46 AM
  3. [SOLVED] Pattern Building VBA Code - Working code, would like to use cleaner code
    By Benisato in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-10-2015, 03:05 PM
  4. [SOLVED] Excel VB code. Message pops up while code running asking question. Code must not wait.
    By Heinrich Venter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2014, 06:10 AM
  5. [SOLVED] VBA code for assigning a numeric code to text; then numeric code populates table
    By cteaster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2014, 08:01 PM
  6. Replies: 2
    Last Post: 03-09-2013, 04:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1