Power Query Code Issue - Adding Line Feed

Tashat

Board Regular
Joined
Jan 12, 2005
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hello All

I had another post on here yesterday asking about VBA code, but with the help of some Fluff and Peter_SSs they informed me the problem was probably with my Power Query code and not the VBA. I have VBA which allows the user to select multiple entries from a drop-down list - a2:b7 (table1). I then have a Power Query to separate those entries out into separate cells f2:g14. I then ran a pivot table based on f2:g14 to group the fruits together. However, as you can see from the example below, this makes fruits repeat and I believe it is because the VBA or the Power Query are "seeing" the fruits differently depending on where they appeared in the original user entered list a2:b7 - i.e. if the user put them at the top/middle of their list or if they appeared at the bottom of the list. Fluff believed it was related to the power query adding a line feed to some of the entries. Is anyone able to help me edit the power query code or VBA to prevent this from happening please? I have added both codes below. Thanks in advance


Fruit Example.xlsm
ABCDEFGHIJK
1User Input using VBA to have multiple Entries from a drop-down listPower Query Output to separate the entries in column A to separate cells and repeat the shopPivot Table to count the number of shops for each fruit purchase. Issue is here because fruits are repeated instead of grouped
2Fruit TypeShopFruit TypeShopRow LabelsCount of Shop
3Strawberries Apples PearsSainsburysStrawberries SainsburysApples 2
4Apples Pears BlueberriesMorrisonsApples SainsburysBlueberries1
5Blueberries OrangesAsdaPearsSainsburysBlueberries 1
6Oranges PearsTescoApples MorrisonsOranges1
7Pears StrawberriesSainsburysPears MorrisonsOranges 1
8BlueberriesMorrisonsPears2
9Blueberries AsdaPears 2
10OrangesAsdaStrawberries1
11Oranges TescoStrawberries 1
12PearsTescoGrand Total12
13Pears Sainsburys
14StrawberriesSainsburys
15Data Validation List
16Strawberries
17Apples
18Oranges
19Apples
20Pears
21Blueberries
Sheet1
Cells with Data Validation
CellAllowCriteria
A3:A11List=$C$16:$C$21


SQL:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Split = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Fruit Type", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Fruit Type")
in
    Split

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 1 Then
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
      If Oldvalue = "" Then
        Target.Value = Newvalue
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & vbNewLine & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Change the code to use:

Code:
Target.Value = Oldvalue & vbLf & Newvalue

rather than:

Code:
Target.Value = Oldvalue & vbNewLine & Newvalue
 
Upvote 0
Solution
Change the code to use:

Code:
Target.Value = Oldvalue & vbLf & Newvalue

rather than:

Code:
Target.Value = Oldvalue & vbNewLine & Newvalue
Hi RoryA - That's amazing! thank you so much. Do you mind explaining to me what the difference is between the code and why it helped? I'm trying to learn it for myself. Thank you so much.
 
Upvote 0
vbNewLine is actually two characters - a carriage return and a line feed. Your PQ code is only splitting on the linefeed, so some of your values actually had a carriage return character on the end, making them different to the ones that did not, even though they look identical.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top