[英]Copy and paste the row a N number of times (N based on a value in the cell)
[英]Copy/Paste n Times Based on Other Cell Values
我已經碰壁了。 盡管在SO帖子像這一個 ,這是非常相似的或這一個上Kioskea,我只是無法連接在我的腦海點基於這樣是使這個工作需要一個公式結果過濾單元和復制之間。 這是數據表-簡化-我正在使用:
A B C D E F G H
R1 Name Num Status #Orig #InPro #Act #Rem #RemStatus
R2 ABC 032 Complete 22 0 11 11 Purged
R3 LMN 035 In Prog 25 21 4 21 Pending Scan
R4 XYZ 039 Not Act 16 16 0 16 Not Active
這描述了紙質文件箱的狀態及其處理方式:
G和H列根據狀態可以具有三種含義:
我的代碼(如下所示)應該執行的操作是遍歷范圍(A2:H61)中的每一行。 如果狀態為“不活動”,則可以忽略該行,然后移至下一行。 如果狀態為“進行中”或“已完成”,則宏在“讀取”的任何行中都需要復制單元格A,B和H,並將其(列)“ G”粘貼到另一個工作表中的次數-在同一工作簿中-從下一個可用行開始。 深呼吸
我知道。 這也傷了我的大腦。 這是我到目前為止的代碼:
Sub TEST_Copy_Process()
Dim srcrange As Range
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set wb = ActiveWorkbook
Set ws1 = Worksheets("SIS Agregate")
Set ws2 = Worksheets("Center Detail")
Set srcrange = Range(wb.ws2.Cells("A2:H61"))
For Each Row In srcrange.Rows
If Row = "Not Active" And Row.Offset(0, 3) = SectorType Then
Continue
ElseIf Row = "In Progress" And Row.Offset(0, 3) = SectorType Then
ElseIf Row = "Complete" And Row.Offset(0, 3) = SectorType Then
End If
Set LastCell = wb.ws1.Cells(wb.ws1.Rows.Count, "A").End(xlUp)
LastCellRowNumber = LastCell.Row + 1
Next Row
End Sub
一旦我得到了實際上正在做艱苦工作的代碼,我就不知道如何挑選出最好的代碼。 如上所述,像帖子這已經helpd讓我在這里。 我慢慢地開始理解我在Excel先生身上發現的東西。 這個人似乎正在執行If / Then工作,但我不知道它是如何復制或粘貼的。
我感謝所有幫助。 即使您可以向我指出可以稍微解釋一下的資源(除了Amazon上的書:]),也將是很大的幫助!
讓我們看看這是否能使您走上正確的道路。 對於不太了解的人來說,您的代碼看起來很好,所以也許您是一個快速的研究:)
我很困惑為什么您要使用.Offset(0, 3)
(在您的解釋中似乎沒有提到),以及為什么要與SectorType
(在您提供的代碼中是未定義的變量)進行比較。 我將假設這些都是不必要的,並且會無意間從其他示例中復制 (請讓我知道是否弄錯了)。
我沒有測試過,但我會更改此作業:
Set srcrange = Range(wb.ws2.Cells("A2:H61"))
對此,如果沒有其他原因,那就更直接了。 我還將這個范圍更改為僅引用H列,因為這是您的邏輯居中的一列(注意:我們始終可以使用Offset
和/或Resize
方法訪問其他單元格)。
Set srcrange = wb.ws2.Range("H2:H61")
邏輯的重點在此塊中,請注意刪除Row.Offset(9, 3) = SectorType
。 我還將使用Select Case
代替If/Then
。 當發現要測試的條件不止一個或兩個以上時,我發現這些內容更易於閱讀/理解:
For Each Row In srcrange.Cells '## In this case, Cells/Rows is the same, but I use Cells as I find it less ambiguous
Select Case Row.Value
Case "Not Active"
'## If the status is Not Active, Column G and H match it, and nothing needs to be done
'Do nothing
Case "In Progress", "Complete"
'## If the status is In Progress or Complete, ... copy cells A, B, and H _
' and paste it (column)"G" number of times in another worksheet - _
' within the same workbook - starting in the next available row.
'# Get the next empty cell in column A of the ws1
' I modified this to use Offset(1, 0), to return the cell BENEATH
' the last cell.
Set LastCell = wb.ws1.Cells(wb.ws1.Rows.Count, "A").End(xlUp).Offset(1)
'## copy the values from columns A, B, H to ws1
'## Column A goes in column A
LastCell.Value = Row.Offset(0, -7).Value
'## Column B goes in column B
LastCell.Offset(0, 1).Value = Row.Offset(0, -6).Value
'## Column H goes in column C (because you did not specify)
LastCell.Offset(0, 2).Value = Row.Value
End Select
Next Row
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.