简体   繁体   English

excel中的Visual Basic代码

[英]Visual basic code in excel

This is my table: 这是我的表:

I want to search data from raw table and then feed the data according to date and brand in selected m1,m2,m3,issue,wastage,extra,repack fields. 我想从原始表中搜索数据,然后根据日期和品牌在选定的m1,m2,m3,问题,浪费,额外,重新包装字段中输入数据。 the problem here is that my table is not apdating as per my needs . 这里的问题是我的桌子没有按照我的需要摆放。 here my code is not updating data for brand 'b' of selected date. 这里我的代码没有更新所选日期的品牌'b'的数据。

This is my code: 这是我的代码:

Sub FindMatches()
    Dim oldrow As Integer
    Dim newrow As Integer
     For oldrow = 4 To 14
        For newrow = 3 To 20
            If Cells(oldrow, 12) = Cells(1, newrow) And Cells(oldrow, 13) = Cells(newrow, 1) Then  'date and brand
                    If Cells(1, 14) = Cells(newrow, 2) Then
                          Cells(newrow, 3).Value = Cells(oldrow, 14).Value ' m1
                    End If
                    If Cells(1, 15) = Cells(newrow + 1, 2) Then
                          Cells(newrow + 1, 3).Value = Cells(oldrow, 15).Value ' m2
                    End If
                    If Cells(1, 16) = Cells(newrow + 2, 2) Then
                                    Cells(newrow + 2, 3).Value = Cells(oldrow, 16).Value ' m3
                    End If
                    If Cells(1, 17) = Cells(newrow + 3, 2) Then
                                        Cells(newrow + 3, 3).Value = Cells(oldrow, 17).Value ' issue
                    End If
                    If Cells(1, 18) = Cells(newrow + 4, 2) Then
                                            Cells(newrow + 4, 3).Value = Cells(oldrow, 18).Value ' repack
                    End If
                    If Cells(1, 19) = Cells(newrow + 5, 2) Then
                                                Cells(newrow + 5, 3).Value = Cells(oldrow, 19).Value ' extra
                    End If
                    If Cells(1, 20) = Cells(newrow + 6, 2) Then
                    Cells(newrow + 6, 3).Value = Cells(oldrow, 20).Value ' wastage
                    End If
         End If

        Next newrow
        Next oldrow
End Sub

I'd go with the following 我会选择以下内容

Option Explicit
Sub FindMatches()

Dim rawRng As Range, newTableDateRng As Range, newTableBrandRng As Range, cell As Range, foundDate As Range, foundBrand As Range

Set rawRng = Worksheets("shet").Range("L3:T100")
Set newTableDateRng = Worksheets("shet").Range("C2:I2")
Set newTableBrandRng = Worksheets("shet").Range("A4:A100")

With rawRng    

    For Each cell In .Columns(1).SpecialCells(xlCellTypeConstants)
        Set foundDate = newTableDateRng.Find(What:=cell, LookIn:=xlValues, LookAt:=xlWhole)
        If Not foundDate Is Nothing Then
            Set foundBrand = FindValue(newTableBrandRng, cell.Offset(, 1))
            If Not foundBrand Is Nothing Then
                cell.Offset(, 2).Resize(, 7).Copy
                Intersect(foundDate.EntireColumn, foundBrand.EntireRow).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, , True
            End If
        End If
    Next cell

End With

End Sub


Function FindValue(rng As Range, value As String) As Range

On Error Resume Next
Set FindValue = rng.Cells(Application.WorksheetFunction.Match(value, rng, 0))
On Error GoTo 0

End Function

please note that all initial settings are specific to data "structure" as per your screenshot example. 请注意,按照您的屏幕截图示例,所有初始设置都特定于数据“结构”。 should you need to change it, you have to follow the same "pattern" (ie rawRng starts from the first "raw table" data row under the header, and so on). 如果你需要改变它,你必须遵循相同的“模式”(即rawRng从标题下的第一个“原始表”数据行开始,依此类推)。 also note that the 7 number used in cell.Offset(, 2).Resize(, 7).Copy statement comes out from your posted data structure , where you need to copy values from "m1" to "wastage" fields included, and that you must then make sure that each "brand" rows group in "new table" must match this pattern (namely, have 7 rows) 另请注意, cell.Offset(, 2).Resize(, 7).Copy语句中使用的7数字来自您发布的数据结构,您需要将值从“m1”复制到包含的“wastage”字段,并且然后必须确保“新表”中的每个“品牌”行组都必须与此模式匹配(即具有7行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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