2017-08-22 112 views
0

下面的代碼是在我的函數的開始使用時,我接收到「用戶類型沒有定義」錯誤。我只是定義一個包含數字的單元格作爲變量,以減少稍後在我的代碼中輸入的數量。我很困惑,我的變量類型是沒什麼特別的,只是一個老好人數..定義變量

Sub PECcalc() 

Dim pu238 As [Worksheets("PEC Conversion").Cells(4, 6).Value] 
Dim pu241 As [Worksheets("PEC Conversion").Cells(5, 6).Value] 
Dim am241 As [Worksheets("PEC Conversion").Cells(6, 6).Value] 
Dim pu238PEC As [Worksheets("PEC Conversion").Cells(2, 2).Value] 
Dim pu239PEC As [Worksheets("PEC Conversion").Cells(3, 2).Value] 
Dim pu240PEC As [Worksheets("PEC Conversion").Cells(4, 2).Value] 
Dim pu241PEC As [Worksheets("PEC Conversion").Cells(5, 2).Value] 
Dim am241PEC As [Worksheets("PEC Conversion").Cells(6, 2).Value] 
Dim np237PEC As [Worksheets("PEC Conversion").Cells(7, 2).Value] 
Dim u328PEC As [Worksheets("PEC Conversion").Cells(8, 2).Value] 
Dim u235PEC As [Worksheets("PEC Conversion").Cells(9, 2).Value] 
+5

'昏暗pu238作爲123.231'(假設電池包含123.231)沒有任何意義 - 嘗試'Dim pu238 As Double:pu238 = Worksheets(「PEC Conversion」)。Cells(4,6).Value' – YowE3K

+0

修復了前3行但是後續行 'Dim pu238PEC As Double :pu238PEC [工作表( 「PEC轉換」)細胞(2,2)。價值]' 給我的錯誤「請編譯錯誤:預期子,功能或屬性」 – Harry

+1

'昏暗pu238PEC作爲雙:[工作表(「PEC轉換」)細胞(2,2)。價值] pu238PEC'應該已經寫在以類似的方式前三 - 即'昏暗pu238PEC爲雙:pu238PEC =工作表(「PEC轉換」)細胞(2,2).Value' – YowE3K

回答

1
Sub PECcalc() 

    Dim pu238 As Double 
    pu238 = Worksheets("PEC Conversion").Cells(4, 6).Value 

    Dim pu241 As Double 
    pu241 = Worksheets("PEC Conversion").Cells(5, 6).Value 

    Dim am241 As Double 
    am241 = Worksheets("PEC Conversion").Cells(6, 6).Value 

    Dim pu238PEC As Double 
    pu238PEC = Worksheets("PEC Conversion").Cells(2, 2).Value 

    Dim pu239PEC As Double 
    pu239PEC = Worksheets("PEC Conversion").Cells(3, 2).Value 

    Dim pu240PEC As Double 
    pu240PEC = Worksheets("PEC Conversion").Cells(4, 2).Value 

    Dim pu241PEC As Double 
    pu241PEC = Worksheets("PEC Conversion").Cells(5, 2).Value 

    Dim am241PEC As Double 
    am241PEC = Worksheets("PEC Conversion").Cells(6, 2).Value 

    Dim np237PEC As Double 
    np237PEC = Worksheets("PEC Conversion").Cells(7, 2).Value 

    Dim u328PEC As Double 
    u328PEC = Worksheets("PEC Conversion").Cells(8, 2).Value 

    Dim u235PEC As Double 
    u235PEC = Worksheets("PEC Conversion").Cells(9, 2).Value 
    '...etc 

或其他風格:

Sub PECcalc() 

    Dim pu238 As Double 
    Dim pu241 As Double 
    Dim am241 As Double 
    Dim pu238PEC As Double 
    Dim pu239PEC As Double 
    Dim pu240PEC As Double 
    Dim pu241PEC As Double 
    Dim am241PEC As Double 
    Dim np237PEC As Double 
    Dim u328PEC As Double 
    Dim u235PEC As Double 

    pu238 = Worksheets("PEC Conversion").Cells(4, 6).Value 
    pu241 = Worksheets("PEC Conversion").Cells(5, 6).Value 
    am241 = Worksheets("PEC Conversion").Cells(6, 6).Value 
    pu238PEC = Worksheets("PEC Conversion").Cells(2, 2).Value 
    pu239PEC = Worksheets("PEC Conversion").Cells(3, 2).Value 
    pu240PEC = Worksheets("PEC Conversion").Cells(4, 2).Value 
    pu241PEC = Worksheets("PEC Conversion").Cells(5, 2).Value 
    am241PEC = Worksheets("PEC Conversion").Cells(6, 2).Value 
    np237PEC = Worksheets("PEC Conversion").Cells(7, 2).Value 
    u328PEC = Worksheets("PEC Conversion").Cells(8, 2).Value 
    u235PEC = Worksheets("PEC Conversion").Cells(9, 2).Value 
    '...etc 
+0

我仍然得到「類型不匹配」錯誤 – Harry

+4

請與方廢除括號並從編譯時驗證你在'['和']'之間放置的任何內容。方括號表達式只是'Application.Evaluate'的簡寫。系統地使用它們是非常危險的,有人應該指出。 –

+0

值是否也需要放在括號中?編輯:是會說......括號的目的是什麼? – dwirony