0

このマクロを改善する方法を教えてください。

マクロが行うのは、アプリケーション (SmarTerm Beta) でアカウントが更新するリストの Excel ファイルを読み取ることだけです。技術的にはすでに目標を達成していますが、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 ‘&lt;----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
4

2 に答える 2

0

assylias からのコメントと、別の投稿者がこのアプローチで「回答」したことを考えると、

oExcelObjインスタンス化されている場所がわかりません。または、特定のシートをどのように参照しているか。

どちらにせよ、

  1. 範囲を設定することで選択を回避できます。つまりSet rng1 = oExcelObj.Sheets(1).Range("A1")
    、からのオフセットを使用できますrng1
  2. コードの実行中にユーザーが干渉することはできません
于 2012-04-16T04:32:15.240 に答える
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)
于 2012-04-16T03:55:03.140 に答える