I regularly copy values spread across several different Excel spreadsheet cells e.g. like this:
Île d'Ouessant Finistère France
I want to combine values from several different cells and separate them with commas. So above would become:
Île d'Ouessant, Finistère, France
I thought this would be as simple as replacing tabs (\t) with a comma. But instead, attached macro seems to be replacing tabs with new lines. How can I replace tabs with commas?
You made that conclusion based on what? Did you use the debugger or the value inspector to check the new value before you paste it? I tested your macro and it is correctly inserting commas, not newlines. You could test this yourself by using either the debugger or the value inspector.
If your target app is interpreting the commas as newlines, that could explain your conclusion. I can't test this because I don't have Excel.
Try a different app, instead of Excel. Try Notes, and see if it shows commas or newlines.
It's an Excel thing -- copying a tab-separated string from eg TextEdit works as you want but copying multiple cells in Excel and running your macro will paste a table in TextEdit but a series of lines in Script Editor.
You could work your way through the various clipboard flavours to find the problem, but a shortcut is to Search and Replace into a variable to force plain text:
According to this post, Excel values aren't separated by tabs. So I tried using \n instead and it adds commas between the cell values, but it also adds a comma after the last value too. I'm sure there's a way to fix that though.
In that post they were dealing with a column of values -- \n separated.
OP seems to be dealing with a row of values -- \t separated.
This used to work, and values are still tab-separated if you paste into eg Script Editor or BBEdit. But MS recently added to the Clipboard flavours and now a simple S'n'R on tabs doesn't work, you have to either remove the "broken" flavour (beyond me in a quick try) or force to plain text by putting the Clipboard data into a KM variable then S'n'Ring the variable.