![巨集建議 - 如果某些儲存格顯示“否”,則將行貼上到新工作表中](https://rvso.com/image/1502897/%E5%B7%A8%E9%9B%86%E5%BB%BA%E8%AD%B0%20-%20%E5%A6%82%E6%9E%9C%E6%9F%90%E4%BA%9B%E5%84%B2%E5%AD%98%E6%A0%BC%E9%A1%AF%E7%A4%BA%E2%80%9C%E5%90%A6%E2%80%9D%EF%BC%8C%E5%89%87%E5%B0%87%E8%A1%8C%E8%B2%BC%E4%B8%8A%E5%88%B0%E6%96%B0%E5%B7%A5%E4%BD%9C%E8%A1%A8%E4%B8%AD.png)
我想要一些使用 VBA 和巨集的建議。
我想要一種連結工作表的方法(工作紙 1 至 6)與主表(工作表 7)。
如果該行包含“不”在列中I
(整個工作紙 1 至 6),程式碼可以將該行複製並貼上到工作表 7?
那麼如果行(在工作紙 1 至 6)改為“是的” 另一個程式碼是否能夠從中刪除該行工作表 7?
對於某些上下文,工作紙 1 至 6是職位列表和'是的'&'不'如果客戶已付款。如果'不'他們被加到債務人名單中工作表 7。如果'是的'他們需要從債務人名單中刪除。
答案1
這段程式碼將幫助您:
Public Sub debtors()
Dim wkb As Workbook
Dim wks As Worksheet
Dim wksdest As Worksheet
Set wkb = ThisWorkbook
Set wksdest = wkb.Sheets("Sheet7")
wksdest.Rows.Clear 'Clear the contents of Sheet7
destRow = 1 'First row on Sheet7
For i = 1 To 6 'Loop through Sheets 1 to 6
newIndex = Right(Str(i), 1)
thisSheet = "Sheet" + newIndex
Set wks = wkb.Sheets(thisSheet)
wks.Activate
'Selects column I
Columns("I:I").Select
'Find a coincidence with the string "NO"
Set cell = Selection.Find(What:="NO", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'If there is a coincidence (is Not Nothing)
If Not cell Is Nothing Then
firstRow = cell.Row
newRow = cell.Row
'Copy the row and paste on Sheet7
wks.Rows(newRow).Copy
wksdest.Rows(destRow).PasteSpecial xlPasteValues
destRow = destRow + 1
foundValue = True
'Find next coincidences in the same sheet
While foundValue
Set cell = Selection.FindNext(cell)
If Not cell Is Nothing Then
newRow = cell.Row
If newRow <> firstRow Then
wks.Rows(newRow).Copy
wksdest.Rows(destRow).PasteSpecial xlPasteValues
destRow = destRow + 1
Else
foundValue = False
End If
Else
foundValue = False
End If
Wend
End If
Next i
wksdest.Activate
End Sub
使用ALT+開啟 VBA/宏F11,在本練習冊插入一個新模組並將程式碼貼到右側。
點擊綠色三角形執行它。
我在程式碼上添加了註釋,以便您了解它是如何工作的。
您也可以按一下第一行來逐步執行它,然後按 完成每個步驟F8。