简体   繁体   中英

Copy an entire row from a sheet to another sheet on basis of text in a cell in VBA using For loop

From Sheet1 and Sheet2, if a cell from B column has "In Progress", then I want to copy that entire row to another Sheet4.
I want to repeat it for all rows of both the sheets.

Sub Demo1()

    Dim wb As Workbook
    Dim ws As Worksheet, sh As Worksheet
    Dim lastrow As Long
    Dim w As Integer
    Dim i As Integer
    
    Set wb = Workbooks(Book1)
    Set ws = Worksheets("Sheet4")
    Set sh = ActiveSheet
    
    For w = 1 To wb.Sheets.Count
    
        For i = 1 To lastrow
        
            If ActiveSheetCells(i, 2).Value = "In Progress" Then
            
            wb.ws.Cells(1, 1).Insert
            Else
            If Cells(i, 2).Value = "" And i < 50 Then
            ActiveCell.Offset(1, 0).Select
            End If
            Cells(i, 2).Value = "" And i > 49
       Next i
    
    Next w
End Sub

Error Message
在此处输入图像描述

Sheet 1
在此处输入图像描述

Sheet 2
在此处输入图像描述

Sheet 3
在此处输入图像描述

Quick review on your code, based on my comments to the post (untested):

Sub Demo1()
Dim wb As Workbook:  Set wb = Workbooks("Book1")
Dim destinationSheet As Worksheet:  Set destinationSheet = wb.Worksheets("Sheet4")
Dim sourceSheet As Worksheet:  Set sourceSheet = ActiveSheet
With sourceSheet
    Dim lastRowSource As Long:  lastRowSource = .Cells(.Rows.Count, 1).End(xlUp).Row
    Dim w As Long, i As Long
    For w = 1 To wb.Sheets.Count
        For i = 1 To lastRowSource
            If .Cells(i, 2).Value = "In Progress" Then
                destinationSheet.Cells(1, 1).Insert
            Else
                If .Cells(i, 2).Value = "" And i < 50 Then
                    'Why are you Selecting and what are you doing with it?
                    .Cells(i,X).Offset(1, 0).Select 'Change from "activeCell" to an actual cell reference as you don't change the activecell when looping... 
                End If
                Cells(i, 2).Value = "" And i > 49 'Is this supposed to be another If statement?
            End If 'Added
       Next i
    Next w
End With

Don't use Integer , use Long ; the prior gets converted within VBA so you can save the processing with using the latter.

Use descriptive variable names so you're not lost in 10 months re-looking at your code, or having someone else look at your code. For the most part, people should be able to understand what's happening without the use of excessive comments.

Do your best to not have a wall of variables. If you can dimension a variable just as it's being used, you're pairing things together and might catch that x as long when you're using it as a string a lot faster.

You have a .Select and nothing happens with that. Additionally, included as a comment, using ActiveCell is probably not what you want... use a direct cell reference. Note that when you loop, VBA will change its references, however it does not physically change its activecell .

You have what appears to be another If statement which does not include any If / Then for the i > 49 bit.

The culprit of your error is the lack of End If , which is now placed with the comment Added .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM