Changing Sheets

AccessShell

Board Regular
Joined
Sep 5, 2015
Messages
50
Office Version
  1. 2003 or older
Platform
  1. Windows
I am running Excel on Win 10 Home
In VBA code I am trying to change to a different sheet.

Here is the code
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i            As Integer
Dim j            As Integer
Dim dteExDivDate As Date
Dim curCashAmt   As Currency
Dim dteRecDate   As Date
Dim dtePayDate   As Date
Dim strSymbol    As String
Dim intLastRow   As Integer

    If Target.Address = "$A$1" Then       
            For i = 2 To 24
                Sheets("Dividends").Select
                strSymbol = Range("H" & i).Value
                dteExDivDate = Range("C" & i).Value
                curCashAmt = Range("D" & i).Value
                dteRecDate = Range("E" & i).Value
                dtePayDate = Range("F" & i).Value
                
                [B][SIZE=5]Sheets(strSymbol).Select[/SIZE][/B]
                Debug.Print Range("A" & 7).Value
                intLastRow = Range("A" & Rows.Count).End(xlUp).Row
                For j = 7 To intLastRow
                    If Range("A" & j).Value = dtePayDate Then
                        Range("R" & j).Value = dteExDivDate
                        Range("S" & j).Value = curCashAmt
                        Range("T" & j).Value = dteRecDate
                        Range("U" & j).Value = dtePayDate
                    End If
                Next j
            Next i
    End If
    
End Sub
The problem occurs on the highlighted line. The sheet selected does appear on the screen. But the next 2 lines give results for the previous sheet (Dividends).
Can anyone explain what I am doing wrong?

Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Because the code is in a sheet module any unqualified ranges will refer to the sheet containing the code & not the active.
Therefore you need to qualify the ranges with the correct sheet.
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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