![]() |
|
|
|
#1
|
||||
|
||||
|
You are asking AI to create code and it pretends it knows what it is doing but I can tell you, in this case it really doesn't. I don't recognise the .DefaultSorting command and it absolutely doesn't belong there (what would you expect to happen if you sorted a single thing? what would it go in front or behind of?). If you removed that line AND the subsequent one then I would expect the code to actually have a chance to work without the error you are seeing.
The CleanText function would also need to be edited to actually do something useful. Looking at how the code is calling CleanText, the intention is to return a valid Bookmark name. I don't know the comprehensive rules for valid bookmark names but I can say that a bookmark name can't include spaces or begin with a number. If your selected text is a single word then the macro 'could' run without an error but in most cases it will probably fail because of what was selected when the macro started. The MsgBox lines aren't right either but at least they don't stop the macro from proceeding. Then you have to ask, what is the ultimate point of the code. Are you thinking it is useful to create a bookmark around a word with a name that matches the word? How is this going to be something useful? You should look at CoPilot generated code as an opportunity to learn about coding rather than expecting it to actually work. If this is an example of what it can do then it might be rare that it produces errorless code. This could be because of limitations in AI or perhaps because you asked the question in a 'non-optimal' way. Perhaps if you experimented with asking the question in different ways, you could compare the various code outputs to see if there are variants that work as you expected.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#2
|
|||
|
|||
|
Thank you for the prompt replies. I think you can tell I am very amateur to VBA code and to what CoPilot is/can do. Now I know.
The point of the code I am looking for is simple: take a selected list of names from a table and create bookmarks for each of them. So I'm expecting some kind of for loop that goes through the range of selected text line by line and creates a bookmark for each. Does anyone have some REAL sample code I could try? ![]() Example: Table A: Column 1 Column 2 Column 3 ... Row1 Name1 Row2 Name2 Row3 Name3 Row4 Name4 In my word document Table A I select just Name2 and Name3, run the macro and end up with two new Bookmarks: Name2, Name3 Last edited by randyspaulding12; 05-28-2024 at 07:43 AM. Reason: I wasn't clear enough in my example showing I was selecting from a table. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VBA Code to copy data from two sheet ,paste into corresponding sheet through cmdbutton | jackfruit88 | Excel Programming | 1 | 07-08-2022 09:27 PM |
| paste returns the wrong not the latest cut | Peborgh | Word | 2 | 11-23-2021 03:31 AM |
Error 4605 while trying to copy and paste with same document
|
Ilmari | Word VBA | 2 | 05-05-2020 12:08 AM |
Copy/Paste error
|
stuart255 | Office | 5 | 05-06-2019 02:13 AM |
| VBA Newbie (Trying to write a code to copy and paste info from one sheet to another) | Forum2000s | Excel Programming | 2 | 05-07-2017 04:51 AM |