If then statement with multiple conditions to place values from one sheet into another sheet

Sub Values()
Dim wb As Workbook

Dim Data, Bun, Bun_Dept, Master As Worksheet
Dim lastrow, lrow As Long
Dim i As Integer
Dim j As Integer

Set Data = Worksheets("Master")
Set Bun = Worksheets("Bun_Dept")


lastrow = Data.Cells(Rows.Count, "A").End(xlUp).Row
For i = 5 To lastrow
k = Data.Cells(i, 1).Value
Next i
MsgBox (i)
lrow = Bun.Cells(Rows.Count, "B").End(xlUp).Row
 For j = 4 To lrow
 Next j
  If Data.Cells(i, 7) = "Location" And Data.Cells(i, 18) = "BUN" And Data.Cells(i, 73) <> "" Then
  Bun.Cells(j, 2) = Data.Cells(i, 1).Value: Bun.Cells(j, 3) = Data.Cells(i, 2).Value: Bun.Cells(j, 4) = Data.Cells(i, 4).Value
  Bun.Cells(j, 5) = Data.Cells(i, 5).Value: Bun.Cells(j, 6) = Data.Cells(i, 58).Value: Bun.Cells(j, 7) = Data.Cells(i, 59).Value
  Bun.Cells(j, 8) = Data.Cells(i, 26).Value: Bun.Cells(j, 9) = Data.Cells(i, 25).Value: Bun.Cells(j, 12) = Data.Cells(i, 73).Value
  Bun.Cells(j, 13) = Data.Cells(i, 74).Value
MsgBox ("Data.Cells(i, 1).Value")
End If

End Sub

Looking for a way to transfer values from one sheet to another provided that the conditions are met in the If Then statement. If condition is met in that row then values are placed in another sheet.

1

Copy Rows Meeting Conditions to Another Worksheet

  • We are practicing how to build a loop here. This is more efficiently handled using the Range.AutoFilter method and/or by introducing data structures (arrays, collections, dictionaries).

Source

Destination

A Quick Fix

  • Copies the values of specific columns of each source row, where all conditions are met, to specific columns of the first available destination row.
Sub ImportNewData()
    
    ' Workbook
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Source
    Dim sws As Worksheet: Set sws = wb.Sheets("Master")
    Dim sLastRow As Long:
    sLastRow = sws.Cells(sws.Rows.Count, "BU").End(xlUp).Row
    ' It could also be either 'R' or 'G' but their values aren't copied.
    
    ' Destination
    Dim dws As Worksheet: Set dws = wb.Sheets("Bun_Dept")
    Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "L").End(xlUp).Row
    
    ' Declare additional variables.
    Dim sRow As Long, AreConditionsMet As Boolean
    
    ' Loop (iterate) through the rows of the source worksheet.
    For sRow = 5 To sLastRow
        AreConditionsMet = False ' reset for each iteration
        ' Check if the conditions are met.
        If CStr(sws.Cells(sRow, "BU").Value) <> "" Then
            If CStr(sws.Cells(sRow, "R").Value) = "BUN" Then
                If CStr(sws.Cells(sRow, "G").Value) = "Location" Then
                    AreConditionsMet = True
                End If
            End If
        End If
        ' If conditions are met, write to the next destination row.
        If AreConditionsMet Then
            dRow = dRow + 1
            dws.Cells(dRow, "B").Value = sws.Cells(sRow, "A").Value
            dws.Cells(dRow, "C").Value = sws.Cells(sRow, "B").Value
            dws.Cells(dRow, "D").Value = sws.Cells(sRow, "D").Value
            dws.Cells(dRow, "E").Value = sws.Cells(sRow, "E").Value
            dws.Cells(dRow, "F").Value = sws.Cells(sRow, "BF").Value
            dws.Cells(dRow, "G").Value = sws.Cells(sRow, "BG").Value
            dws.Cells(dRow, "H").Value = sws.Cells(sRow, "Z").Value
            dws.Cells(dRow, "I").Value = sws.Cells(sRow, "Y").Value
            ' The following line determines the columns for the last rows!
            ' If there are empty cells at the bottom of source column 'BU',
            ' they will not be checked (increasing efficiency).
            ' More importantly, only rows containing non-blank ('<>""') values
            ' in source column 'BU' will be copied ensuring there will always
            ' be a value in column 'L' of the destination sheet.
            dws.Cells(dRow, "L").Value = sws.Cells(sRow, "BU").Value
            dws.Cells(dRow, "M").Value = sws.Cells(sRow, "BV").Value
        End If
    Next sRow

    ' Inform.
    MsgBox "New data imported.", vbInformation

End Sub

2

Your code has several problems:

  • The first for loop does nothing. It loops through the rows, reading a value and assigning it to the variable k, and then immediately throws it away without using it to make the next loop. I don’t know what you intendd it to do, but you missed. I’ve removed it.

  • You’ve got Next j in the wrong place. Your loop is running, doing nothing, because there’s nothing between For j and Next j. All the code that you want to execute has to be between the For and Next. The code where you’re trying to copy the data is just executing once.

  • Your code formatting is a mess. If you properly indent the code, it’s much easier to follow the flow of execution when reading it or stepping through in the debugger.

Here’s an (untested) rework of your code to address those issues. Clearly, I don’t have your data to test it, and you didn’t post any sample data at all (which you should do).

Sub Values()
  Dim wb As Workbook
  Dim Data, Bun, Bun_Dept, Master As Worksheet
  Dim lastrow, lrow As Long
  Dim i As Integer
  Dim j As Integer

  Set Data = Worksheets("Master")
  Set Bun = Worksheets("Bun_Dept")

  lastrow = Data.Cells(Rows.Count, "A").End(xlUp).Row
  lrow = Bun.Cells(Rows.Count, "B").End(xlUp).Row
  For j = 4 To lrow
    If Data.Cells(i, 7) = "Location" And Data.Cells(i, 18) = "BUN" And Data.Cells(i, 73) <> "" Then
      Bun.Cells(j, 2) = Data.Cells(i, 1).Value: Bun.Cells(j, 3) = Data.Cells(i, 2).Value: Bun.Cells(j, 4) = Data.Cells(i, 4).Value
      Bun.Cells(j, 5) = Data.Cells(i, 5).Value: Bun.Cells(j, 6) = Data.Cells(i, 58).Value: Bun.Cells(j, 7) = Data.Cells(i, 59).Value
      Bun.Cells(j, 8) = Data.Cells(i, 26).Value: Bun.Cells(j, 9) = Data.Cells(i, 25).Value: Bun.Cells(j, 12) = Data.Cells(i, 73).Value
      Bun.Cells(j, 13) = Data.Cells(i, 74).Value
    End If
  Next j
End Sub

And speaking of the debugger, you need to learn to use it. You would have very quickly found the problem yourself by stepping through either one of those loops, especially the second one, because you would have seen it wasn’t doing what you wanted.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật