[英]VBA, copy and paste lots of rows in another worksheet with a criteria
Well, I'm a newbie in VBA so I'm asking for some help. 好吧,我是VBA的新手,所以我需要一些帮助。 So I have a file from an experiment with a lot of data. 因此,我有一个实验文件,其中包含大量数据。 Basically, I have 21 steps of measurements and for each step N points are recorded. 基本上,我有21个测量步骤,每步记录N点。 Every steps are on one worksheet and I can spot them with the word "step". 每个步骤都在一个工作表上,我可以在“步骤”一词中找到它们。 For each step I want to copy the N points and send them to another sheet, I wrote a macro but nothing happen except creating a new sheet. 对于每个步骤,我要复制N个点并将其发送到另一张纸上,我编写了一个宏,但是除了创建新纸之外什么都没有发生。 You can have a look on my code below: 您可以在下面查看我的代码:
Sub mymacro() Worksheets("laos").Activate Dim n As Long Dim i As Byte Dim LastRow As Long Dim WS As Worksheet Set WS = Sheets.Add With Worksheets("laos") LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With For n = 1 To LastRow With Worksheets("laos") If .Cells(n, 1) = " step " Then For i = 1 To 9763 'N points recorded .Rows(i).Copy Destination:=WS.Range("A" & i) Next End If End With Next End Sub
This should do what you are looking to do if I understood your original post, which I probably didn't. 如果我了解您的原始帖子,而我可能不知道,那么这应该可以满足您的期望。 More details about the layout of your sheets would help. 有关工作表布局的更多详细信息将有所帮助。 This concept is probably enough though to teach you what you are seeking. 这个概念可能足以教会您所要寻找的东西。 Look at the screenshots provided to understand the layout. 查看提供的屏幕截图以了解布局。
In this example, you already have created the target sheet. 在此示例中,您已经创建了目标表。 You just need to make header rows, and name the sheet, then when you declare the variable newSheet = "WHATEVER YOU NAMED THAT SHEET" in this example, it's "TargetSheet" 您只需要制作标题行并命名工作表,然后在此示例中声明变量newSheet =“无论您命名了什么”,它就是“ TargetSheet”
Sub mymacro()
'Declare the counters as Integers, not byte
Dim oRow As Integer
Dim i As Integer
Dim LastRow As Integer
Dim LastNewRow As Integer
Dim newSheet As String
Dim nRow As String
Dim n As Integer
Dim LastNCol As Integer
Dim searchString As String
'Not Sure what kind of value the N points are.
Dim Value As String
'The original sheet is Sheets("laos"
'If you don't need to create the new Worksheet every time, just declare it.
newSheet = "TargetSheet"
'set LastRow using by specifying the sheet and range to search.
LastRow = Sheets("laos").Range("A65536").End(xlUp).Row
LastNewRow = Sheets(newSheet).Range("A65536").End(xlUp).Row
'Sets the destination Row on the newSheet
nRow = 2
'oRow = Original Row.
For oRow = 2 To LastRow
'This is looking to Row# N:, Column A for the value = "step" contained in the text
'If the InStr (InString) function returns a value greater than 0 meaning "step" occurs
searchString = Sheets("laos").Cells(oRow, 1)
If InStr(searchString, "step") > 0 Then
'Assuming you have to loop through N points, Find the last column of the STEP row.
LastNCol = Sheets("laos").Cells(oRow, Columns.Count).End(xlToLeft).Column
'Label the new Row
Sheets(newSheet).Cells(nRow, 1) = Sheets("laos").Cells(oRow, 1)
'start loop with 2 for ColumnB
For n = 2 To LastNCol
'Copy the VALUE from the original sheet, and set the new sheet with it.
Value = Sheets("laos").Cells(oRow, n)
Sheets(newSheet).Cells(nRow, n) = Value
Next n
'Since we are done copying all the N Points to the New Row, we increment the nRow + 1
nRow = nRow + 1
'Must END IF
End If
Next oRow
End Sub
Sorry guys, I wasn't very accurate and I don't have enough reputation to post a screenshot of my worksheet.So I got Sine Strain measure with N points recorded (in my code N=9763 but it can change), and there are 22 Sine Strain (or step). 抱歉,我不是很准确,我没有足够的声誉来发布我的工作表的屏幕快照。所以我得到记录了N点的Sine Strain量度(在我的代码N = 9763中,但它可以更改),是22正弦应变(或步进)。 Everything is on the same worksheet. 一切都在同一工作表上。 At the end, I want to have one sine strain per sheet (I don't care about the name of the different sheets). 最后,我希望每张纸具有一个正弦应变(我不在乎不同纸的名称)。
I hope It will be useful for you. 希望对您有用。
I'll try to do something with your code. 我将尝试对您的代码进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.