VBA Code vbNewLine Issue

Tashat

Board Regular
Joined
Jan 12, 2005
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hi All - I am not able to write my own VBA code without support and I have found some code on another site which works almost perfectly, but I need some advice on a tweak, but I don't know the syntax. Am I allowed to post the code here and credit the person who wrote it, but just ask for some support on changing it slightly?

It is code which allows a data validation drop-down box to accept multiple entries and puts each entry on a new line within the cell (example below without code). My issue is that the last entry in each cell automatically has a new line (vbNewLine) appended to it, making it "different" from an entry which appears at the top of the list, or mid-list when I produce a power query based on the data. I've included an example below (without the code) to illustrate my point. For example "Strawberries" in cells A2 and A5 are the "same", but in cell A4 it is "Strawberries" with a line break after, so therefore is "viewed" differently in my PowerQuery. Am I allowed to post the code in here to ask for help with changing it so the last entry in each cell doesn't have a line-break (vbNewLine) associated with it?


Book1
AB
1ListShop
2Strawberries Apples OrangesSainsburys
3Apples PearsMorrisons
4Blueberries Oranges StrawberriesTesco
5Oranges Apples Strawberries BlueberriesAsda
Sheet1
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Am I allowed to post the code here and credit the person who wrote it,
Provided that does not breach the term and conditions/rules of the other site, "yes".
However, if the code comes from another site, why not ask there?
 
Upvote 0
Hi Peter_SSs - Thank you for your reply. I would ask on the other site, but no one responds to comments anymore from the looks of it. I am allowed to share it as long as I credit the author in the code, which I have.

The code is below and I just want to prevent the last entry in each of my cells having a new line appended to it. Any help would be much appreciated. I'm learning so much from this site.

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 = 7 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
 
Upvote 0
I can't duplicate your problem. can you post a link to your workbook?
 
Upvote 0
I can't duplicate your problem. can you post a link to your workbook?
Hi there

Here is an example to illustrate. The issue is that for example "Pears" in cell A2 is captured as "Pears (with a line break)" and "Pears" in cell A3 is just "Pears". So when I run a PowerQuery to allow me to extract the different fruits as individual lines, it "sees" "Pears" differently - because whichever fruit appears as the last entry in each cell has a line-break appended to it. So I want to edit the VBA so that it stops the last entry in each cell having a line break. Is that possible, bearing in mind that cells could have any number of entries?
Thanks in advance

Fruit Example.xlsm
ABC
1ListShop
2Strawberries Apples PearsSainsburys
3Apples Pears BlueberriesMorrisons
4Blueberries OrangesAsda
5Oranges PearsTesco
6Pears StrawberriesSainsburys
7
8
9
10
11
12
13
14
15Strawberries
16Apples
17Oranges
18Apples
19Pears
20Blueberries
Sheet1
Cells with Data Validation
CellAllowCriteria
A2:A10List=$C$15:$C$20


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
 
Upvote 0
Starting with a blank cell in A2 with that code in place and Data Validation as shown ...
Pick Strawberries from the drop-down list
then pick Apples from the drop-down list
then pick Pears from the drop-down list
.. does not produce a line-break (or any other character) after the word Pears for me.

I have checked both by analysing the results and also stepping through the code a line at a time and it produces no character at all after the 's' at the end of pears.

If you are getting another character then I suspect there must be other code in the workbook somehow producing that. What evidence is there that there is a line-feed at the end?

What do you get returned if you put this formula in a blank cell?

=CODE(RIGHT(A2,1))
 
Upvote 0
it "sees" "Pears" differently - because whichever fruit appears as the last entry in each cell has a line-break appended to it.
I suspect that the problem is the last value does not have a line feed after it, whereas all the other values will.
 
Upvote 0
Yes perhaps I'm wrong about it adding a line break after the last entry. What I do know is that I have pulled a PowerQuery to extract each of the fruits into separate rows and then it repeats the supermarket name, that way I can pull a pivot table from that data to group all oranges together and then which supermarkets they apply to, all pears, all apples etc. However, the pivot table repeats oranges twice and I believe it's because there is something (not visibly) different about them and I believe it is dependent on where in the list the user has put them - i.e. at the top/middle or at the end of the list. I hope that makes sense.
 
Upvote 0
I believe it's because there is something (not visibly) different about them
There is, the last value in the cell does not have a line feed after, whereas all the others do.
I suspect the problem is with your Power Query & not the data validation. So I would suggest you start a thread in the Power BI section & post the code you are using for your PQ.
 
Upvote 0
There is, the last value in the cell does not have a line feed after, whereas all the others do.
I suspect the problem is with your Power Query & not the data validation. So I would suggest you start a thread in the Power BI section & post the code you are using for your PQ.
Amazing - thank you so much for helping me unpick this. I will post that soon. Thank you again.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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