[英]Macro to copy variable number of rows with fixed columns
I have searched high and low on the internet to find an answer to my macro problem-I don't think what I am trying to so is that difficult, but maybe it is! 我在互联网上搜寻高低寻找我的宏观问题的答案-我认为我要尝试的不是那么困难,但也许是这样!
In my dataset column K contains my data markers. 在我的数据集中,列K包含我的数据标记。 What I want to do is find "22" in column K and find "23" is column K and copy all the rows of data in columns C to J that fall between the row numbers indicated by the markers contained in column K. The number of rows between markers 22 and 23 is different for each spreadsheet, which is where I am running into problems.
我想做的是在K列中找到“ 22”,在K列中找到“ 23”,然后将C到J列中的所有数据行复制到属于K列包含的标记所指示的行号之间。每个电子表格在标记22和23之间的行数是不同的,这就是我遇到问题的地方。 I've tried to set the range for these variables but I am basically a macro novice and am not having much luck.
我试图为这些变量设置范围,但我基本上是个宏新手,运气不高。 Any help greatly appreciated.
任何帮助,不胜感激。
Probably the easiest way would just find 22 & 23 and establish where the rows are located 可能最简单的方法是找到22和23并确定行的位置
Then you can create the range to copy with those known rows and columns. 然后,您可以创建范围以使用那些已知的行和列进行复制。 Name a sheet "Copy to Sheet" for this example.
在此示例中,将工作表命名为“复制到工作表”。
Sub Button1_Click()
Dim Rws As Long, Rng As Range
Dim c1 As String, c2 As String
Dim FndC1 As Range, FndC2 As Range
Dim c1C As Integer, c2C As Integer
Dim ws As Worksheet
Set ws = Sheets("Copy to Sheet")
c1 = 22
c2 = 23
Rws = Cells(Rows.Count, "K").End(xlUp).Row
Set Rng = Range(Cells(1, "K"), Cells(Rws, "K"))
Set FndC1 = Rng.Find(what:=c1, lookat:=xlWhole)
Set FndC2 = Rng.Find(what:=c2, lookat:=xlWhole)
If Not FndC1 Is Nothing Then
c1C = FndC1.Row
Else: MsgBox c1 & " Not Found"
Exit Sub
End If
If Not FndC2 Is Nothing Then
c2C = FndC2.Row
Else: MsgBox c2 & " Not Found"
Exit Sub
End If
Range(Cells(c1C + 1, "C"), Cells(c2C - 1, "J")).Copy _
ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.