在读取一列Excel文件时,如何在不选择单元格的情况下定义单元格坐标?

时间:2012-04-15 07:53:29

标签: excel vba

有谁能告诉我如何改进这个宏?

所有的宏都只是读取一个Excel文件,列出要在应用程序中更新的帐户(SmarTerm Beta)。它在技术上已经完成了目标,但有没有办法对它进行编码,以便在读取Excel文件时,从中读取帐号的单元格的坐标以及写入输出的单元格的坐标不依赖于“预选”的细胞?选择单元格的风险在于,如果有人在宏运行时意外选择了不同的单元格,那么一切都会搞砸。

这是我目前的代码:

Public oExcelObj As Object

Function WaitSystem(Optional NoDialog as Variant) As Boolean
    Dim nContinue as Integer
    Dim nTimeOut as Integer 'In seconds.
    'The default timeout for each command is 3 minutes. 
    'Increase this value if your host requires more time
    'for each command.
    nTimeOut = 10
    If IsMissing(NoDialog) then NoDialog = False
    'Wait for response from host.
    Session.EventWait.Timeout = nTimeOut
    Session.EventWait.EventType = smlPAGERECEIVED
    Session.EventWait.MaxEventCount = 1
    WaitSystem = True
    If Session.EventWait.Start = smlWAITTIMEOUT Then
        If NoDialog Then
            WaitSystem = False
            Else
                nContinue = QuerySyncError()
                If nContinue <> ebYes then WaitSystem = False
        End If
    End If
    Set LockStep = Nothing
End Function

'Establish link.  Search for Excel. 
Function OleLinkConnection
    Const XlMaximized = &HFFFFEFD7
    Titlebar$ = AppFind$("Microsoft Excel")
    If Titlebar$ <> "" Then
        bIsExcelActive = True               
        If AppGetState(Titlebar$) = ebMinimized Then                
            AppSetState 2, Titlebar$
        End If
        Else
            bIsExcelActive = False              
    End If
    If bIsExcelActive Then
        'Create Excel Object using current instance of Excel.
        Set oExcelObj = GetObject(, "Excel.Application")
        Else
            'Create Excel Object using a new instance of Excel.
            Set oExcelObj = CreateObject("Excel.Application")
    End If
    Version = oExcelObj.Application.Version
    oExcelObj.ScreenUpdating = True
    oExcelObj.Displayalerts = True
    oExcelObj.Visible = true
End Function

Sub JPBmacro
    Dim AccountNumber As String
    Dim Temp As Integer
    Begin Dialog StartDialogTemplate ,,211,74,"Run JPBmacro?"
    OKButton 60,12,92,20,.Proceed
    CancelButton 60,40,92,20,.Exit
    End Dialog
    Dim StartDialog As StartDialogTemplate
    r% = Dialog(StartDialog)
    If r% = 0 Then End
    g$ = "G:\DATA\outputfile.xlsx"
    oleCode = OleLinkConnection
    oExcelObj.Workbooks.Open g$
    oExcelObj.Range("A1").Select ‘<----This selects the cell from which all coordinates are based off of.  The coordinates of oExcelObj.ActiveCell.Offset(Y,X).Value VBA depend on selecting a cell.

    NEXTACCOUNT:
        Temp = 0
        AccountNumber = oExcelObj.ActiveCell.Offset(Temp,0).Value
        While AccountNumber <> ""
            Session.SendKey "CLEAR"
            If WaitSystem = False Then End
            Session.Send "ACTU " & AccountNumber
            Session.SendKey "ENTER"
            If WaitSystem = False Then End
            If Trim(Session.ScreenText(4,6,1,22)) = "INVALID ACCOUNT NUMBER" Or Trim(Session.ScreenText(4,6,1,19)) = "ACCOUNT NOT ON FILE" Then
                oExcelObj.ActiveCell.Offset(Temp,1).Value = Trim(Session.ScreenText(4,6,1,22))
                GoTo RESTARTLOOP
            End If 

            UPDATEIOV:
                If Trim(Session.ScreenText(13,76,1,1)) = "Y" Then
                    oExcelObj.ActiveCell.Offset(Temp,1).Value = "Account already flagged as institutional."
                    Else
                        Session.Row = 13
                        Session.Column = 76
                        Session.send "Y"
                        Session.SendKey "ENTER"
                        If WaitSystem = False Then End
                        oExcelObj.ActiveCell.Offset(Temp,1).Value = Trim(Session.ScreenText(24,2,1,50))
                End If

            RESTARTLOOP:
                Temp = Temp + 1
                AccountNumber = oExcelObj.ActiveCell.Offset(Temp,0).Value
        Wend

    ENDNOW:
        oExcelObj.Workbooks.Close
        MsgBox "All Done!"

End Sub

2 个答案:

答案 0 :(得分:0)

为什么不保留对第一个单元格的引用?

Dim rng as Range
Set rng = oExcelObj.Range("A1")
i=1
...
x = rng.Cell(i,1).Value

'Or faster yet is reading all the values into an variant array.
Dim array() as Variant
array = rng.Resize(N,M).Value

' Work with array as
x = array(i,1)

答案 1 :(得分:0)

鉴于来自assylias的评论,另一张海报已经“回答”了这种方法:

我看不到实例化oExcelObj的位置?或者你如何引用特定的表格。

无论哪个,

  1. 您可以通过设置范围来避免选择,即Set rng1 = oExcelObj.Sheets(1).Range("A1")
    然后使用rng1
  2. 的偏移量
  3. 代码运行时用户无法干预