+ Reply to Thread
Results 1 to 12 of 12

Having cells change their contents according to what the contents of other cells are

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2008
    Posts
    21

    Having cells change their contents according to what the contents of other cells are

    I have a column in my .XLS whose contents I want to change depending on what the contents of the cells of a different column are. However, I only want two cells from the same row to be dependent on each other. So, for example, if $A$1 reads "1" then $B$1 should read "apple" and if $A$2 reads "1" then $B$2 should read "apple"; if $A$12 reads "3" then $B$12 should read "pavement" etc.

    Any help would be appreciated!

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I think you need to look at one of these Functions - VLOOKUP or CHOOSE.

    See

    http://www.excel-it.com/excel_functions.htm
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    03-24-2008
    Location
    Calabar, Nigeria
    MS-Off Ver
    2003, 2007
    Posts
    37
    I hope you are familiar with VBA. Copy and paste the following code in the code module for the sheet you are working with (right click the sheet tab and select 'View Code'). Change the X_ and Y_ Values correspondingly as desired. You may also change the column index for the X_ and Y_Values by changing the const declaration values of lX_Col and lY_Col

    Option Explicit
    
    Private Const lX_Col As Long = 1, lY_Col As Long = 2
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Select Case Target.Column
        Case lX_Col
            Dim lThisIndex As Long, varThisY As Variant
            If Exists(Target, X_Values, lThisIndex) And Not IsEmpty(Target) Then
                varThisY = Y_Values(lThisIndex)
                Me.Cells(Target.Row, lY_Col) = varThisY
            Else
                Me.Cells(Target.Row, lY_Col) = Empty
            End If
        End Select
    End Sub
    
    Private Function Exists _
        ( _
            ByVal element As Variant, _
            SingleRankArray As Variant, _
            Optional ByRef ZeroBasedIndex As Variant = Empty _
        ) _
    As Boolean
        Exists = False
        If IsArray(SingleRankArray) Then
            Dim lUbound As Long, lLBound As Long, lCount As Long
            Dim ThisEle As Variant
            lUbound = UBound(SingleRankArray): lLBound = LBound(SingleRankArray)
            For lCount = lLBound To lUbound
                ThisEle = SingleRankArray(lCount)
                If element = ThisEle Then
                    ZeroBasedIndex = lCount - lLBound
                    Exists = True
                    Exit Function
                End If
            Next lCount
        End If
    End Function
    
    Private Function X_Values() As Variant
        X_Values = Array(0, 1, 5, 9, 19)
    End Function
    
    Private Function Y_Values()
        Y_Values = Array("Zero", "One", "Five", "Nine", "Nineteen")
    End Function
    Last edited by nsenor udofa; 03-25-2008 at 08:24 AM.

  4. #4
    Registered User
    Join Date
    01-17-2008
    Posts
    21
    Thanks for the help

  5. #5
    Registered User
    Join Date
    01-17-2008
    Posts
    21
    Edit: NVM, see this.
    Last edited by I Am Herenow; 03-25-2008 at 12:27 PM.

  6. #6
    Registered User
    Join Date
    03-24-2008
    Location
    Calabar, Nigeria
    MS-Off Ver
    2003, 2007
    Posts
    37

    Creating Hyperlinks in Excel VBA etc.

    This is the code for your worksheet that I think you need.

    The validation on activation will help you or your user select the proper text.
    In addition to the X_ and Y_ value functions, the Link_Values function is also introduced to hold the hyperlink addresses. The Y_Links are derived from the title of the link web pages.

    I hope you understand the logic. ENJOY!

    Option Explicit
    
    Private Const lX_Col As Long = 5, lY_Col As Long = 7
    
    Private Sub Worksheet_Activate()
        Dim rngValidate As Excel.Range
        Set rngValidate = Me.Columns(lX_Col)
        With rngValidate.Validation
            .Delete
            .Add xlValidateList, xlValidAlertInformation, , Join(X_Values, ",")
            .IgnoreBlank = True
            .InCellDropdown = True
            .ShowInput = False
            .ShowError = False
        End With
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rngY As Excel.Range
        Select Case Target.Column
        Case lX_Col
            Dim lThisIndex As Long, varThisY As Variant, varThisLink
            If Exists(Target, X_Values, lThisIndex) And Not IsEmpty(Target) Then
                varThisY = Y_Values(lThisIndex)
                varThisLink = Link_Values(lThisIndex)
                Set rngY = Me.Cells(Target.Row, lY_Col)
                rngY = varThisLink
                With rngY.Hyperlinks
                    .Delete
                    .Add rngY, varThisLink, , _
                        "Click to go to tips on " & CStr(Target), _
                        varThisY
                End With
                Target.EntireColumn.AutoFit
                rngY.EntireColumn.AutoFit
            Else
                Me.Cells(Target.Row, lY_Col) = Empty
            End If
        End Select
    End Sub
    
    Private Function Exists _
        ( _
            ByVal element As Variant, _
            SingleRankArray As Variant, _
            Optional ByRef ZeroBasedIndex As Variant = Empty _
        ) _
    As Boolean
        Exists = False
        If IsArray(SingleRankArray) Then
            Dim lUbound As Long, lLBound As Long, lCount As Long
            Dim ThisEle As Variant
            lUbound = UBound(SingleRankArray): lLBound = LBound(SingleRankArray)
            For lCount = lLBound To lUbound
                ThisEle = SingleRankArray(lCount)
                If element = ThisEle Then
                    ZeroBasedIndex = lCount - lLBound
                    Exists = True
                    Exit Function
                End If
            Next lCount
        End If
    End Function
    
    Private Function X_Values() As Variant
        X_Values = Array _
        ( _
            "Additional Mathematics", _
            "Biology", _
            "Chemistry", _
            "English Language", _
            "English Literature", _
            "French", _
            "History", _
            "Latin", _
            "Mathematics", _
            "Physics", _
            "Religious Studies", _
            "Spanish" _
        )
    End Function
    
    Private Function Y_Values()
        Y_Values = Array _
        ( _
            "OCR>Qualifications>Free Standing Maths...", _
            "Edexcel International Qualification: GCSE", _
            "Edexcel International Qualification: GCSE", _
            "AQA GCSE English B Specs...", _
            "AQA GCSE English Lit...", _
            "AQA GCSE French Assesment Materials", _
            "OCR>Qualification>GCSE>History B...", _
            "OCR>Qualification>GCSE>Latin...", _
            "Edexcel International Qualification: GCSE", _
            "Edexcel International Qualification: GCSE", _
            "Edexcel International Qualification: GCSE", _
            "AQA GCSE Spanish Assesment Materials..." _
        )
    End Function
    
    Private Function Link_Values()
        Link_Values = Array _
        ( _
            "http://www.ocr.org.uk/qualifications/freestandingmathsquals" & _
                  "/additional_mathematics/documents.html#past_papers", _
            "http://www.edexcel-international.org/quals/igcse/4325/", _
            "http://www.edexcel-international.org/quals/igcse/4335/", _
            "http://www.aqa.org.uk/qual/gcse/eng_b_assess.php", _
            "http://www.aqa.org.uk/qual/gcse/eng_lit_b_assess.php", _
            "http://www.aqa.org.uk/qual/gcse/french_a_assess.php", _
            "http://www.ocr.org.uk/qualifications/gcse/history_b_modern_world/", _
            "http://www.ocr.org.uk/qualifications/gcse/latin/documents.html", _
            "http://www.edexcel-international.org/quals/igcse/4400/", _
            "http://www.edexcel-international.org/quals/igcse/4420/", _
            "http://www.edexcel.org.uk/quals/gcse/rs/gcse/1481/", _
            "http://www.aqa.org.uk/qual/gcse/span_a_assess.php" _
        )
    End Function
    Last edited by nsenor udofa; 03-26-2008 at 06:02 AM.

  7. #7
    Registered User
    Join Date
    01-17-2008
    Posts
    21
    Thanks for the help, but at the moment "Private Function Link_Values()" all shows up red and it gives me a syntax error when I try to use the code.

  8. #8
    Registered User
    Join Date
    03-24-2008
    Location
    Calabar, Nigeria
    MS-Off Ver
    2003, 2007
    Posts
    37
    Add a double quote to the begining of line 5 of the procedure. Or copy and paste the code again. I have corrected the error.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I can't see why you need VBA to do this. A simple VLOOKUP or CHOOSE would work.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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