2017-10-13 115 views
2

當我運行我的案例時,它僅返回每條線的Else語句。我知道這種情況應該起作用,因爲當我在像「D2」這樣的一個單元格上測試它時,只有它返回了正確的數據。只有當我嘗試在循環內使用這種情況時,我纔會得到全零。我已經嘗試過研究幾件事情,但無法使代碼正確工作。然而,我對VBA非常陌生,因爲我只有在接受這個項目之前擁有一些JavaScript經驗,請幫助。案例僅返回Else語句

Dim account As Range 
Dim i As Long 

Set account = Range("D2:D1100") 
For Each cell In account 

    If cell.Value Then 

     Dim acctNum As Integer, result4 As Integer 

      Select Case acctNum 

       Case 1110 
        result = 105010 

       Case 2710 
        result = 205000 

       Case 2750 
        result = 205010 

       Case 3110 
        result = 401100 

       Case 3115 
        result = 401110 

       Case 3120 
        result = 401120 

       Case Else 
        result = 0 

      End Select 
     cell.Offset(0, 1).Value = result 
    End If 
Next 
+5

內評估您的cases對細胞你不指定任何值ACCTNUM – NineBerry

+0

我必須說實話,我不知道我在做什麼,因爲這是我第一次每週使用VBA ......我不得不研究並拆解迄今爲止的每一段代碼。所以請原諒我,如果我像一個愚蠢的問題,像我應該分配給acctNum什麼價值?你的意思是像acctNum = XX?我到底在哪裏做這個任務? – PonderingOrchid

+0

那麼你選擇'acctNum'的值的情況下,它位於哪裏?在給「結果」賦予一個值之前,你必須首先測試某些東西。 – dwirony

回答

0

我修改了下面的代碼,它的工作原理是exp ected。我會建議使用這種類型的工作陣列,但這對於稍後當您更熟悉VBA時需要考慮。

您遇到的錯誤已被其他人指出,並且我修復了一些您稍後會發現的錯誤。

對於超過32000的值,你想用龍,而不是整數(它不是完全32000,但如果你在任何地方接近這個數字,只是用龍)

我不認爲它使用的價值acctNum所以我把它拿出來了,你Dim'i'但是不用它,所以我也刪除了它。

Dim account As Range 
Dim result As Long 

Set account = Range("D2:D1100") 
For Each cell In account 

    If cell.Value Then 

     Select Case cell.Value 

      Case 1110 
       result = 105010 

      Case 2710 
       result = 205000 

      Case 2750 
       result = 205010 

      Case 3110 
       result = 401100 

      Case 3115 
       result = 401110 

      Case 3120 
       result = 401120 

      Case Else 
       result = 0 

     End Select 
    cell.Offset(0, 1).Value = result 
End If 
Next 
+0

哇!很感謝!它更乾淨! – PonderingOrchid

+0

不客氣。當您的聲望高到足以解鎖投票能力時,請不要忘記提供您認爲有幫助的答案,讓其他人更容易找到答案。 –

1

您必須爲變量acctNum分配一個值。現在,您只需創建變量,以便在使用該變量時沒有任何價值。見下文。

Dim acctNum As Integer, result4 As Integer ' You've created the variable here 

Select Case acctNum 'And here you're trying to use it but it hasn't been given a value yet 

相反,如果你給它的值代碼中Select Case部分之前將提供你正在尋找的結果:

Dim acctNum As Integer, result4 As Integer ' You've created the variable here 

acctNum = 2710 

Select Case acctNum 'And now the variable has a value and should hit the '2710' case 

希望這有助於:)

+0

請注意,在這裏我只給它分配了一個硬編碼值'2710'。你可以從單元格中提取該值(例如acctNum = cell.Value) – tmwoods

0

好像你想要的東西像這樣:

Dim account As Range 
Dim i As Long 
Dim acctNum As Integer, result4 As Integer 

Set account = Range("D2:D1100") 

For Each cell In account.Cells 

    If Len(cell.Value) > 0 Then 
     Select Case cell.Value 
      Case 1110 
       result = 105010 
      Case 2710 
       result = 205000 
      Case 2750 
       result = 205010 
      Case 3110 
       result = 401100 
      Case 3115 
       result = 401110 
      Case 3120 
       result = 401120 
      Case Else 
       result = 0 
     End Select 
     cell.Offset(0, 1).Value = result 
    End If 
Next 
0

爲了幫助您理解誤解可能在哪裏。

你在說什麼Select Case acctNum這意味着每個人case將根據acctNumTrue/ False爲基準進行評估。例如:

是否1110評價爲True針對acctNum?不...那麼下一個案例。

是否2710評價爲True針對acctNum?沒有...等,直到你打你的其他陳述。

的原因,這些都被作爲評估是False因爲你沒有分配一個值acctNum因此你不是真的評估反對任何。

正如上面的答案中所述,如果您更改所評估的內容,您將獲得預期的輸出。

Select Case cells.value - 現在你的account範圍

+0

謝謝。這是一個有意義的解釋。 – PonderingOrchid