如何在不打开vba的情况下激活excelsheet?

时间:2018-06-19 08:27:53

标签: vba excel-vba excel

如果单元格大于“0”,我想检查另一个excel中的单元格。

如果不打开引用excel就可以这样做吗?

2 个答案:

答案 0 :(得分:4)

您可以使用公式:

=IF('C:\Path\To\Workbook\[myWorkbook.xlsx]Sheet1'!A1>0,"YES","NO")

或者如果你必须使用VBA:

Dim isGreaterThan As Boolean
'// Change the Sheet number and cell reference to match
With GetObject("C:\Path\To\Workbook\myWorkbook.xlsx")
    isGreaterThan = (CInt(.Sheets(1).Range("A1").Value) > 0)
    .Close
End With

Debug.Print "Number is " & IIf(isGreaterThan,"","NOT ") & "greater than 0"

答案 1 :(得分:0)

正如omegastripes评论的那样,使用ADO也是可以的。

以下内容将返回指定工作簿的sheet1上的单元格A1的值。

确保您已设置对" Microsoft ActiveX Data Objects X.X Library"的引用。

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strPath As String

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

'full Path of file to check
strPath = "C:\Path\To\Workbook\myWorkbook.xlsx"

'open connection to workbook
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & strPath & ";" & _
            "Extended Properties=""Excel 12.0;HDR=No;"""

'open recordset of value(s) from specified sheet & range
rs.Open "SELECT * FROM [Sheet1$A1:A1];", cn, adOpenStatic, adLockReadOnly

'print returned value
If Not rs.EOF Then Debug.Print rs(0)

'cleanup
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing