使用 VBA,如何选择行范围中的所有其他单元格(要垂直复制和粘贴)?
我有一个 2200 多页的文本文件。它是由客户通过数据交换传递给我们的,用星号分隔值,用波形符 (~) 表示行的结尾。该文件以 Word 文本文件形式发送给我。大多数行被分成两部分(一行覆盖整行和第二行的一部分)。我一次将其片段(10 个页面块)传输到 Excel 中,不幸的是,行末尾出现的任何零都会在“文本到列”过程中被丢弃。因此,我会仔细观察每个“长”行,以确保零没有丢失,并手动重新输入任何零。
以下是一小部分示例数据:
SDQ EA 92 1551 378 1601 151 1603 157 1604 83
“SDQ、EA 和 92”是无关的(数据传输的伪影)。我想使用 Excel 和/或 VBA 选择 1551、1601、1603 和 1604(这些是商店编号),以便我可以复制这些值,然后垂直转置粘贴它们。然后我将返回并复制 378、151、157 和 83(销售值),以便我可以将它们转置粘贴到商店编号旁边。接下来的两行数据包含相同的商店编号,但给出了相应的美元值。我只需要复制美元值,以便可以将它们垂直转置粘贴到单位值旁边(例如 378、151、157 和 83)。
只需将光标放在该行中感兴趣的第一个单元格上,然后运行宏来复制所有其他单元格,就会极大地加快我的工作速度。我尝试使用 ActiveCell
和 Offset
引用来选择要复制的范围,但没有成功。有没有人给我一些建议?预先感谢您的帮助。
I have a 2200+ page text file. It is delivered from a customer through a data exchange to us with asterisks to separate values and tildes (~) to denote the end of a row. The file is sent to me as a text file in Word. Most rows are split in two (1 row covers a full line and part of a second line). I transfer segments (10 page chunks) of it at a time into Excel where, unfortunately, any zeroes that occur at the end of a row get discarded in the "text to columns" procedure. So, I eyeball every "long" row to insure that zeroes were not lost and manually re-enter any that were.
Here is a small bit of sample data:
SDQ EA 92 1551 378 1601 151 1603 157 1604 83
The "SDQ, EA, and 92" are irrelevant (artifacts of data transmission). I want to use Excel and/or VBA to select 1551, 1601, 1603, and 1604 (these are store numbers) so that I can copy those values, and transpose paste them vertically. I will then go back and copy 378, 151, 157, and 83 (sales values) so that I can transpose paste them next to the store numbers. The next two rows of data contain the same store numbers but give the corresponding dollar values. I will only need to copy the dollar values so they can be transpose pasted vertically next to unit values (e.g. 378, 151, 157, and 83).
Just being able to put my cursor on the first cell of interest in the row and run a macro to copy every other cell would speed up my work tremendously. I have tried using ActiveCell
and Offset
references to select a range to copy, but have not been successful. Does any have any suggestions for me? Thanks in advance for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有有关该文件的更多信息,很难给出完整的答案。
我认为如果您的输入数据长度超过 2200 页,那么使用默认的 Excel 打开功能打开它不太可能是正确的方法。特别是因为 Excel 具有最大行数和列数。如果文件是文本文件 (.txt),我建议使用 VBA 打开它并读取每一行,一次一行,然后处理数据。
这是一个帮助您入门的示例。请记住,这会将每行文本转换为数据列,因此您将在运行 2200 页文本之前快速填充 Excel 的所有列。但这只是一个例子。
It's hard to give a complete answer without more information about the file.
I think if your input data is 2200+ pages long, it's unlikely that opening it with the default excel opening functions is the way to go. Especially since Excel has maximum number of rows and columns. If the file is a text file (.txt) I would suggest opening it with VBA and reading each line, one at a time, and processing the data.
Here's an example to get you started. Just keep in mind that this is transposing each row of text into columns of data, so you will quickly fill all the columns of excel long before you run thru 2200 pages of text. But it's just an example.