【问题标题】:Select each row and put in different sheet选择每一行并放入不同的工作表
【发布时间】:2021-08-30 17:32:50
【问题描述】:

在我的第 2 列中的工作功能中,我想选择该行并将其复制到同名的工作表中。在我运行代码的所有时间里,都会出现一条消息 1004 错误。

`Sub ventilation()

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim LastRow As Integer
Dim dernierelilgne As Integer

Application.ScreenUpdating = False

' faire une boucle pour effacer les feuilles seelctionner'

For j = 1 To 9
    
    Sheets(j).Select
    LastRow = Range("A1000000").End(xlUp).Row
    For i = LastRow To 6 Step -1
        Sheets(j).Select
        Selection.Delete Shift:=xlUp
    Next i
    
    Sheets("source").Select
    derniereligne = Range("A1000000").End(xlUp).Row
      
      'en fonction du mot dans la colonne, envoyer vers la feuille correspondante'
    
    For k = 6 To derniereligne
    
        Sheets("source").Select
        If Sheets(j).Name = Cells(k, 2).Value Then
        
            Rows(k).Select
            Selection.Copy
            Sheets(j).Select
            LastRow = Range("A1000000").End(xlUp).Row + 1
            Cells(LastRow, 1).Select
            ActiveSheet.Paste
            
        End If
        
    Next k
    
Next j

Sheets("Source").Select
Application.CutCopyMode = False
Application.ScreenUpdating = True

结束子

如果您有解决方案,请!

【问题讨论】:

  • 不要Select,也不要依赖implicit references
  • For i = LastRow To 6 Step -1 循环应该做什么?也许Sheets(j).Select 应该是Rows(i).Select
  • 'for i= lastrow to 6 step -1' 假设在放置“源”的新引用之前循环清理它的工作表。

标签: excel vba copy row excel-2010


【解决方案1】:

 Sub ventilation()

    Dim ws As Worksheet, wsSrc As Worksheet
    Dim i As Long, j As Long, k As Long
    Dim sName As String
    Dim LastRow As Long, derniereligne As Long

    Application.ScreenUpdating = False

    ' faire une boucle pour effacer les feuilles seelctionner'
    Set wsSrc = ThisWorkbook.Sheets("source")
    derniereligne = wsSrc.Range("A" & Rows.Count).End(xlUp).Row

    For j = 1 To 9
        Set ws = ThisWorkbook.Sheets(j)
        sName = ws.Name
        LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
        ws.Rows("2:" & LastRow).Delete
        LastRow = 2
    Next j
      'en fonction du mot dans la colonne, envoyer vers la feuille correspondante'
    
        For k = 2 To derniereligne
            If wsSrc.Cells(k, 2).Value = sName Then
                wsSrc.Rows(k).Copy ws.Range("A" & LastRow)
                LastRow = LastRow + 1
            End If
        Next k
    
    Application.ScreenUpdating = True
End Sub

【讨论】:

  • 它清理工作表 1 到 9,但不复制这些工作表中的行。
  • 您将 B 列与 wsSrc.Cells(k, 2).Value = sName 匹配,但屏幕截图显示 A 列中的工作表名称。此外,Next j 应该在 Next K 之后。我会更新我的答案。
  • okai 感谢您的时间。我不完全理解代码,但它现在可以工作了!
【解决方案2】:

没有选择

更新 1 - LastRow 改为使用 UsedRange

更新 2 - 匹配 A 列

Sub ventilation()

    Dim ws As Worksheet, wsSrc As Worksheet, header As Range
    Dim i As Long, j As Long, k As Long
    Dim sName As String
    Dim LastRow As Long, derniereligne As Long

    Application.ScreenUpdating = False

    ' faire une boucle pour effacer les feuilles seelctionner'
    Set wsSrc = ThisWorkbook.Sheets("source")
    Set header = wsSrc.Range("A1:I1")
    derniereligne = wsSrc.Range("A" & Rows.Count).End(xlUp).Row

    For j = 1 To 9
        Set ws = ThisWorkbook.Sheets(j)
        sName = ws.Name
        
        LastRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
        If LastRow >= 3 Then ws.Rows("3:" & LastRow).Delete
        LastRow = 3

        'en fonction du mot dans la colonne, envoyer vers la feuille correspondante'
        header.Copy ws.Range("A2")
        For k = 2 To derniereligne
            If wsSrc.Cells(k, "A").Value = sName Then
                wsSrc.Rows(k).Copy ws.Range("A" & LastRow)
                LastRow = LastRow + 1
            End If
        Next k
    Next j
    Application.ScreenUpdating = True
    MsgBox derniereligne - 1 & " rows scanned on worksheet " & wsSrc.Name, vbInformation
End Sub

【讨论】:

  • 感谢您的回答,但会删除第一行并且不要复制该行。也许是因为我看不懂你的代码。
  • @Cédric source 是什么表号?工作簿中有多少张?
  • 工作表来源是最后一个,我有 10 张工作表。
  • @Cédric 擦除哪张纸上的第一行? ws.Rows("6:" & LastRow).Delete 删除工作表 1 到 9 上第 5 行下方的所有行。注意 dernierelilgne As Long 应该是 derniereligne As Long 但这不会停止它的工作。
  • 他擦除了每张纸(9),我已经纠正了错误dernierelilgne
猜你喜欢
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多