将窗口句柄传递给变量

时间:2013-03-15 05:53:25

标签: excel-vba vba excel

Sub Macro1()
'
' Macro1 Macro
'

'
    Dim wn, contacts, report As Excel.Window
    Dim windows(1 To 100) As Excel.Window
    Dim i As Integer

    i = 1
    For Each wn In Application.windows
        windows(i) = wn
        i = i + 1
    Next wn

    If IsEmailValid(windows(1).Cells(1, 1)) = True Then
        report = windows(1)
        contacts = windows(2)
    Else
        contacts = windows(1)
        report = windows(2)
    End If


End Sub

你在这看错了什么?根据我对VBA的了解,我正在努力。

1 个答案:

答案 0 :(得分:1)

  • 您没有正确声明变量(wncontactsvariant s。) 使用Dim wn As Excel.Window, contacts As Excel.Window, report As Excel.Window
  • 您需要使用Set分配object
    Set windows(i) = wn
  • window对象没有Cells属性
    目前尚不清楚你实际想要实现的目标,但猜测你可能想要Worksheets集合(workbook)或可能Application.Workbooks而不是Application.Windows