1
我試圖根據列D中的數據將Sheet'All'的整行復制到另一個工作表.D列(Homework/Advanced/Beginner)中有多個值,這些行需要複製有相應的名字。 (家庭作業到家庭作業頁)如果條件滿足,將excel中的行復制到表中。
工作表'全部'中的數據將被添加到並且新數據需要被複制而不復制已經存在的數據。
我試圖根據列D中的數據將Sheet'All'的整行復制到另一個工作表.D列(Homework/Advanced/Beginner)中有多個值,這些行需要複製有相應的名字。 (家庭作業到家庭作業頁)如果條件滿足,將excel中的行復制到表中。
工作表'全部'中的數據將被添加到並且新數據需要被複制而不復制已經存在的數據。
這不是一個大問題。最好的辦法是保持簡單,並在「全部」變化時複製所有內容。我有一個「重新分配」按鈕上的「全」片,並有事件調用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
該代碼相當靈活;它會找到整個源塊,因此,只要列「D」包含工作表鍵並且數據以A1(無標題)開始,那麼無論您擁有多少列都無關緊要。如果您有標題,請將所有A1引用更改爲A2。
其他牀單(「家庭作業」等)必須已經創建。 - 並且您需要爲Microsoft Scripting Runtime設置參考。
代碼中唯一「有趣」的部分是計算出目標範圍(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]
'' e.g. "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
謝謝馬克,我得到了'編譯錯誤:用戶定義類型未定義'指向'貓作爲字典'我做錯了什麼? – 2010-09-24 14:19:58
您需要Microsoft Scripting Runtime,Dictionary對象所在的位置。在VBA窗口中,單擊工具|參考並向下滾動以選擇Microsoft腳本運行時。字典是非常有用的,儘管我在這裏做了相對簡單的使用。這是你應該熟悉的工具之一。 – 2010-09-24 14:53:42
謝謝馬克,現在可以工作 - 如果我真的閱讀說明會有所幫助 - 對不起 – 2010-09-24 15:21:53