How to have a macro compare two texts and change them if identical?

I think it must be an if/then but cannot figure it out.

Yes, this is an if/then. The exact condition depends on what form these texts are in (variables, clipboard, files, etc.) but the basic process is something like this:

Test for Identical Variables.kmmacros (3.3 KB)
03 PM

Thank you. I’m having a hard time understanding variables but I’m trying to work it out.

These are in text files with boxes with titles. If what text is inside two particular form field boxes is identical then both texts need to be changed to the same new text (the identical text may be different from pair to pair but the new text would always be the same). This needs to go on multiple times for different pairs of boxes. I think somehow I need to set the variables to the boxes and specify their box pairs?

Also, to possibly save from starting another thread, I’m having difficulty on another (separate and unrelated) aspect too: in another file names need to be moved one at a time but many (but not all) will have middle initials that need to be erased, some with periods and some with just the letter. Some even have two middle initials. I think it may be impossible to make a macro that can fix all these conditions but I’m thinking there must be some way to at least automatically erase the single middle initial without a period ones, which are the most common. One other complication is that sometimes the first name is a single initial and sometimes there’s more than just the firstname-middleinitial-lastname format, such as two last names or two first names.

The farthest I’ve got is that I can do this by using the clipboard and an if/then and somehow to specify to look for a single letter surrounded by spaces on either side inside the text to delete if there, but I don’t know how to specify this condition on a macro.

No problem. I do think there are ways to more precisely automate the things you describe, but we need more details before we can be of more help. For example, by text files, do you mean plain .txt files? Because I’m not aware of any sort of box format for plain text. Can you share an actual file or pair of files that you want to compare, or if the data is sensitive, a file/pair of files in the same format? Basically, the more precise information you can share about the data you’re working with and exactly what you want to accomplish, the more help we can offer.

This is transferring from text files (by clipboard) to a company website pages I’m doing data transfer work for. The website has titled form fields to input the information. I’ve already set up a macro to transfer but because of the way the text files are the macro sometimes incorrectly inputs the incorrect identical information into a pair of form fields at a time.

Each pair of form fields with identical text may have different text but they always need to be changed to the exact same text, and I can’t do it on the text file side, it must be done after transfer to the form fields (or in between in the clipboard or some such). Not all identical text form fields do need to be changed (some may be identical but not paired and that’s okay) but if particular pairs are identical they do need to be (and I’m saying paired but I just mean two particular form fields each time, and not always the same two, though it will always be one of the same four form field pairs).

For some reason I cannot wrap my mind around what variables are exactly and can’t find any good beginners explanation of them. If you want to give me any examples, you can just use temporary form field titles and I can replace them with the necessary titles (the titles are usually three or four words each). So far the closest I’ve got is thinking I could specify a macro to select a particular form field and inspect, then select another and inspect and if they’re identical change both their texts. I can’t figure this out though since I don’t understand variables but think that must be the only way. I’m trying to understand your example but am just not getting it exactly (why set variables to ‘identical’ or ‘no longer indentical’? how would that make the macro understand what to do? why display text in a separate window? how do i name variables so it knows which I need to find? etc.)

With the names transfer, it’s similar in that the names are going from text files to form fields (though these aren’t related to any of the identical ones) and I need to erase middle initials. Again, I can’t do it on the text file side so it must be done in transition by clipboard or in the form field. I’m thinking maybe there’s something where after the name is pasted to the form field I can have a macro somehow select the text and delete any single letter or single letter with a period after with, and with a space both before and after (to prevent single letter/initial first or last names from being deleted).

