0
我需要根據特定條件將行移動到新工作表。我在這裏發現了一個非常有用的討論,它幾乎正是我需要的,但是這些行需要從主表中刪除。我一直在使用的代碼是這樣的:將行按照特定條件剪切/粘貼到新工作表
Option Explicit
Sub Fr33M4cro()
Dim sh33tName As String
Dim custNameColumn As String
Dim i As Long
Dim stRow As Long
Dim customer As String
Dim ws As Worksheet
Dim sheetExist As Boolean
Dim sh As Worksheet
sh33tName = "Sheet1"
custNameColumn = "I"
stRow = 2
Set sh = Sheets(sh33tName)
For i = stRow To sh.Range(custNameColumn & Rows.Count).End(xlUp).Row
customer = sh.Range(custNameColumn & i).Value
For Each ws In ThisWorkbook.Sheets
If StrComp(ws.Name, customer, vbTextCompare) = 0 Then
sheetExist = True
Exit For
End If
Next
If sheetExist Then
CopyRow i, sh, ws, custNameColumn
Else
InsertSheet customer
Set ws = Sheets(Worksheets.Count)
CopyRow i, sh, ws, custNameColumn
End If
Reset sheetExist
Next i
End Sub
Private Sub CopyRow(i As Long, ByRef sh As Worksheet, ByRef ws As Worksheet, custNameColumn As String)
Dim wsRow As Long
wsRow = ws.Range(custNameColumn & Rows.Count).End(xlUp).Row + 1
sh.Rows(i & ":" & i).Copy
ws.Rows(wsRow & ":" & wsRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Private Sub Reset(ByRef x As Boolean)
x = False
End Sub
Private Sub InsertSheet(shName As String)
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = shName
End Sub
任何見解都會有幫助,因爲我對此很新穎。謝謝!在Rows.Count
前面的ws.
:
非常感謝! –
我該如何編輯這個只是剪切/粘貼列A-M的值? –
@ A.Newt - 看我的編輯。 – BruceWayne