VB6检查并使用已打开的excel文件

时间:2018-01-16 05:33:02

标签: excel vb6

我尝试使用excel文件,如果它未打开则打开新文件或使用已经打开但未打开新文件的文件

在下一个代码中,每次运行此代码时都会打开新的Excel文件。 我想改变它只是打开一个新文件,只有它没有打开, 如果它已经打开,则在不打开新文件的情况下使用打开的文件

Set x = Excel.Workbooks(wbname)

有时它的工作,有时候不...我不明白 有一段时间Excel手表

Watch :   :     Excel : <Expression not defined in context> : Empty : Form1.WorkbookIsOpen

我不明白为什么???

Dim a(83) As String, b(82) As String, c(83) As Single, d(82) As Single, e(82) As Single
Dim y As Single

Function WorkbookIsOpen(wbname) As Boolean
    Dim x As Workbook
    On Error Resume Next
    Set x = Excel.Workbooks(wbname)
    If Err = 0 Then WorkbookIsOpen = True _
    Else WorkbookIsOpen = False
End Function


Private Sub Form_Load()

Dim Ex As Workbook

If WorkbookIsOpen("M678.xlsm") = False Then
    Set Ex = CreateObject("excel.application")
    Ex.Workbooks.Open "C:\prg\bak-new\M678.xlsm"
    Ex.Application.WindowState = xlMaximized
    Ex.Visible = True
Else
    Set Ex = Excel.Workbooks("M678.xlsm")
End If


   For y = 0 To 82
    Ex.Sheets("Product.data").Select
Ex.Range("b" & (y + 4)).Select
 a(y) = Ex.ActiveCell.FormulaR1C1
     Ex.Sheets("Product.data").Select
Ex.Range("d" & (y + 4)).Select
  b(y) = Val(Ex.ActiveCell.FormulaR1C1)
Ex.Range("c" & (y + 4)).Select
 c(y) = Val(Ex.ActiveCell.FormulaR1C1)

   Ex.Sheets("Product.Data no7").Select
Ex.Range("c" & (y + 4)).Select
 d(y) = Val(Ex.ActiveCell.FormulaR1C1)

    Ex.Sheets("Product.Data no8").Select
Ex.Range("c" & (y + 4)).Select
  e(y) = Val(Ex.ActiveCell.FormulaR1C1)
Next

w_p

End Sub

Public Sub w_p()
Dim Ex As Object
Set Ex = CreateObject("excel.application")
Ex.Workbooks.Add
Ex.Visible = True
For y = 1 To 82
Ex.Range("a" & (y)).Select
Ex.ActiveCell.FormulaR1C1 = a(y)
Ex.Range("b" & (y)).Select
Ex.ActiveCell.FormulaR1C1 = b(y)
Ex.Range("c" & (y)).Select
Ex.ActiveCell.FormulaR1C1 = c(y)
Ex.Range("d" & (y)).Select
Ex.ActiveCell.FormulaR1C1 = d(y)
Ex.Range("e" & (y)).Select
Ex.ActiveCell.FormulaR1C1 = e(y)
Next
End
End Sub

EDITING !!!

我尝试了另一种方法,通过添加一个按钮来运行操作而不是一遍又一遍地运行.EXE文件... 但是,我还有一些问题 我跑的时候

Ex.Sheets(&#34; Product.data&#34)。选择

看起来它不是活跃的工作簿......我如何让它变得活跃?

新代码在这里:

Dim a(83) As String, b(82) As String, c(83) As Single, d(82) As Single, e(82) As Single
Dim y As Single
Dim ex As Excel.Application
Dim subEx As Workbook

Private Sub Command1_Click()
   For y = 0 To 82
    ex.Sheets("Product.data").Select
ex.Range("b" & (y + 4)).Select
 a(y) = ex.ActiveCell.FormulaR1C1
     ex.Sheets("Product.data").Select
ex.Range("d" & (y + 4)).Select
  b(y) = Val(ex.ActiveCell.FormulaR1C1)
ex.Range("c" & (y + 4)).Select
 c(y) = Val(ex.ActiveCell.FormulaR1C1)

   ex.Sheets("Product.Data no7").Select
ex.Range("c" & (y + 4)).Select
 d(y) = Val(ex.ActiveCell.FormulaR1C1)

    ex.Sheets("Product.Data no8").Select
ex.Range("c" & (y + 4)).Select
  e(y) = Val(ex.ActiveCell.FormulaR1C1)
Next

w_p
End Sub

Private Sub Form_Load()

Set ex = CreateObject("excel.application")
ex.Workbooks.Open "C:\prg\bak-new\M678.xlsm"
ex.Application.WindowState = xlMaximized
ex.Visible = True
Set subEx = ex.Workbooks.Add
ex.DisplayAlerts = False
End Sub

Public Sub w_p()
'ex.Workbooks.Add
'subEx.Visible = True
For y = 1 To 82
subEx.Range("a" & (y)).Select
subEx.ActiveCell.FormulaR1C1 = a(y)
subEx.Range("b" & (y)).Select
subEx.ActiveCell.FormulaR1C1 = b(y)
subEx.Range("c" & (y)).Select
subEx.ActiveCell.FormulaR1C1 = c(y)
subEx.Range("d" & (y)).Select
subEx.ActiveCell.FormulaR1C1 = d(y)
subEx.Range("e" & (y)).Select
subEx.ActiveCell.FormulaR1C1 = e(y)
Next
End Sub

0 个答案:

没有答案