基于Excel的跟踪数据库

时间:2017-04-11 11:20:05

标签: excel excel-vba vba

我有一个代码,每天更新工作表“RAW”,包含更多行并更新现有行,我正在尝试获取B列中的数字以匹配工作表数据中的A列,然后根据具体信息是什么在其他列中将1添加到列中的值(17个不同的选项) 它基本上将被用作跟踪器来检查特定状态下的某些天数,并且我需要将其保留为历史测量值。这是我到目前为止似乎没有用的东西。 另外,如果可能的话,我还希望它能够测量数据列表中缺少的第18个类别吗?

'status tracking
Sub Status_Track()
    Dim a As Long 'topic number
    Dim Z As Long
    Dim R As Long
    Dim i As Long
    Dim S As Long
    Dim D As Long

    Worksheets("RAW").Activate
    R = Cells(Rows.Count, 2).End(xlUp).Row
    C = Cells(1, Columns.Count).End(xlToLeft).Column

    Z = 0
    i = 2
    Do Until i > R

        'ident
        If Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "ERKA") Then
            Z = Worksheets("Data").Cells(i, 6) + 1
            Worksheets("Data").Cells(i, 6).Value = Z
        ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "INBA") Then
            'Inba
            Z = Worksheets("Data").Cells(i, 7) + 1
            Worksheets("Data").Cells(i, 7).Value = Z
        ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "ABGE") Then
            'Abge
            Z = Worksheets("Data").Cells(i, 8) + 1
            Worksheets("Data").Cells(i, 8).Value = Z
        ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "GELO") Then
            'Gelo
            Z = Worksheets("Data").Cells(i, 5) + 1
            Worksheets("Data").Cells(i, 5).Value = Z
        ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "UEBE") And (Cells(i, 11) = 0) Then
            'UEBE
            Z = Worksheets("Data").Cells(i, 9) + 1
            Worksheets("Data").Cells(i, 9).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "<1") Then
            '1
            Z = Worksheets("Data").Cells(i, 10) + 1
            Worksheets("Data").Cells(i, 10).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "6") Then
            '6
            Z = Worksheets("Data").Cells(i, 11) + 1
            Worksheets("Data").Cells(i, 11).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "9") Then
            '9
            Z = Worksheets("Data").Cells(i, 12) + 1
            Worksheets("Data").Cells(i, 12).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "10") Then
            '10
            Z = Worksheets("Data").Cells(i, 13) + 1
            Worksheets("Data").Cells(i, 13).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "15") Then
            '15
            Z = Worksheets("Data").Cells(i, 14) + 1
            Worksheets("Data").Cells(i, 14).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "30") Then
            '30
            Z = Worksheets("Data").Cells(i, 15) + 1
            Worksheets("Data").Cells(i, 15).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "50") Then
            '50
            Z = Worksheets("Data").Cells(i, 16) + 1
            Worksheets("Data").Cells(i, 16).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "60") Then
            '60
            Z = Worksheets("Data").Cells(i, 17) + 1
            Worksheets("Data").Cells(i, 17).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "70") Then
            '70
            Z = Worksheets("Data").Cells(i, 18) + 1
            Worksheets("Data").Cells(i, 18).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "80") Then
            '80
            Z = Worksheets("Data").Cells(i, 19) + 1
            Worksheets("Data").Cells(i, 19).Value = Z
         ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "90") Then
            '90
            Z = Worksheets("Data").Cells(i, 20) + 1
            Worksheets("Data").Cells(i, 20).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "97") Then
            '97
            Z = Worksheets("Data").Cells(i, 21) + 1
            Worksheets("Data").Cells(i, 21).Value = Z
        ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "100") Then
            '100
            Z = Worksheets("Data").Cells(i, 22) + 1
            Worksheets("Data").Cells(i, 22).Value = Z      
        End If
    Loop
End Sub

1 个答案:

答案 0 :(得分:0)

它可能看起来像找到相应的标识符

Option Explicit 'must be the first line in a module: forces you to declare any variables before use

