简体   繁体   English

从Sheet3复制一行,粘贴到Sheet1,另存为文件,然后转到Sheet 3的下一行

[英]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.

相关问题 将工作表1中的所有单元格从A + AC复制到工作表3,然后在工作表1中删除行 - Copy All cells from A + AC in sheet1 to Sheet3 then delete row in Sheet1 从 Sheet1 复制使用的范围并粘贴到 Sheet3 - Copy Used Range from Sheet1 and paste into Sheet3 如果单元格的颜色为绿色,则从 Sheet1 复制一行并将其粘贴到 Sheet 2 - Copy a row from Sheet1 and paste it into Sheet 2 if color of a cell is green 如何将第一行从 Sheet1 移动到 Sheet2,将 Sheet1 上的第二行移动到 Sheet3,将 Sheet1 上的第三行移动到 Sheet4 然后重复? - How to move first row from Sheet1 to Sheet2, move second row on Sheet1 to Sheet3, move third row on Sheet1 to Sheet4 then repeat? 从Sheet2查找名称并将行复制到Sheet3 - Lookup Name From Sheet2 and Copy Row to Sheet3 如果工作表sheet2上的单元格与工作表sheet1上的单元格匹配,则将行从工作表2复制到工作表1并循环到下一行 - If cell on sheet2 row1 matches cell on sheet1 then copy row from sheet 2 to sheet 1 and loop for next row 从Sheet1复制范围并将其粘贴到Sheet 2中 - Copy Range from Sheet1 And paste it in Sheet 2 VBA-比较Sheet1的值与Sheet2,将结果复制/粘贴到Sheet3 - VBA - Compare Sheet1 values to Sheet2, copy/paste the result to Sheet3 从单元格J中包含“是”的Sheet1复制行并将其粘贴到Excel 2007中的Sheet4 - Copy and Paste a Row from Sheet1 Containing “YES” in Cell J to Sheet4 in Excel 2007 将数据从“工作表1”归档到“工作表2”的下一个空白行 - Archive data from “sheet1” to next blank row of “sheet2”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM