[英]Trying to create a Loop that moves Rows to a different sheet based on cell value
I am trying to make a loop that checks for the string (defined by the user) in all the cells in column "A" and cuts every row where cell "A" doesn't contain the string and moves it to another sheet (Cml) I have the following code that runs without any error message but it doesn't seem to do what it's supposed to. 我正在尝试创建一个循环,以检查“ A”列中所有单元格中的字符串(由用户定义),并剪切单元格“ A”中不包含字符串的每一行并将其移至另一张纸上(Cml )我有下面的代码可以运行,没有任何错误消息,但是它似乎并没有达到预期的效果。
Sub PSFormat()
Dim cb As Shape
Dim Cml As Worksheet
Dim Aud As Worksheet
Dim z As Long, LastRow As Long
Dim myDate2 As String
Set Aud = Worksheets("CURRENT")
Set Cml = Worksheets("OLD")
myDate2 = InputBox("Please enter the date you are reviewing in yyyy-mm-dd format")
With Aud
LastRow = Aud.Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column "A"
For z = 2 To LastRow
If InStr(Aud.Range("A" & z).Value2, myDate2) < 0 Then ' check if current cell in column "A" contains "myDate2" defined by the user
'if the cell in col "A" doesn't contain "myDate2" then cut the entire row and paste it to sheet Cml
Aud.Rows((1) & z).EntireRow.Cut _
Destination:=Cml.Rows((1) & z)
End If
Next z
End With
End Sub
Any help would be greatly appreciated! 任何帮助将不胜感激!
[UPDATE] This is the entire macro for reference. [更新]这是供参考的整个宏。
Sub PSFormat()
Dim cb As Shape
Dim Cml As Worksheet
Dim Aud As Worksheet
Dim z As Long, LastRow As Long
Dim myDate2 As String
Set Aud = Worksheets("CURRENT")
Set Cml = Worksheets("OLD")
myDate2 = InputBox("Please enter the date you are reviewing in yyyy-mm-dd format")
Aud.Range("A1").EntireRow.Insert
Aud.Range("A1").Value = "TIME"
Aud.Range("B1").Value = "ACTION"
Aud.Range("C1").Value = "PLATFORM"
Aud.Range("D1").Value = "MAKER ID"
Aud.Range("E1").Value = "APPLICATION"
Aud.Range("F1").Value = "JUSTIFICATION"
Aud.Range("A1:F1").AutoFilter
LastRow = Aud.Cells(Rows.Count, "B").End(xlUp).Row
For x = 1 To LastRow
If Aud.Range("D" & x).Value <> "PSECSELF" Then Aud.Range("F" & x).Value = "A"
If Aud.Range("D" & x).Value = "PSECSELF" Then Aud.Range("F" & x).Value = "N/A"
If Aud.Range("B" & x).Value = "Unsuccessful login attempt" Then Aud.Range("F" & x) = "N/A"
If Aud.Range("B" & x).Value = "Administrator login" Then Aud.Range("F" & x) = "N/A"
If Aud.Range("B" & x).Value = "Remote help successful" Then Aud.Range("F" & x) = "N/A"
If Aud.Range("B" & x).Value = "Helpdesk user deleted" Then Aud.Range("F" & x) = "N/A"
If Aud.Range("B" & x).Value = "Token deleted" Then Aud.Range("F" & x) = "N/A"
Next x
With Aud
LastRow = Aud.Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column "A"
For z = 2 To LastRow
If InStr(Aud.Range("A" & z).Value2, myDate2) < 0 Then ' check if current cell in column "A" contains "myDate2" defined by the user
'if the cell in col "A" doesn't contain "myDate2" then cut the entire row and paste it to sheet Cml
Aud.Range("A" & z).EntireRow.Cut _
Destination:=Cml.Rows((1) & z)
End If
Next z
End With
Aud.Range("F1").Value = "JUSTIFICATION"
Aud.Range("F2").AutoFilter Field:=6, Criteria1:="A"
Aud.Buttons.Add(617.25, 24, 72, 72).Select
Selection.OnAction = "PSSaveFile"
Selection.Characters.Text = "SAVE"
Aud.Range("F2").Select
MsgBox "Please filter for yesterday's date first!"
End Sub
I would do the following but as noted in comments, you are leaving a gap in the range where you cut from. 我会做以下事情,但正如评论中所指出的,您在切入范围内留下了空白。 In which case, deleting the empty rows afterwards is a good idea. 在这种情况下,最好先删除空行。 Assumes values in the sheet are formatted as strings. 假设工作表中的值格式为字符串。
Option Explicit
Sub PSFormat()
Dim cb As Shape
Dim Cml As Worksheet
Dim Aud As Worksheet
Dim z As Long, LastRow As Long
Dim myDate2 As String
Dim LastRowOld As Long
Dim cutRange As Range
Set Aud = Worksheets("CURRENT")
Set Cml = Worksheets("OLD")
myDate2 = InputBox("Please enter the date you are reviewing in yyyy-mm-dd format")
With Aud
LastRow = Aud.Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column "A"
LastRowOld = Cml.Cells(.Rows.Count, "A").End(xlUp).Row
For z = 2 To LastRow
If Not IsEmpty(Aud.Range("A" & z)) And InStr(Aud.Range("A" & z).Value2, myDate2) =0 Then Then ' check if current cell in column "A" contains "myDate2" defined by the user
If Not cutRange Is Nothing Then
Set cutRange = Union(cutRange, Aud.Range("A" & z))
Else
Set cutRange = Aud.Range("A" & z)
End If
'if the cell in col "A" doesn't contain "myDate2" then cut the entire row and paste it to sheet Cml
End If
Next z
End With
If Not cutRange Is Nothing Then
cutRange.Copy Cml.Cells(LastRowOld, "A")
cutRange.Delete
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.