[英]Copy a row from Sheet3, paste into Sheet1, save as file, then go to the next row of Sheet 3
I am trying to find a way to streamline a very repetitive task. 我正在尝试找到一种简化重复性工作的方法。
This is the first time I am trying to build a proper macro, so things are confusing for me. 这是我第一次尝试构建适当的宏,因此事情对我来说很混乱。
Below is an attempt to make it work. 以下是尝试使其工作。
Sub test()
Dim r As Range, j As Integer
Set r = Range("A2:C500")
Do
Sheets("Sheet1").Range(r.Offset(1, 0)).Select
Selection.Copy
Sheets("Sheet1").Select
Range("D2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("D2:F494")
Range("D2:F494").Select
ActiveWorkbook.SaveAs Filename = j, FileFormat:=xlUnicodeText, _
CreateBackup:=False
If r.Offset(1, 0) = "" Then Exit Do
Loop
End Sub
I am trying to 我在尝试着
- copy Row A2:C2 from Sheet 3 -从工作表3复制行A2:C2
- paste it into D2 of Sheet 1 -将其粘贴到工作表1的D2中
- drag that value all the way down to the end of Sheet 1 -将该值一直拖动到工作表1的末尾
- Save Sheet 1 as text file (any file name is fine. I was trying to save as 1, 2, 3, and so on.) -将工作表1保存为文本文件(任何文件名都可以。我试图另存为1、2、3,依此类推。)
- Then go to the next row of Sheet 3 (A3:C3) and repeat the process until it reaches the last row, A500:C500. -然后转到工作表3(A3:C3)的下一行,重复该过程,直到到达最后一行A500:C500。
When I recorded the macro for just the first row, it looked as follows: 当我只为第一行记录宏时,它看起来如下:
Sheets("Sheet3").Select
Range("A2:C2").Select
Selection.Copy
Sheets("Sheet1").Select
Range("D2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("D2:F494")
Range("D2:F494").Select
ActiveWorkbook.SaveAs Filename:= _
"D:\Users\XXX\Desktop\XXX. XX\1.txt", FileFormat:=xlUnicodeText, _
CreateBackup:=False
ANY help would be greatly appreciated! 任何帮助将不胜感激!
It's not clear what you want to do. 目前尚不清楚您想做什么。 Sounds like you are saying you want to copy a row (or some cells in a row) from one sheet to a particular single cell on another sheet and then export that sheet to a text file.
听起来您是在说要从一个工作表复制一行(或行中的某些单元格)到另一工作表的特定单个单元格,然后将该工作表导出到文本文件。 This seems strange.
这似乎很奇怪。 Why not just write a macro to get all the data you want and directly write it to file in the first place?
为什么不只是编写一个宏来获取所需的所有数据,然后直接将其直接写入文件呢? Do you just want all the data from all rows of the first three columns of "sheet3" exported to a file?
您是否只希望将“ sheet3”的前三列的所有行中的所有数据导出到文件中? Or is there boilerplate stuff in the other parts of "sheet1" that you want to be in every on of these text files?
还是您希望在这些文本文件的每个文件中的“ sheet1”其他部分中都有样板文件?
Or do you want to create 500 text files, each with a row from sheet3? 还是要创建500个文本文件,每个文本文件都来自sheet3?
You are saving the ActiveWorkbook
so it won't save the sheets as individual files. 您正在保存
ActiveWorkbook
因此不会将工作表另存为单个文件。 You need to create a new workbook for each set of data using Workbooks.Add
, then save and close each of these new workbooks once you've copied the data. 您需要使用
Workbooks.Add
为每组数据创建一个新工作Workbooks.Add
,然后在复制数据后保存并关闭这些新工作簿。
(A Worksheet has a SaveAs method, but it doesn't work - it doesn't save an individual sheet from a workbook.) (工作表具有SaveAs方法,但不起作用-不会从工作簿中保存单个工作表。)
You can fill the entire area using Paste or PasteSpecial
without having to fill it down: 您可以使用Paste或
PasteSpecial
填充整个区域,而不必将其填满:
Worksheets("Sheet3").Range("A2:C2").Copy
Worksheets("Sheet1").Range("D2:F494").PasteSpecial xlValues
Application.CutCopyMode = False
I'm afraid the recorder will only get you so far, and you'll need to study and modify the code that it creates. 恐怕录音机只会帮助您到达目标,您需要研究和修改其创建的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.