2015-09-29 61 views
0

我想創建中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工作表是目前唯一的「回!」

+0

在您的代碼中'BuySideLockDays'永遠不會被賦值。 –

回答

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 
0

你的代碼工作正常,但你忘了一個聲明:

BuySideLockDays = Cells(StartCounter, 36).Value 

把它放在select語句之前。 (已測試)

+0

@TimWilliams抱歉,在發佈我的郵件之前,沒有看到您的答案。我應該刪除嗎? –

+0

如果OP對代碼的其餘部分感到滿意​​,那麼他們可以只添加你的行...... –

+0

通常這是我嘗試做的事情,幫助用最小的改變修復給定的代碼。但我很欣賞其他人的努力(比如你的)建議全面清潔解決方案:) –