Excel VBA - 复制多个范围并按选择顺序粘贴它们而不会覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19980681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - Copying multiple ranges and pasting them in order of selection without overwriting
提问by DanK
So I have no VBA background, but I have managed to put this together by scouring the internet. It is for a workbook where the ultimate goal is to have users (Engineers) select certain checkboxes. When they are done (And there will be a lot of checkboxes), they click the button "Build" and depending on their selections, they will generate a new worksheet or new workbook (Haven't decided on that one yet) with all the selected information pasted into it, in order.
所以我没有 VBA 背景,但我已经设法通过搜索互联网把它放在一起。它适用于最终目标是让用户(工程师)选择某些复选框的工作簿。完成后(并且会有很多复选框),他们单击“构建”按钮,根据他们的选择,他们将生成一个新的工作表或新的工作簿(尚未决定),其中包含所有选定的信息按顺序粘贴到其中。
Here is the code as it stands:
这是它的代码:
Sub Merge()
If Range("F3") = True Then
Sheets("Data").Select
Range("A1:A8").Select
Selection.Copy
Sheets("UI").Select
Cells(1, 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Else
MsgBox "Part 1 - Failed."
End If
If Range("F6") = True Then
Sheets("Data").Select
Range("A10:A17").Select
Selection.Copy
Sheets("UI").Select
Cells(10, 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Else
MsgBox "Part 2 - Failed"
End If
If Range("F9") = True Then
Sheets("Data").Select
Range("A19:A26").Select
Selection.Copy
Sheets("UI").Select
Cells(19, 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Else
MsgBox "Part 3 - Failed"
End If
End Sub
and here is the file in my google drive if that makes things easier for you: https://drive.google.com/file/d/0Bza853yM-QSMSlFWbElBc1dsbGM/edit?usp=sharing
如果这让您更轻松,这里是我的谷歌驱动器中的文件:https: //drive.google.com/file/d/0Bza853yM-QSMSlFWbElBc1dsbGM/edit?usp=sharing
Currently, it is pasting the ranges that are "True" into pre-defined cell ranges. However, what i would like is if Red = True and Green = True, that red is pasted into A1:A8, and Green would be pasted into A10:A18. Similarly, if Blue and Green are True, or just Blue or Green are true, they paste into A1:A8 and the next "True" statement pastes under them. This way you could have any number of True statements and they will paste in order, with only 1 row spaced between them instead of 8 or 20 rows.
目前,它正在将“真”的范围粘贴到预定义的单元格范围中。但是,我想要的是,如果 Red = True 和 Green = True,则该红色将粘贴到 A1:A8 中,而绿色将粘贴到 A10:A18 中。类似地,如果蓝色和绿色为真,或者只有蓝色或绿色为真,则将它们粘贴到 A1:A8 中,然后将下一个“真”语句粘贴到它们下方。这样你就可以有任意数量的 True 语句,它们会按顺序粘贴,它们之间只有 1 行而不是 8 或 20 行。
I looked over this website and others but didn't find anything. So i'm not sure if this is possible or that i'm too ignorant on the subject to search for the words of the concept i'm looking for.
我查看了这个网站和其他网站,但没有找到任何东西。所以我不确定这是否可能,或者我对这个主题太无知而无法搜索我正在寻找的概念的词。
Thanks for your wisdom! -Dan
谢谢你的智慧!-担
采纳答案by APrough
Instead of pasting your copies into a static Range, you could find the next blank cell on your "UI" sheet, and paste the next set of results to that.
您可以在“UI”工作表上找到下一个空白单元格,然后将下一组结果粘贴到该单元格中,而不是将您的副本粘贴到静态范围中。
Code to find next blank cell...
查找下一个空白单元格的代码...
Sheets("UI").Select
Range("A2").Select
Selection.End(xlDown).Select
Selection.Offset(1, 0).Select