2016-05-04 99 views
0

我將我的excel vb代碼複製到vbs中,我知道存在語義/邏輯差異,真的很感謝一些指導。我收到上面的錯誤「Next i」(第44行) - 這本質上是一個整數。800A0401 vbs錯誤

Set xlBook = GetObject("C:\Users\midi\Desktop\IT\E\PRF.xlsm") 
    For each wsheet in xlbook.worksheets 
    msgbox wsheet.name 

next 

Sub RefreshConns() 
    ' Refreshes the connections according to the specified cells 

With Application 
    .EnableEvents = False 
    .ScreenUpdating = False 
End With 
Sheets("Run Macro").Activate 
Dim connName 
Dim connStr 
Dim sqltext ' SQL text 

Dim TempconnName 
Dim TempconnStr 
Dim Tempsqltext ' temporary SQL text 
Dim i 
Dim SiteName 

SiteName = ActiveSheet.Cells(1, 2) 
'MsgBox (SiteName) 
For i = 5 To 11 
connName = ActiveSheet.Cells(i, 1).Value 
connStr = ActiveSheet.Cells(i, 2).Value 
sqltext = ActiveSheet.Cells(i, 4).Value 
'MsgBox (connName) 

TempconnStr = Replace(connStr, "SiteNameVBA", SiteName) 
'Debug.Print (ActiveWorkbook.Connections(connName).ODBCConnection.Connection) 
'MsgBox (TempconnStr) 
'Tempsqltext = Replace(sqltext, "SiteNameVBA", SiteName) 

'On Error Resume Next 
ActiveWorkbook.Connections(connName).ODBCConnection.CommandText = sqltext 
ActiveWorkbook.Connections(connName).ODBCConnection.Connection = "ODBC;" & TempconnStr 
ActiveWorkbook.Connections(connName).Refresh 

Next i 

With Application 
    .EnableEvents = True 
    .ScreenUpdating = True 
End With 

End Sub 

Public Function ZeroToBlank(x String) String 
If x = "0" Then 
    ZeroToBlank = "" 
Else 
    ZeroToBlank = x 
End If 
End Function 
+1

在VBScript中使用'Next',而不是'Next i'。 –

+1

在VBScript中,你總是使用'Next'而不是'Next i'或類似的東西。另外,如果創建Excel應用程序對象,VBScript只能控制Excel。另外 - 你從不*在VBScript中聲明typenames,因爲一切都是變體。實際上,您應該找到一些與Excel交互的VBScript程序的良好示例,並確保您在嘗試修改VBA宏以作爲VBScript程序運行之前瞭解該代碼。 –

+0

@JohnColeman非常感謝!我採納了你的建議,並找到了一些很好的起點。非常感激。 – midiman

回答

0

Next關閉for循環。但你沒有任何for循環。將兩行都刪除,其中next

EG像以前一樣,除了通過省略as type來完成變型之外,您無法聲明任何內容。你只是不考慮類型。

Public Function ZeroToBlank(x) 
    If x = "0" Then 
     ZeroToBlank = "" 
    Else 
     ZeroToBlank = x 
    End If 
End Function