+ Reply to Thread
Results 1 to 18 of 18

Copying text from clipboard to first empty row

  1. #1
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Copying text from clipboard to first empty row

    I have a 30 line text file with all lines looking like this, some separated by empty lines. All have a 'return' (CR LF) at the end.

    Please Login or Register  to view this content.
    I have externally copied all the text to the clipboard. My VBA macro includes this code which display it in a temporary message box. (Well, most of it. Apparently there is a 1024 character limit.)

    Please Login or Register  to view this content.
    But I'm struggling with the next two stages and would appreciate some help please.

    1. For each line, set a VBA variable containing the part after last equal sign. Ideally, but not essential, I'd like to use a variable name identical to the string after the first equal sign. So the first line would generate a string variable called 'tFileName' with value '20190706Cadgwith-Coverack-r1060-m7.3.gpx'.

    2. In the single, already open workbook 'WalkIndex.xlsm', sheet 'Target', create a new row from these variables at the end of all previous rows. So tFilename would be entered into col C, tDatePrefix into col B, etc. When my source was in another workbook I was doing it with the following code (developed with much help here):

    Please Login or Register  to view this content.

    I've attached an extract from WalkIndex.xlsm

    Any assistance would be much appreciated please.
    Attached Files Attached Files
    Terry, East Grinstead, UK
    Excel 365, Win 10 Pro, i7 PC, 4.0 GHz

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Copying text from clipboard to first empty row

    Quote Originally Posted by terrypin View Post
    ... set a VBA variable containing the part after last equal sign ...
    Have you tried using the 'Split' function ?

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Could be very easier from an attachment with the source text file and the expected result workbook accordingly …

  4. #4
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    The message you have entered is too short Please lengthen your message to at least 10 characters.

    What? The message was "Well, as you wish, here is the 'expected result workbook' with the extra two cells added. And a text file made by copy pasting the code I provided."

    And it had two files attached, but this is my third attempt to send my reply because of that daft message!
    Attached Files Attached Files

  5. #5
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    Quote Originally Posted by porucha vevrku View Post
    Have you tried using the 'Split' function ?
    Thanks, no, not yet but I will, splitting on " = ". Any suggestions about #2 please?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Copying text from clipboard to first empty row

    Hello terrypin,

    This macro will open the text file named "SourceText.txt" which is saved to the workbook's directory. It will read all the lines in the file looking for "tFileName" and "tDatePrefix". The latter will fill both columns "A" and "B" and the former column "C". The file name has the file type removed as in your example. The macro expects these labels to repeat together in pairs throughout the file. Once the file has been read, the data is output to the worksheet "Target" starting with the next empty row.

    The macro below has been added to the attached workbook. Try it out on your original text file and let me know the results.

    Macro added to the Module "Misc"
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    That looks great Leith, thanks so much for taking the time to develop it! To do it justice I'm going to get some chores out of the way, tackle it methodically afterwards, and report back, probably tomorrow.

    Posted Monday 27 January 2020, 1112 UK GMT.

  8. #8
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    Brilliant, you're a star! I was able to test it sooner than I expected and it worked perfectly.

    I'll now tackle extending it to cover the full set of variables, during which I may seek further help if I hit difficulties.

    When I have the project finished, I'll begin studying how it works!

    Terry, UK

  9. #9
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    Leith: I hit my first problems very quickly. The actual source files have 31 lines (including blanks), not 4. So I'm getting 'Run-time error '9': Subscript out of range' in the line
    Please Login or Register  to view this content.
    So far I haven't worked out which of your variables (or statements) I need to change.

    Unlike my simplified example the source files are actually named tTrackName.txt, such as "20190707Porthallow-MawnanSmith(Lift)-r765-m7.7". That's a variable I can get into VBA before running your macro. Do I then substitute it as I've shown here?

    Please Login or Register  to view this content.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Copying text from clipboard to first empty row

    Hello terrypin,

    If you post the actual text file you are using, I can then prevent these unexpected problems from occurring.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    As I asked for since post #3 …

  12. #12
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    Quote Originally Posted by Marc L View Post

    As I asked for since post #3 …
    I promptly showed you a simplified file.

  13. #13
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    Quote Originally Posted by Leith Ross View Post
    Hello terrypin,

    If you post the actual text file you are using, I can then prevent these unexpected problems from occurring.
    Thanks Leith, here's an example of a typical FULL file.
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by terrypin View Post
    I promptly showed you a simplified file.
    Which was very not useful, just lead to a waste of time for Leith …

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Now, 'cause of missing initial informations,
    attach the expected result workbook - so the after context - according to your last text file attachment …

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Copying text from clipboard to first empty row

    Hello terrypin,

    It has been a busy day. I found the problem in the macro. The Split function looks for delimiting character or characters and returns an array of substrings. The function will return the original string if no delimiter is found in a single zero based element array.

    Them macro assumed there would be at 2 or more equal signs in a valid line and that blank lines were truly blank. It appears that the last line of new file you provided contains spaces. I changed the compare line (in red font) in the loop from a > -1 to > 0. This fixed the problem, The amended macro below also allows you to chose the text file to open.

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    Thanks Leith, really grateful for your patient work on this. Sorry about those hidden spaces.

    I’ll try your revised macro when I return home later today.

    It’s frustrating being a VBA novice, working mainly in ‘copy/paste mode’. On the one hand, accomplishing a task optimally with advanced code like yours is great, but the downside is that I don’t understand it fully enough to be able to adapt it with confidence.

    So in parallel I’m also working on an alternative more intuitive approach to copying these variables, and later I’ll raise it in a separate post. It relies on that ground-breaking method you showed me for clipboard management in Windows 10, which I’m now using regularly, thanks again

    Terry, UK

  18. #18
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    533

    Re: Copying text from clipboard to first empty row

    Hi Leith,

    I've been working on this for a few hours this morning and thought you might appreciate an update.

    As expected, your revised macro worked perfectly, and can now be applied to ANY of my source files, thanks a bunch! So I'm now focusing on adapting it.

    I've made some comments about my editing aims in the copy of your macro that I will be using for my tentative edits. It's called Sub AddTrackData_All(), directly below yours and so I've attached an updated version of the workbook. I've also added a summary of the variables in the sheet "TEMP".

    I deliberately kept my initial post as short and simple as possible. But some extra background detail may now be useful.

    I'm a long term user of a program called Macro Express Pro, a high level scripting application. For non-programmers like me its GUI is much easier and intuitive, although of course it doesn't approach VBA for speed or versatility. I'm using it in this project with a program called GPS Utility. Up until recently my MX macro 'GPX ANALYSIS', supplemented by VBA, generated a tracksheet for each GPX file, example attached, and then copied its data to WalkIndex.xlsm.

    But I decided to simplify that drastically and no longer prepare a tracksheet. Instead, a greatly culled set of 21 variables are prepared by GPX ANALYSIS.

    As well as being displayable in text form and in profile images like the example attached, they are copied directly to a text file of the same name as the GPX file. I may even decide to dispense with that step, relying entirely on the one row summary in WalkIndex.xlsm, but meanwhile I'm using it as a basis for the copying under discussion here.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by terrypin; 01-28-2020 at 09:53 AM.

+ 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] Copying to first empty row?
    By terrypin in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-09-2020, 02:35 PM
  2. Replace Empty Text/Space in all Empty Cells
    By bhenlee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-04-2018, 10:07 PM
  3. [SOLVED] Copying to next empty row
    By 4evra in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2013, 07:17 PM
  4. Copying to Next Empty Row
    By Pasha81 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-10-2009, 01:27 PM
  5. copying text to empty cells
    By therealjag in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-24-2009, 04:28 AM
  6. copying a range to first empty row is copying over data
    By jseufert in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2009, 03:45 AM
  7. copying empty cells as empty
    By sjhonny in forum Excel General
    Replies: 2
    Last Post: 01-17-2007, 06:52 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