I think I've (sort of) figured out the name part. It would be very lengthy but I think it will work. Here's the macro (the clipboard contains " h " and the move and click is to close the Find function:

48 PM

Of course, I'd have to have 52(!) of these if/thens one right after another in the same macro (26 with individual letters and 26 with letters with periods after) which I hope won't slow down the macro too much but I'm going to give it a try. I'm not sure if there's a way to put all letters in one if/then box since each one would have to hunt and delete a different letter, or if there's a way to instead of putting 26 different letters just put something to represent any letter in one spot so like space-anyoneletter-space or space-anyoneletter-period-space that might make it much quicker.

I’ve figured out an inelegant way to compare the two texts too (I think it would be much more elegant with variables but I can’t seem to figure out what they are and do and how to use them).

I made two macros, the second being just an if/then. The first macro copies the first text, switches to KM, clicks on the form field of the if/then macro (I have KM open to that if/then with it in the same spot so the click always hits it) and paste, then switches back to the webpage and copies the second text and then instructs the second macro to start.

The second macro (the if/then, now with the first form field text copied to it) is instructed to compare the clipboard to the if/then form field and if they’re matches to go back to the webpage and change both texts to something else, and if they’re not matches to do nothing.

I appreciate that you took the time to describe your task so thoroughly, but I'm afraid a description alone is still lacking in key details that someone who isn't already familiar with your task would need in order to understand how to help. Based on your description I think it is fair to say that there are definitely more elegant and precise ways of accomplishing your task, but without detailed examples (meaning screenshots and, ideally, an actual data sample) of things like

•the text files in question
•the kind of data that needs to be compared
•the web form it's being entered into
•what non-identical data needs to be changed to
•your exact current procedure for doing all this

There simply isn't much in the way of practical help we can offer. If you can't share actual data due to confidentiality reasons that's fine, but in that case, it would be much easier for us to understand and help if you use fake data that is in the exact same format as the kind you're working with (for example, replacing a real person's name with Jane Smith).

Regarding your clipboard " h " example, there is a way to put something that represents any letter in one spot, but again, without a more detailed understanding of your task, data, and workflow, I can't suggest a practical way to use it (for example, even looking at the screenshot of your current "if the clipboard contains " h " macro, I can't tell what app you're using it in or exactly what it is you're trying to accomplish).

Regarding your comparison method, you shouldn't need to manipulate the KM editor to make a macro like this work at all. I'm afraid I don't have the time to try and explain how variables work at the moment, but I can tell you how you could use a variable in this use-case: after copying the first text, instead of switching back to KM, have the macro set the newly copied text to a variable, then after copying the second text, compare the clipboard to the variable that contains the first text. It could work something like this:

Compare Clipboard to Variable.kmmacros (8.2 KB)
06 AM

Thank you so much.

It’s been a few days and I’ve made some leaps and bounds. Using your help and diving into variables until I finally understood, I now have it and your macro suggestions make sense to me now.

In fact, I’m now somewhat far past that. I thought about starting a new thread but I’ll post my next question here again. I dove headlong into not only variables but also regular expressions which like variables I’ve never worked with before. I basically did a crash course in learning the basics.

Now I’m trying to write a regular expression to extract numbers from text files, or to be more specific non-editable but copyable pdf files. I’m planning to use the Search Using Regular Expression macro.

The number I need to extract is a one to four digit number, always changing, but it will always be surrounded (sometimes in front, sometimes behind) by certain variable text. There may be multiple different instances in a text, but I only (and specifically) need the first instance each time.

Here’s an example of four possibilities (I actually have closer to 20 to fill in, about half in front of the number and half behind): texta ([1-9][0-9]{0,4})|textb ([1-9][0-9]{0,4})|([1-9][0-9]{0,4}) textc|([1-9][0-9]{0,4}) textd

I was planning on using the pipe (vertical slash) symbol between all the various possibilities and write them out each individually, but as I started that KM was assigning each parentheses grouping a different variable, and even though only one will result in a number during a calculation it seems I can’t assign them all the same variable since unless the number is found with the last-written possibility the variable would be filled with nothing. What I need is a regular expression with the numbers section ([1-9][0-9]{0,4}) only grouped once (or at least as few as possible) to result in one (or just a few) possible variable(s) for them.

I’m way over my head here from where I was a week ago and even if I figure the regex out by myself and it seems to work, I’m worried I might mess some part of the expression up and not realise it until much later consequently ruining a lot of work.

My current thought is this: (texta |textb )([1-9][0-9]{0,4})|([1-9][0-9]{0,4})( textc| textd)

So it’s two possible variable fields for the numbers, but I can’t think of how I can make just one because then it seems like I’d have to make both the texts in front and behind the numbers optional since they can’t both be mandatory, which then means it could just find any number at all without the appropriate text with it and take it. I’m not sure if it’s possible to make an alternation expression with one alternate in front of and the other alternate behind a non-alternate section (in this case the numbers) but where one of the alternates must be found, with the non-alternate section only represented in the equation once. Anyway, if I haven’t messed anything up I think my current thought expression would search the correct possibilities and create two separate numbers-only variable fields in KM and while it’s not just one it’s better than 20. Does this seem correct and is it the best way?

You're very welcome. I'm truly glad to hear that you've made so much progress.

I'm sorry that I don't have time to offer more of an explanation right now (or to have responded to this sooner) but I think I figured out one possible solution to your question:

Find Number.kmmacros (3.7 KB)
35 PM

The regular expression I used to search for the number was this:

(?:(?<=texta |textb )[1-9]\d{0,3})|(?:[1-9]\d{0,3}(?= textc| textd))

You can find a breakdown of it here: https://regex101.com/r/i0EAGR/1

I tried the Search and Replace using Regex action first, but it ended up leaving the Result variable blank and not working the way I expected, so I used the For Each action to search for the regex instead and the Break from Loop action to cancel the For Each once it found a result that matched. Again, I'm sorry I don't have time to explain it better at the moment, but hopefully this will help you get closer to your goal. Good luck!