J
joeu2004
Jennifer Murphy said:I just want a simple macro that only I will use.
What you asked for is hardly "simple".
See the macro implemented in the Excel file "copy to textfile.xls" at
http://www.box.net/shared/tgi09xdin8nr77jb36bd.
I believe it does what you want, namely:
1. You select the first row of data. Then press alt+F8 to execute the
macro.
2. Presumptions:
a. The first column contains data (e.g. text).
b. The last column contains integers.
c. Interstitial columns are ignored.
d. The data are in contiguous rows.
3. The macro prompts for the text file name. If it already exists, you can
elect to overwrite it, select another file name, or cancel.
4. The macro copies the two columns to a new temporary worksheet. So the
original data is not altered at all, as requested.
5. The data is sorted by the first column.
6. The macro ignores rows in which the first column contains specified "bad
characters".
Note: I chose to implement a VBA function to check for "bad characters",
calling InStr successively. This might prove to be too time-consuming for
large data (e.g. 500,000 rows).
Arguably, it would be better to use VBScript Regular Expression
operations. I chose not to because: (a) I am not sure we can rely on it;
(b) I don't know if VB regexpr is indeed faster; and (c) it would add
significant complexity to automate setting up the VBScript Reg Expr
"reference" (i.e. Tools > References).
But if that is something that you require, it can be done.
7. The macro sums the integers in the second column for like data in the
first column.
Note: "like data" means "exactly equal", as requested. The comparison
is case-sensitive; for example, "aBc" is considered not "exactly equal" to
"AbC".
Caveat: the text "1234" is not considered "exactly equal" to the number
1234 in the first column. Presumably, this is not a problem because you
said that the first column is expected to contain only text.
8. The macro verifies that the second column contains positive integers
(i.e. >0).
9. If there are no errors, the summary data is written to the text file,
separating the two columns with one tab character.