have to run a macro twice

1.2k views Asked by At

i need to run the macro twice for it to work

have no clue how to fix it

Dim ws As Worksheet
Dim tmp As Worksheet
Dim acct As String

Set ws = Sheets("Booking")
Set tmp = ThisWorkbook.Worksheets.Add
tmp.Activate

On Error Resume Next
AppActivate "BTS", True
On Error GoTo 0
Sleep 1000

On Error Resume Next

SendKeys "MRGN", True 'enter margin
Sleep 500
SendKeys "^", True 'reset
Sleep 500
SendKeys "40911025", True ' enter account number
SendKeys "{F3}", True 'get balance secreen
Sleep 750 'waits X seconds
SendKeys "^{INSERT}", True 'copy
Sleep 100
tmp.Activate

tmp.Range("A1").Select
tmp.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False

        ws.Range("I7") = Trim(Left(tmp.Range("A7"), 13)) 'GetEquity
        ws.Range("I9") = Trim(Left(tmp.Range("A9"), 13)) 'GetMM
        ws.Range("I11") = Trim(Left(tmp.Range("A11"), 13)) 'GetDebt
        ws.Range("I13") = Trim(Left(tmp.Range("A13"), 13)) 'GetL250

Application.DisplayAlerts = False

tmp.Delete

Application.DisplayAlerts = True
Application.ScreenUpdating = True


Sheets("Booking").Activate

it works, but I need to run it twice.

1

There are 1 answers

3
Patrick O'Brien On

If you need the macro to loop twice, set up a separate subroutine in the same module and just call the main subroutine twice. Kind of a brute force way to do it though, so I'd recommend not doing this often.

call main_subroutine_name() 
call main_subroutine_name()

Your issue however appears to be with the data that you're pasting. Are you trying to paste values, and where are you trying to paste the data? If you're trying to paste the data in range "A1," try:

range("A1").pastespecial ...

Since you already activated the specific sheet, you shouldn't need to specify the sheet as the region in which you're trying to paste, and can rather just reference cell "A1" as shown above.