如果条件满足,将excel中的行复制到表中。

我试图根据D列中的数据将Sheet'All'的整行复制到另一个Sheet中.D列(Homework / Advanced / Beginner)中有多个值,这些行需要被复制到相应的名字。 (家庭作业表)。

“全部”工作表中的数据将被添加到新的数据,而不需要复制那些已经存在的数据。

这不是一个大问题。 最好的办法是保持简单,只要“全部”变化就复制一切。 我会有一个“重新分配”button“全部”表,并有事件调用scatterRows()

你不说你的源代码表是什么样的,所以我做了一些表格“全部”:

9 0.181626294 carrot beginner Irene 5 0.221180184 beans advanced Eva 8 0.221813735 turnip advanced Harry 10 0.314800867 lettuce homework John 4 0.360163255 peas homework Doug 11 0.379956592 pepper advanced Karen 3 0.44415906 tomato beginner Charlie 6 0.647446239 corn beginner Frank 2 0.655706735 potato advanced Bob 7 0.666002258 lentils homework George 1 0.768524361 squash homework Alice 

代码相当灵活; 它会find整个源代码块,所以只要列“D”包含工作表键,并且数据以A1(无标题)开始,那么无论您拥有多less列都无关紧要。 如果您有标题,请将所有A1引用更改为A2。

其他表(“家庭作业”等)必须已经创build。 – 并且您需要为Microsoft脚本运行时设置参考。

代码中唯一“有趣”的部分是找出目标范围的string(putString)。

 Option Explicit '' Copy rows from the "all" sheet to other sheets '' keying the sheetname from column D. '' **** Needs Tools|References|Microsoft Scripting Runtime '' Changes: '' [1] fixed the putString calculation. '' [2] Added logic to clear the target sheets. Sub scatterRows() Dim srcRange As Range Dim srcRow As Range Dim srcCols As Integer Dim srcCat As String Dim putRow As Integer Dim putString As String Dim s ''*New [2] '' Current row for each category Dim cats As Dictionary Set cats = New Dictionary cats.Add "homework", 0 cats.Add "beginner", 0 cats.Add "advanced", 0 '' Clear the category sheets *New [2] For Each s In cats.Keys Range(s & "!A1").CurrentRegion.Delete Next s '' Find the source range Set srcRange = [all!a1].CurrentRegion srcCols = srcRange.Columns.Count '' Move rows from source Loop For Each srcRow In srcRange.Rows '' get the category srcCat = srcRow.Cells(4).Value '' get the target sheet row and increment it putRow = cats(srcCat) + 1 cats(srcCat) = putRow '' format the target range string *Fixed [1] '' eg "homework!A3:E3" putString = srcCat & "!" & _ [a1].Offset(putRow - 1, 0).Address & _ ":" & [a1].Offset(putRow - 1, srcCols - 1).Address '' copy from sheet all to target sheet Range(putString).Value = srcRow.Value Next srcRow End Sub