+ Reply to Thread
Results 1 to 14 of 14

Need VBA coding for carriage return

  1. #1
    Registered User
    Join Date
    04-24-2022
    Location
    Boulder, CO
    MS-Off Ver
    2016
    Posts
    6

    Need VBA coding for carriage return

    I am trying to create a macro that will call another macro only when a carriage return is detected in a specific cell. The carriage return is a constant and unfortunately I do not know the VBA coding syntax required to handle a constant. Here is a sample of what I developed (which does not work). Any help will be appreciated.

    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 05-08-2022 at 03:17 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,162

    Re: Need VBA coding for carriage return

    Welcome to the Forum rogers1944 !

    First, Target is input to the Sub and you are wiping it out. It tells you what cells changed. You need to test to see if D85 is the cell that changed.

    Second Target is a Range, and Range has no method or property called Const.

    Third, your code seems to be looking to see if the cell has nothing but a carriage return. Do you really want to see if the cell contains some text that includes a carriage return?

    And last, you probably want to also look for a line feed, which is what you get if you type ALT+ENTER into a cell.

    Please try this and see if it does what you want.
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    04-24-2022
    Location
    Boulder, CO
    MS-Off Ver
    2016
    Posts
    6

    Re: Need VBA coding for carriage return

    Thanks for applying the code tags -- unfortunately I did not see that requirement. And thanks also for providing the additional explanations regarding the coding requirements.

    I have implemented your code, but perhaps I have another problem. For the worksheet that this code will be applied, there already are Worksheet-Activate and Worksheet-Click functions (the Activate needs to take precedence). I added the Worksheet-Change, but also found that the F8 debug would not function on the Change code. Moreover, the new code did not function when a carriage return was initiated in cell D85. Do you have any suggestions as to how to remedy this situation?

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Need VBA coding for carriage return

    There is nothing wrong with the code in post #2 so any problem is in the implementation - attached workbook has code in the Change event of Sheet1 as an example (Displays a message box rather than calling a procedure). Other event procedures are not affected by this, but there is no 'Worksheet-Click' event.

    ...but also found that the F8 debug would not function on the Change code.
    If you mean you cannot simply run the procedure by stepping through it using F8 then you cannot do that if the procedure has any parameters declared but you can add a Break-point (F9) on any executable line to display the code before the line with the break-point executes.
    Attached Files Attached Files
    Last edited by cytop; 05-09-2022 at 12:39 AM.

  5. #5
    Registered User
    Join Date
    04-24-2022
    Location
    Boulder, CO
    MS-Off Ver
    2016
    Posts
    6

    Re: Need VBA coding for carriage return

    I downloaded the Book1 file provided by cytop and installed it on my computer. When I click a carriage return in cell D85, the cursor moves to cell D86 and there is no message box appearing. I don't understand why this code works for two separate posters but it does not work on my computer. I have checked various settings on my computer, but did not find anything that might have an adverse effect. Does anyone have any suggestions?

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,445

    Re: Need VBA coding for carriage return

    Hi rogers,

    Perhaps you need a SelectionChange event instead of WorksheetChange? See the attached and save it to your hard drive before testing it.
    Macros not enabled yet.xlsm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Registered User
    Join Date
    04-24-2022
    Location
    Boulder, CO
    MS-Off Ver
    2016
    Posts
    6

    Re: Need VBA coding for carriage return

    I downloaded and installed the "Macros not enabled yet" file from MarvinP. When I click on cell D85, the message box appears. I verified that the line feed option caused this action. But once I have selected cell D85 and execute a carriage return, no message box appears. Apparently detection of the carriage return is not occurring, which is most important to me. Hopefully we are getting closer to a solution. Other suggestions will be very appreciated.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,445

    Re: Need VBA coding for carriage return

    Hi rogers,

    Excel does what it does and we mere mortals need to figure out why we don't understand what it is doing. The SelectionChange event doesn't fire if you don't make a change in the cell. IE - you need to change something in D85 to have that event fire.
    Then if you look at every character in the string you don't have a CRLF character in D85. I've created a character by character list of the string in D85 and you only have a Char(10).
    I think you get those line breaks in an Excel cell by doing Alt+Enter. Us mortals would assume it is a CRLF but I think it is only a LF??? (or is that only a CR??)
    Macros not enabled yet 2.xlsm

  9. #9
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Need VBA coding for carriage return

    Quote Originally Posted by rogers1944 View Post
    I downloaded the Book1 file provided by cytop ... When I click a carriage return in cell D85, the cursor moves to cell D86
    This is the default Excel behaviour. If you want to insert a new line when editing in a cell you must use Alt-Enter.

    The code provided by 6StringJazzer works as long as you edit D85 and add a new line as above. This is what you asked for in the OP

  10. #10
    Registered User
    Join Date
    04-24-2022
    Location
    Boulder, CO
    MS-Off Ver
    2016
    Posts
    6

    Re: Need VBA coding for carriage return

    Thanks to everyone who have offered assistance. Please note that I originally stated in my first post that I wanted an action to occur when a carriage return was detected in a specific cell. I believe that cytop hit the nail on the head when he stated that the default behavior of Excel is such that the cursor will move to an adjacent cell when a carriage return is executed. That action is apparently preventing a change detect from occurring.

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,162

    Re: Need VBA coding for carriage return

    I'm not sure that's clear.

    If you hit ENTER, the content of the cell is committed and the cursor moves to the next cell. There is no carriage return in the cell. This action can be detected with the Change event.

    If you are editing a cell and hit ALT+ENTER, a carriage return (line feed, actually) is inserted into the text and the cell remains open for editing. There is no event to be detected at this point.

    Can you please elaborate on your original request, that you take an action "when a carriage return is detected in a specific cell"?

  12. #12
    Registered User
    Join Date
    04-24-2022
    Location
    Boulder, CO
    MS-Off Ver
    2016
    Posts
    6

    Re: Need VBA coding for carriage return

    Hmm. this is getting interesting, since I am now discovering that there might be a difference between a carriage return and the Enter key. What I would like to accomplish is to enter or edit data in a specific cell and then hit the Enter key. Upon the Enter key being depressed, I would then like to start a macro (this would apply to the one cell only). Sorry if my original verbiage was misleading or incorrect.

  13. #13
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,445

    Re: Need VBA coding for carriage return

    Hi rogers,

    I gave you the code to display a message when you press enter. It is a Selection_Change event not a Worksheet_Change event (if you changed something in the cell.

    Set a breakpoint in both Events in my code in my attached file above and see which of the two event codes is run when you change D85 and press Enter/Return.

  14. #14
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,162

    Re: Need VBA coding for carriage return

    Quote Originally Posted by rogers1944 View Post
    What I would like to accomplish is to enter or edit data in a specific cell and then hit the Enter key. Upon the Enter key being depressed, I would then like to start a macro
    This is not detecting a carriage return in the cell, this is detecting a change to the cell, hence the Change event will be fired.

    Quote Originally Posted by MarvinP View Post
    I gave you the code to display a message when you press enter. It is a Selection_Change event not a Worksheet_Change event (if you changed something in the cell.
    He will get the Selection_Change event when he selects the cell, then the Change event when he hits ENTER, then the Selection event again when the selection is automatically moved to the cell below after he hits ENTER.

    From the description, it sounds like rogers1944 wants to run a macro when the value in D85 is changed by an edit. So the Change event is appropriate, therefore my code should work.

+ 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. [SOLVED] How do I get a carriage return?
    By Excelski in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2017, 06:23 AM
  2. [SOLVED] replacing two carriage return wit one carriage return
    By CDandVinyl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-05-2015, 05:55 PM
  3. Carriage Return
    By hezcal1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2012, 09:27 AM
  4. Tab Carriage Return
    By WhtKnt in forum Excel General
    Replies: 5
    Last Post: 01-05-2011, 05:00 PM
  5. Carriage Return in a Concatenation
    By [email protected] in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 08-12-2006, 12:15 PM
  6. Carriage Return
    By rexmann in forum Excel General
    Replies: 3
    Last Post: 05-09-2006, 11:25 AM
  7. Squares for carriage return
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 12:05 PM

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