当两列相等时使用'Case'

时间:2015-09-29 00:19:41

标签: excel vba excel-vba loops case

我正在尝试在VBA中创建一个案例,其中“:)”将列在AL列中,当列AJ& AK是相同的值:

Sub ColumnAL()

Dim LockDays, StartCounter, LastRow, Equal, BuySideLockDays, PreviousCell


Worksheets("Table").Activate
LastRow = LastRowNum(Sheets("Table"))
Cells(1, 36).Value = "BuySideLockDays"
Cells(1, 37).Value = "LockDays"
Cells(1, 38).Value = "Equal"

For StartCounter = 2 To LastRow
LockDays = Cells(StartCounter, 37).Value

Select Case LockDays
    Case Is = BuySideLockDays
        Equal = ":)"

   Case Else
        Equal = "!!!"

End Select
Cells(StartCounter, 38).Value = Equal


Next StartCounter

End Sub

我也使用PublicFunction来设置我的LastRow编号,它似乎正在起作用:

Public Function LastRowNum(Sheet As Worksheet) As Long
If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
    LastRowNum = Sheet.Cells.Find(What:="*", _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
Else
    LastRowNum = 1
End If
End Function

Excel表格目前仅返回“!!!”

2 个答案:

答案 0 :(得分:0)

Sub ColumnAL()

    Dim StartCounter, LastRow
    Dim ws As Worksheet, rw as Range    

    Set ws = Worksheets("Table")

    ws.Activate

    LastRow = LastRowNum(ws)

    ws.Cells(1, 36).Value = "BuySideLockDays"
    ws.Cells(1, 37).Value = "LockDays"
    ws.Cells(1, 38).Value = "Equal"

    For StartCounter = 2 To LastRow

        Set rw = ws.Rows(StartCounter)
        rw.Cells(38).Value = _
           IIf(rw.Cells(36).Value = rw.Cells(37).Value, ":)", "!!!")

    Next StartCounter

End Sub

答案 1 :(得分:0)

您的代码工作正常,但您忘记了一条陈述:

library(stringi)
data.frame(words = unlist(stri_extract_all_words(stri_trans_tolower(documents))))
#       words
# 1      this
# 2        is
# 3  document
# 4    number
# 5       one
# 6  document
# 7       two
# 8        is
# 9       the
# 10   second
# 11  element
# 12       of
# 13      the
# 14   vector

把它放在select语句之前。 (测试)