2015-01-15 107 views
0

錯誤400,我得到一個400錯誤上Cells.Value在以下功能:VBA Excel中:在Cells.Value

Function Add_Buttons(myColumn) 

    Dim btn As Button 
    ActiveSheet.Buttons.Delete 
    Dim t As Range 

    'Search for last row 
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row 

    'Force-Update 
    Application.Calculate 

    'Generate a button for each cell, if it's not empty 
    For i = 3 To lastRow 
     Set t = Range(Cells(i, myColumn), Cells(i, myColumn)) 
     If Cells(i, t.column - 1).Text = "Nicht OK" Then 
      Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.RowHeight) 
      With btn 
       .OnAction = "'DieseArbeitsmappe.Update_DB """ & ActiveSheet.Cells(i, myColumn).Value & """'" 'THIS IS WHERE THE ERROR OCCURS 
       .Caption = "Update" 
       .Name = "Btn_" & ActiveSheet.Name & "_" & i 
      End With 
     End If 
    Next i 

End Function 

我無法找到我的代碼中的任何錯誤。它與ActiveSheet.Cells(i, myColumn).Value確實是一些東西,因爲沒有這個參數,.OnAction工作正常。

+1

對於我而言,除了myColumn爲'1'外,因爲單元格(1,0)不存在 – brettdj

+0

對於myColumn,我設置了一個字母,在本例中爲「I」。這可能是它失敗的原因嗎?在我看來,如果你設置了一個數字或一個字母,它就不會有什麼區別。 – SoBiT

+0

嘗試使用'Chr(34)'作爲你想要在字符串中的雙引號。 – peege

回答

1

我明白了。如果您有類似的情況,請確保單元格中的引號已經轉義。這樣你可以避免這個錯誤。