+ Reply to Thread
Results 1 to 2 of 2

Error in code to skip cells between entry fields - help needed!

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    1

    Exclamation Error in code to skip cells between entry fields - help needed!

    I've taken over management of a spreadsheet essentially used to log the comming and going of phsyical files.
    The main problem I can't seem to fix lies with Marcos in the 'entry' page laid out for users to enter the data, i.e data, author, etc - a code used to skip the cells between entry fields.

    (there is a cell seperating entry fields below one another for asthetics only)

    the code currently sits as follows:

    ' oldCell variable used to store the cell which has focus at the end of the macro below
    Public oldCell As Range

    Private Sub Worksheet_Activate()
    Range("E10").Select
    Set oldCell = Cells.Range("E10")
    End Sub

    ' RUN EVERY TIME A NEW CELL IS SELECTED IN THE Add Patient sheet.
    '
    ' Used to skip the cells between the entry fields
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    ' Variables for the loop
    Dim x As Integer
    Dim y As Integer
    y = 9 ' As in one below the first entry field
    ' Iterate through the number of entry cells
    For x = 0 To (UBound(CellsToAdd) - 1)
    ' If we are on a cell which is between an entry cell then...
    If Target.Address = Range("E" & CStr(y)).Address Then
    ' If we came from the entry cell above then...
    If oldCell.Address = Range("E" & CStr(y - 1)).Address Then
    ' Change focus to the next entry cell down (1 cell down)
    Range("E" & CStr(y + 1)).Select
    ' If we came from the entry cell below then...
    ElseIf oldCell.Address = Range("E" & CStr(y + 1)).Address Then
    ' Change focus to the previous entry cell (1 cell down)
    Range("E" & CStr(y - 1)).Select
    End If
    End If
    ' Increase y by 2 (cell spacing in this case)
    y = y + 2
    Next
    ' Set the oldCell variable to the current sell ready for when the macro is next called
    Set oldCell = ActiveCell
    End Sub

    (the bold red writing being the point of error - runs a error '9' - subscript out of range)

    Any help is much appreciated, I'm at a dead end!! The only ideas I can think of is that I might have to define CellsToAdd?

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    re: Error in code to skip cells between entry fields - help needed!

    Welcome to the Board, however, please note:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here


    Please edit your prior post in accordingly

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1