SOLVED

Need help on VBA code

%3CLINGO-SUB%20id%3D%22lingo-sub-3004367%22%20slang%3D%22en-US%22%3ENeed%20help%20on%20VBA%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3004367%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20code's%20purpose%20is%20needs%20to%20print%26nbsp%3B%3CSPAN%3Eif%20there%20is%20a%20'True'%20in%20A%20column%2C%20it%20should%20print%20the%20corresponding%20part%20in%20D%20column%20in%20order.%20Every%20time%20it's%20printing%20the%20first%20item%20in%20the%20D%20column%20to%20the%20C%20column%20which%20is%20not%20I%20want.%20There%20is%20something%20wrong%20with%20this%20code.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EAttached%20files%20below%20xlsx%20is%20original%20one%2C%20xlsm%20file%20is%20formatted%20one.%20%C4%B0n%20xlsx%20there%20is%20a%20sample%20line%20in%2012%20to%20show%20how%20it%20should%20be.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3ECan%20somebody%20help%20me%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22%22%3EFunction%20SelItems(rng1%2C%20rng2)%20As%20String%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EConst%20sep%20%3D%20vbLf%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EDim%20a1()%20As%20String%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EDim%20a2()%20As%20String%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EDim%20i%20As%20Long%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EDim%20j%20As%20Long%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EDim%20s%20As%20String%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3Ea1%20%3D%20Split(rng1%2C%20vbLf)%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3Ea2%20%3D%20Split(rng2%2C%20%22%2C%22)%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EFor%20i%20%3D%20UBound(a1)%20-%205%20To%20UBound(a1)%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EIf%20a1(i)%20%3D%20%22True%22%20Then%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3Es%20%3D%20s%20%26amp%3B%20sep%20%26amp%3B%20a2(j)%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3Ej%20%3D%20j%20%2B%201%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EEnd%20If%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3ENext%20i%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EIf%20s%20%26lt%3B%26gt%3B%20%22%22%20Then%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3ESelItems%20%3D%20Mid(s%2C%20Len(sep)%20%2B%201)%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3EEnd%20If%3C%2FP%3E%3CP%20class%3D%22%22%3EEnd%20Function%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3004367%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20Mac%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3004499%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20help%20on%20VBA%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3004499%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1228329%22%3E%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%40Meceka%3C%2FA%3E%26nbsp%3BTested%20the%20UDF%20that%20you%20received%20earlier%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%3E%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3Bhere%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel%2Fneed-help-with-copy-values-in-comma-seperated-data-into-another%2Fm-p%2F3001593%22%20target%3D%22_self%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel%2Fneed-help-with-copy-values-in-comma-seperated-data-into-another%2Fm-p%2F3001593%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20it%20seems%20the%20the%20first%20line-feed%20in%20each%20of%20the%20Option%20strings%20is%20messing%20up%20things.%20Change%20the%20code%20to%20this%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EOption%20Explicit%0A%0AFunction%20SelItems(rng1%2C%20rng2)%20As%20String%0A%20%20%20%20Const%20sep%20%3D%20vbLf%0A%20%20%20%20Dim%20a1()%20As%20String%0A%20%20%20%20Dim%20a2()%20As%20String%0A%20%20%20%20Dim%20i%20As%20Long%0A%20%20%20%20Dim%20s%20As%20String%0A%20%20%20%20a1%20%3D%20Split(rng1%2C%20vbLf)%0A%20%20%20%20a2%20%3D%20Split(rng2%2C%20%22%2C%22)%0A%20%20%20%20For%20i%20%3D%20UBound(a1)%20-%205%20To%20UBound(a1)%0A%20%20%20%20%20%20%20%20If%20a1(i)%20%3D%20%22True%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20s%20%26amp%3B%20sep%20%26amp%3B%20a2(i%20-%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20i%20%2B%201%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20i%0A%20%20%20%20If%20s%20%26lt%3B%26gt%3B%20%22%22%20Then%0A%20%20%20%20%20%20%20%20SelItems%20%3D%20Mid(s%2C%20Len(sep)%20%2B%201)%0A%20%20%20%20End%20If%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eand%20I%20believe%20it%20works%20as%20desired.%3C%2FP%3E%3CP%3EAlternatively%2C%20consider%20changing%20the%20set-up%20of%20your%20schedule%20as%20suggested%20to%20you%20by%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%3E%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%40Jan%20Karel%20Pieterse%3C%2FA%3E%20in%20this%20thread%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel%2Fhow-to-copy-values-in-comma-seperated-data-into-another-column%2Fm-p%2F3000765%22%20target%3D%22_self%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel%2Fhow-to-copy-values-in-comma-seperated-data-into-another-column%2Fm-p%2F3000765%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%2C%20the%20%3CEM%3E%3CSTRONG%3EData%2C%20From%20Table%3C%2FSTRONG%3E%3C%2FEM%3E%20wouldn't%20work%20for%20you%20since%20you%20are%20on%20a%20Mac.%20But%20there%20are%20other%20ways%20and%20than%20no%20VBA%20coding%20is%20needed.%20If%20you%20are%20on%20MS365%20or%20Excel%202021%20you%20could%20have%20a%20look%20at%20the%20attached%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3004541%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20help%20on%20VBA%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3004541%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%3E%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blank%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%2C%20thank%20you%20for%20your%20answer.%20%C4%B0n%20this%20attached%20image%20below%2C%20there%20are%204%20true%20in%20column%20A%20but%20just%202%20of%20them%20printing%20in%20column%20C%20with%20your%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20have%20any%20idea%20about%20that%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3004688%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20help%20on%20VBA%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3004688%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1228329%22%3E%40%20target%3D_blank%20target%3D_blank%20target%3D_blank%20target%3D_blankMeceka%3C%2FA%3E%26nbsp%3BCan't%20tell.%20Perhaps%20not%20all%20%22True%22%20entires%20in%20column%20A%20are%20exactly%20the%20same.%20And%20the%20output%20doesn't%20seem%20to%20be%20coming%20from%20the%20UDF%2C%20as%20I%20would%20expect%20a%20comma%20separating%20the%20items.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3004715%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20help%20on%20VBA%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3004715%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1228329%22%3E%40%20target%3D_blank%20target%3D_blankMeceka%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOption%20Explicit%3C%2FP%3E%3CP%3EFunction%20SelItems(rng1%2C%20rng2)%20As%20String%3CBR%20%2F%3EConst%20sep%20%3D%20vbLf%3CBR%20%2F%3EDim%20a1()%20As%20String%3CBR%20%2F%3EDim%20a2()%20As%20String%3CBR%20%2F%3EDim%20i%20As%20Long%3CBR%20%2F%3EDim%20j%20As%20Long%3CBR%20%2F%3EDim%20s%20As%20String%3CBR%20%2F%3Ea1%20%3D%20Split(rng1%2C%20vbLf)%3CBR%20%2F%3Ea2%20%3D%20Split(rng2%2C%20%22%2C%22)%3CBR%20%2F%3EFor%20i%20%3D%20UBound(a1)%20-%205%20To%20UBound(a1)%3CBR%20%2F%3EIf%20a1(i)%20%3D%20%22True%22%20Then%3CBR%20%2F%3Es%20%3D%20s%20%26amp%3B%20sep%20%26amp%3B%20a2(j)%3CBR%20%2F%3Ej%20%3D%20j%20%2B%201%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3EElse%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3Ej%20%3D%20j%20%2B%201%3C%2FFONT%3E%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%20i%3CBR%20%2F%3EIf%20s%20%26lt%3B%26gt%3B%20%22%22%20Then%3CBR%20%2F%3ESelItems%20%3D%20Mid(s%2C%20Len(sep)%20%2B%201)%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EEnd%20Function%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20me%20it%20seems%20that%20you%20simply%20have%20to%20add%20the%20else%20part%20of%20the%20if%20then%20else%20statement.%20With%20this%20the%20formula%20returns%20the%20expected%20result%20in%20my%20spreadsheet.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3004874%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20help%20on%20VBA%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3004874%22%20slang%3D%22en-US%22%3EThis%20is%20working%20absolutely%20fine.%20Thank%20you%20so%20much%20for%20your%20time.%20I%20appreciate%20that.%3C%2FLINGO-BODY%3E
New Contributor

This code's purpose is needs to print if there is a 'True' in A column, it should print the corresponding part in D column in order. Every time it's printing the first item in the D column to the C column which is not I want. There is something wrong with this code.

 

Attached files below xlsx is original one, xlsm file is formatted one. İn xlsx there is a sample line in 12 to show how it should be.

Can somebody help me?

 

Function SelItems(rng1, rng2) As String

    Const sep = vbLf

    Dim a1() As String

    Dim a2() As String

    Dim i As Long

    Dim j As Long

    Dim s As String

    a1 = Split(rng1, vbLf)

    a2 = Split(rng2, ",")

    For i = UBound(a1) - 5 To UBound(a1)

        If a1(i) = "True" Then

            s = s & sep & a2(j)

            j = j + 1

        End If

    Next i

    If s <> "" Then

        SelItems = Mid(s, Len(sep) + 1)

    End If

End Function

8 Replies
best response confirmed by Meceka (New Contributor)
Solution

@Meceka Tested the UDF that you received earlier from @Hans Vogelaar here:

https://techcommunity.microsoft.com/t5/excel/need-help-with-copy-values-in-comma-seperated-data-into... 

and it seems the the first line-feed in each of the Option strings is messing up things. Change the code to this:

 

Option Explicit

Function SelItems(rng1, rng2) As String
    Const sep = vbLf
    Dim a1() As String
    Dim a2() As String
    Dim i As Long
    Dim s As String
    a1 = Split(rng1, vbLf)
    a2 = Split(rng2, ",")
    For i = UBound(a1) - 5 To UBound(a1)
        If a1(i) = "True" Then
            s = s & sep & a2(i - 1)
            i = i + 1
        End If
    Next i
    If s <> "" Then
        SelItems = Mid(s, Len(sep) + 1)
    End If
End Function

 

and I believe it works as desired.

Alternatively, consider changing the set-up of your schedule as suggested to you by @Jan Karel Pieterse in this thread:

https://techcommunity.microsoft.com/t5/excel/how-to-copy-values-in-comma-seperated-data-into-another... 

However, the Data, From Table wouldn't work for you since you are on a Mac. But there are other ways and than no VBA coding is needed. If you are on MS365 or Excel 2021 you could have a look at the attached file.

 

Hi @Riny_van_Eekelen , thank you for your answer. İn this attached image below, there are 4 true in column A but just 2 of them printing in column C with your code.

 

Do you have any idea about that?

@Meceka Can't tell. Perhaps not all "True" entires in column A are exactly the same. And the output doesn't seem to be coming from the UDF, as I would expect a comma separating the items.

@Meceka 

Option Explicit

Function SelItems(rng1, rng2) As String
Const sep = vbLf
Dim a1() As String
Dim a2() As String
Dim i As Long
Dim j As Long
Dim s As String
a1 = Split(rng1, vbLf)
a2 = Split(rng2, ",")
For i = UBound(a1) - 5 To UBound(a1)
If a1(i) = "True" Then
s = s & sep & a2(j)
j = j + 1
Else
j = j + 1
End If
Next i
If s <> "" Then
SelItems = Mid(s, Len(sep) + 1)
End If
End Function

 

 

To me it seems that you simply have to add the else part of the if then else statement. With this the formula returns the expected result in my spreadsheet.

This is working absolutely fine. Thank you so much for your time. I appreciate that.

Hi, @Quadruple_Pawn  I wonder if there is a way to add commas between each other in column C instead of line break and space? Because I will need to export it as a CSV file. Please see attached image as an example

@Meceka 

This is probably what you want to do. 

I only changed on line of code to:

s = s & "," & a2(j)

@Quadruple_Pawn Thank you for your kind answer. Now I get real data. And its different than the example one. Now we need to get values between B6 to B11 to print those 6 lines 'True' values into column C as you did before.

 

What can I do here? This is final round of test.