Excel vba:同一行和命令中的非连续单元格

时间:2013-08-06 06:34:18

标签: excel-vba vba excel

我是vba noobie,请帮忙。我有3000行和40列。

从第5行开始到最后一行,只查看第8,11,14,17,20,23,26,29,32列。

我需要编写一个代码,说如果colummns 8,11,14,17,20,23,26,29,32中的每个单元格在同一行中的每个单元格都小于等于5,那么请发表评论在单元格(第40行)=“OK IN PROGRESS” 如果每个相同行中的一个单元格(列8,11,14,17,20,23,26,29,32)绝对大于等于5,那么在单元格(行,40)=(无论哪一列大于5)和“不进行中”

另外,我需要重命名每列的编号。

8=george
11=cindy
14=jennifer
17=lucas
20=apple
23=jeff
26=may
29=kevin
32=oscar

例如,在单元格(第40行),如果它不是8进行中,则8将被乔治取代。

然后下一行

1 个答案:

答案 0 :(得分:0)

这是一个快速的VBA解决方案,如果您需要的话:

Sub ProcessRows()
    Dim rSt As Integer, rEn As Integer, val As Integer, r As Integer
    Dim cSt As Integer, cEn As Integer, c As Integer
    Dim name As String, inProgressMsg As String, notInProgressMsg1 As String, notInProgressMsg2 As String
    rSt = 5
    rEn = 3004
    val = 5
    cSt = 8
    cEn = 32
    inProgressMsg = "OK IN PROGRESS"
    notInProgressMsg2 = "NOT IN PROGRESS"

    For r = rSt To rEn
        notInProgressMsg1 = ""
        For c = cSt To cEn Step 3
            If Cells(r, c).Value > val Then
                name = ReplaceNumberWithName(c)
                notInProgressMsg1 = notInProgressMsg1 & name & " "
            End If
        Next c

        If notInProgressMsg1 = "" Then
            Cells(r, 40).Value = inProgressMsg
        Else
            Cells(r, 40).Value = notInProgressMsg1 & notInProgressMsg2
        End If
    Next r
End Sub

Function ReplaceNumberWithName(n As Integer) As String
    ReplaceNumberWithName = "NO_NAME_FOUND"

    If n = 8 Then
        ReplaceNumberWithName = "george"
    ElseIf n = 11 Then
        ReplaceNumberWithName = "cindy"
    ElseIf n = 14 Then
        ReplaceNumberWithName = "jennifer"
    ElseIf n = 17 Then
        ReplaceNumberWithName = "lucas"
    ElseIf n = 20 Then
        ReplaceNumberWithName = "apple"
    ElseIf n = 23 Then
        ReplaceNumberWithName = "jeff"
    ElseIf n = 26 Then
        ReplaceNumberWithName = "may"
    ElseIf n = 29 Then
        ReplaceNumberWithName = "kevin"
    ElseIf n = 32 Then
        ReplaceNumberWithName = "oscar"
    End If
End Function