'status tracking
Sub Status_Track_Extended()
    Dim wsRaw As Worksheet, wsData As Worksheet
    Set wsRaw = ThisWorkbook.Worksheets("RAW")
    Set wsData = ThisWorkbook.Worksheets("Data")

    Dim LastRow As Long
    LastRow = wsRaw.Cells(wsRaw.Rows.Count, 2).End(xlUp).Row   'find last row in sheet RAW

    Dim FoundCell As Range, FoundRow As Long
    Dim DataCol As Long
    Dim i As Long

    For i = 2 To LastRow   'start at row 2 up to last used row

        'find corresponding row by identifier (column 2) in sheet Data
        Set FoundCell = wsData.Columns(1).Find(wsRaw.Cells(i, 2))
        If Not FoundCell Is Nothing Then 'only do the follwing if the identifier was found in sheet Data
            FoundRow = FoundCell.Row

            'ident
            If wsRaw.Cells(i, 13) = "ERKA" Then
                wsData.Cells(FoundRow, 6).Value = wsData.Cells(FoundRow, 6).Value + 1
            ElseIf wsRaw.Cells(i, 13) = "INBA" Then
                'Inba
                wsData.Cells(FoundRow, 7).Value = wsData.Cells(FoundRow, 7).Value + 1
            ElseIf wsRaw.Cells(i, 13) = "ABGE" Then
                'Abge
                wsData.Cells(FoundRow, 8).Value = wsData.Cells(FoundRow, 8).Value + 1
            ElseIf wsRaw.Cells(i, 13) = "GELO" Then
                'Gelo
                wsData.Cells(FoundRow, 5).Value = wsData.Cells(FoundRow, 5).Value + 1
            ElseIf wsRaw.Cells(i, 13) = "UEBE" And wsRaw.Cells(i, 11) = 0 Then
                'UEBE
                wsData.Cells(FoundRow, 9).Value = wsData.Cells(FoundRow, 9).Value + 1
            ElseIf wsRaw.Cells(i, 11) = 1 Then
                Select Case wsRaw.Cells(i, 28)
                    Case "<1"
                        wsData.Cells(FoundRow, 10).Value = wsData.Cells(FoundRow, 10).Value + 1
                    Case "6"
                        wsData.Cells(FoundRow, 11).Value = wsData.Cells(FoundRow, 11).Value + 1
                    Case "9"
                        wsData.Cells(FoundRow, 12).Value = wsData.Cells(FoundRow, 12).Value + 1
                    Case "10"
                        wsData.Cells(FoundRow, 13).Value = wsData.Cells(FoundRow, 13).Value + 1
                    Case "15"
                        wsData.Cells(FoundRow, 14).Value = wsData.Cells(FoundRow, 14).Value + 1
                    Case "30"
                        wsData.Cells(FoundRow, 15).Value = wsData.Cells(FoundRow, 15).Value + 1
                    Case "50"
                        wsData.Cells(FoundRow, 16).Value = wsData.Cells(FoundRow, 16).Value + 1
                    Case "60"
                        wsData.Cells(FoundRow, 17).Value = wsData.Cells(FoundRow, 17).Value + 1
                    Case "70"
                        wsData.Cells(FoundRow, 18).Value = wsData.Cells(FoundRow, 18).Value + 1
                    Case "80"
                        wsData.Cells(FoundRow, 19).Value = wsData.Cells(FoundRow, 19).Value + 1
                    Case "90"
                        wsData.Cells(FoundRow, 20).Value = wsData.Cells(FoundRow, 20).Value + 1
                    Case "97"
                        wsData.Cells(FoundRow, 21).Value = wsData.Cells(FoundRow, 21).Value + 1
                    Case "100"
                        wsData.Cells(FoundRow, 22).Value = wsData.Cells(FoundRow, 22).Value + 1
                End Select
            End If

        Else 'error if identifier was not found
            MsgBox "Identifier '" & wsRaw.Cells(i, 2) & "' could not be found in sheet 'Data'.", vbExclamation + vbOKOnly
        End If

    Next i
End Sub