How to prevent the sheet from changing whenever the codes refresh

whcmelvin

Board Regular
Joined
Jul 27, 2011
Messages
82
Hi, this is urgent. how do i prevent the sheets from changing whenever the codes below runs? the code below will run every 5 seconds. but whenever it runs, it will go to EmailSheetAuto. i had tried not to select the sheet at the beginning but it does not work. Please help. I am stuck here for hours already.
I would appreciate your help very much.

Code:
Dim TimeToRun

Sub ScheduleCopyPriceOver()
    Application.ScreenUpdating = False
    TimeToRun = Now + TimeValue("00:00:05")
    Application.OnTime TimeToRun, "CopyPriceOver"
End Sub

Sub CopyPriceOver()

    Dim r As Range
    Dim n As Long
    Dim NextRow As Long
        Application.ScreenUpdating = False
    EmailSheetAuto = Sheets("Trading Platform").LoginName.Value
    Sheets(EmailSheetAuto).Select

    NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
    
    Set r = Range(Cells(2, 1), Cells(NextRow, 1))
    
    For n = 2 To r.Rows.Count
        If Sheets("Trading Platform").LoginName.Value = Sheets("Login Details").Cells(2, 17) Then
            If Sheets(EmailSheetAuto).Cells(n, 2).Value = "EUR/USD" Then

                Sheets(EmailSheetAuto).Cells(n, 9) = "hi"
                
            End If
            
        End If
    Next n

    Call ScheduleCopyPriceOver

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
try
Code:
Sub CopyPriceOver()
Dim r As Range
Dim n As Long
Dim NextRow As Long
Application.ScreenUpdating = False
EmailSheetAuto = Sheets("Trading Platform").LoginName.Value
With Sheets(EmailSheetAuto)
    NextRow = Application.WorksheetFunction.CountA(.Range("A:A")) + 1
    Set r = .Range(Cells(2, 1), .Cells(NextRow, 1))
    For n = 2 To r.Rows.Count
        If Sheets("Trading Platform").LoginName.Value = Sheets("Login Details").Cells(2, 17) Then
            If .Cells(n, 2).Value = "EUR/USD" Then
                .Cells(n, 9) = "hi"
            End If
        End If
    Next n
End With
Call ScheduleCopyPriceOver
End Sub
 
Upvote 0
hi, it did refresh, but it only refresh the first row when i am at other sheets. it will refresh everything when it is at the EmailSheetAuto. do you know why is it happening this way?
 
Upvote 0
Hi, the code i am using now is this.... the rest is still the same.
Code:
    Dim r As Range
    Dim n As Long
    Dim NextRow As Long
    Application.ScreenUpdating = False
    EmailSheetAuto = Sheets("Trading Platform").LoginName.Value
    With Sheets(EmailSheetAuto)
        NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
    
        Set r = Range(Cells(2, 1), Cells(NextRow, 1))
    
        For n = 2 To r.Rows.Count
            If Sheets("Trading Platform").LoginName.Value = Sheets("Login Details").Cells(2, 17) Then
                If Sheets(EmailSheetAuto).Cells(n, 2).Value = "EUR/USD" Then
                    If Sheets(EmailSheetAuto).Cells(n, 3).Value = "Buy" Then
                        PPandLL = 1467
                        Sheets(EmailSheetAuto).Cells(n, 10).Value = PPandLL
                    End If
        
                End If
            
            End If
        Next n
    End With
 
Upvote 0
You haven't copied properly.
Hi, the code i am using now is this.... the rest is still the same.
Code:
    Dim r As Range
    Dim n As Long
    Dim NextRow As Long
    Application.ScreenUpdating = False
    EmailSheetAuto = Sheets("Trading Platform").LoginName.Value
    With Sheets(EmailSheetAuto)
        NextRow = Application.WorksheetFunction.CountA([B][SIZE=6][COLOR=Red].[/COLOR][/SIZE][/B]Range("A:A")) + 1
    
        Set r = [B][SIZE=6][COLOR=Red].[/COLOR][/SIZE][/B]Range([B][SIZE=6][COLOR=Red].[/COLOR][/SIZE][/B]Cells(2, 1), [B][SIZE=6][COLOR=Red].[/COLOR][/SIZE][/B]Cells(NextRow, 1))
    
        For n = 2 To r.Rows.Count
            If Sheets("Trading Platform").LoginName.Value = Sheets("Login Details").Cells(2, 17) Then
                If [B][SIZE=6][COLOR=Red].[/COLOR][/SIZE][/B]Cells(n, 2).Value = "EUR/USD" Then
                    If [B][SIZE=6][COLOR=Red].[/COLOR][/SIZE][/B]Cells(n, 3).Value = "Buy" Then
                        PPandLL = 1467
                        [B][SIZE=6][COLOR=Red].[/COLOR][/SIZE][/B]Cells(n, 10).Value = PPandLL
                    End If
        
                End If
            
            End If
        Next n
    End With
 
Upvote 0
hi, sorry for not copying correctly. but it still didnt help.
My code now is
Code:
    Dim r As Range
    Dim n As Long
    Dim NextRow As Long
    Application.ScreenUpdating = False
    EmailSheetAuto = Sheets("Trading Platform").LoginName.Value
    With Sheets(EmailSheetAuto)
        NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
    
        Set r = .Range(.Cells(2, 1), .Cells(NextRow, 1))
    
        For n = 2 To r.Rows.Count
            If Sheets("Trading Platform").LoginName.Value = Sheets("Login Details").Cells(2, 17) Then
                If .Cells(n, 2).Value = "EUR/USD" Then
                    If .Cells(n, 3).Value = "Buy" Then
                        PPandLL = .Cells(n, 5) * 100000 * (Sheets("Currency").Cells(2, 2).Value - .Cells(n, 8).Value)
                        .Cells(n, 10).Value = PPandLL
                    End If

            
            
            
            
                End If
            
            End If
        Next n
    End With
 
Upvote 0
Sigh…
you're STILL not copying properly;
NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
should be
NextRow = Application.WorksheetFunction.CountA(.Range("A:A")) + 1
 
Upvote 0
Oh My Gosh! I am really sorry. i am too stress on my project on vba. really really sorry. but, thanks for your help. =) it worked perfectly fine now.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top