【问题标题】:How to cut entire row based on a specific Text如何根据特定文本剪切整行
【发布时间】:2021-03-26 16:13:14
【问题描述】:

如何为M列中包含“时间”一词的任何单元格剪切整行?

注意我想将数据保存在同一个工作表中。但是,切到最顶端。

一月是工作簿中工作表的名称。

Dim AW As long, I as long 
With Sheets("January")
    AW = .Range("M2:M" & Rows.Count).End(xlUp).Row

    For I = 1 to AW
        With .Range("M2:M" & I)
            If.Value = " Time" Then 
                .EntireRow.Cut Sheets("January").Cells(Rows.Count, "A") End(xlUP).offset(1,0)

【问题讨论】:

  • 您必须循环遍历每个单元格(向后)或过滤,然后才能避免循环。
  • 为什么不在 M 列上排序?
  • 是的,当然。但是,我想将此代码与另一个已完成的代码一起添加。

标签: excel vba cut autofilter


【解决方案1】:
Sub TestRun()
   Call RemoveString("January", "Time", "M")
End Sub



Sub RemoveString(sheetName As String, txt As String, columnLetter As String)

Dim intLastRowNum As Long, intCellNum As Long
With Sheets(sheetName)
    intLastRowNum = .UsedRange.Rows.Count
    For intCellNum = 3 To intLastRowNum
         
            If LCase(.Range(columnLetter & intCellNum).Value) = LCase(txt) Then
                .Rows(intCellNum).EntireRow.Delete
                intCellNum = intCellNum - 1
            End If
            
         
    Next
End With
End Sub

【讨论】:

    【解决方案2】:

    剪切/粘贴到顶部

    • 假设数据在以A1开头的表格中(一行表头和下面的数据)。
    Option Explicit
    
    Sub cutPasteToTop()
        
        Const wsName As String = "January"
        Const cCol As String = "M"
        
        Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
        Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
        
        If ws.AutoFilterMode Then
            ws.AutoFilterMode = False
        End If
        
        Application.ScreenUpdating = False
        
        Dim irg As Range: Set irg = ws.Range("A1").CurrentRegion
        Dim rowsMoved As Boolean
        With irg
            .AutoFilter ws.Columns(cCol).Column, "Time"
            Dim rCount As Long
            rCount = WorksheetFunction.Subtotal(103, .Cells.Resize(, 1))
            If rCount > 1 Then
                Dim srg As Range
                Set srg = irg.Resize(irg.Rows.Count - 1).Offset(1) _
                    .SpecialCells(xlCellTypeVisible)
                ws.AutoFilterMode = False
                irg.Rows(2).Resize(rCount - 1).Insert
                srg.Copy irg.Rows(2).Resize(rCount - 1)
                srg.Delete
                rowsMoved = True
            End If
        End With
    
        Application.ScreenUpdating = True
    
        If rowsMoved Then
            MsgBox "Rows moved.", vbInformation, "Success"
        Else
            MsgBox "Nothing moved.", vbExclamation, "Fail?"
        End If
        
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多