Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
842 views
in Technique[技术] by (71.8m points)

foreach - Copy and Paste Loop

I have two sheets: Sheet 1 and Sheet 2.

  • Sheet 1 only has one column (Column A) with multiple rows. Product 1, Product 2...etc.
  • Sheet 2 has multiple rows and columns.
  • The goal is for each cell in Sheet 1 (starting with A2), copy and paste all of Sheet 2 onto a new sheet, Sheet 3. Loop until a blank cell in Sheet 1.

So for example: Product 1 would appear x times in Column A with the respective rows in Sheet 2. Then Product 2 would appear x times underneath Product 1, with the same respective rows in Sheet 2.

Below is a very rough macro of what I am wanting to do.


Sub Copy_Paste_Loop()
'
' Copy_Paste_Loop Macro
'

'
    Range("B2:C2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    Range("B1").Select
    ActiveSheet.Paste
    Selection.Columns.AutoFit
    Sheets("Sheet2").Select
    Range("B3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet2").Select
    Range("B2:B3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet3").Select
    Range("A1").Select
    ActiveSheet.Paste
    Range("A2").Select
    Application.CutCopyMode = False
    Selection.AutoFill Destination:=Range("A2:A106")
    Range("A2:A106").Select
    Range("B2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.End(xlDown).Select
    Range("B107").Select
    ActiveSheet.Paste
    Sheets("Sheet2").Select
    Range("B4").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet3").Select
    ActiveWindow.SmallScroll Down:=9
    Range("A107").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.AutoFill Destination:=Range("A107:A211")
    Range("A107:A211").Select
End Sub

question from:https://stackoverflow.com/questions/65945326/copy-and-paste-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...