+ Reply to Thread
Results 1 to 8 of 8

Automatically lookup and set an empty cell's value if adjacent cell is typed & vice versa

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-30-2014
    Location
    USA
    MS-Off Ver
    Version 14.4.3, Mac 2011
    Posts
    145

    Automatically lookup and set an empty cell's value if adjacent cell is typed & vice versa

    2 Sheets: "Database" and "Client Codes"
    On Sheet Database,
    Cell B4 is where one would type a Code Number.
    Cell C4 is where one would type the corresponding Client Name.
    On Sheet Client Codes,
    Column A2 and down are Code Numbers.
    Column B2 and down are corresponding Client Names.

    I am hoping to make a Worksheet_Change that looks at Sheet Database Cells B4 and C4 and uses a Index match function to lookup the corresponding Number/Name from Sheet Client Codes.

    I started with a Sub button to simply see if I could make it work. The Index Match formula is returning a 1004 error. The next step would be to make it a Worksheet_Change private sub that makes things simple for someone entering data.

    Sub Auto_Fill()
    Dim lastRow, CodeToClient As Long, ClientToCode As Long
    lastRow = Worksheets("Client Codes").Cells(Rows.Count, 1).End(xlUp).Row
    
    With Application.WorksheetFunction
    CodeToClient = .Index(Sheets("Client Codes").Range("A2:A" & lastRow), _
                              .Match(Sheets("Database").Range("B4").Value, Sheets("Client Codes").Range("B2:B" & lastRow)))
    ClientToCode = .Index(Sheets("Client Codes").Range("B2:B" & lastRow), _
                              .Match(Sheets("Database").Range("C4").Value, Sheets("Client Codes").Range("A2:A" & lastRow)))
    End With
    
    If Range("B4").Value <> "" Then
        If Range("C4").Value <> "" Then
            MsgBox "B4 <> blank, C4 <> blank, Exit Sub"
        Else
            MsgBox "B4 <> blank, C4 = blank," & vbNewLine _
                & "Do C4 = CodeToClient.Value" 'B4 value dictates C4
        End If
    Else
        If Range("C4").Value <> "" Then
            MsgBox "B4 = blank, C4 <> blank," & vbNewLine _
            & "Do B4 = ClientToCode.Value" 'C4 value dictates B4
        Else
            MsgBox "B4 = blank, C4 = blank, Exit Sub"
        End If
    End If
    
    End Sub
    Thanks for taking the time to look,
    Red

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Automatically lookup and set an empty cell's value if adjacent cell is typed & vice ve

    Need to rewrite.
    Last edited by walruseggman; 05-21-2015 at 04:38 PM.

  3. #3
    Forum Contributor
    Join Date
    07-30-2014
    Location
    USA
    MS-Off Ver
    Version 14.4.3, Mac 2011
    Posts
    145

    Re: Automatically lookup and set an empty cell's value if adjacent cell is typed & vice ve

    Quote Originally Posted by walruseggman View Post
    Need to rewrite.
    Thanks I'll try :/

  4. #4
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,792

    Re: Automatically lookup and set an empty cell's value if adjacent cell is typed & vice ve

    I made up some sheets and data and couldn't get it to work with your code. I did have success after making some changes. See the code below.

    Sub Auto_Fill()
    Dim lastRow, CodeToClient As Long, ClientToCode As Long, wsCC As Worksheet, wsDB As Worksheet
    lastRow = Worksheets("Client Codes").Cells(Rows.Count, 1).End(xlUp).Row
    
    Set wsCC = Worksheets("Client Codes")
    Set wsDB = Worksheets("Database")
    
    
    CodeToClient = WorksheetFunction.Index(wsCC.Range("A2:A" & lastRow), _
    WorksheetFunction.Match(wsDB.Range("B4").Value, wsCC.Range("B2:B" & lastRow), 0))

    EDIT

    I also had success evaluating your formula this way:

    CodeToClient = Evaluate("=INDEX('Client Codes'!A2:A" & lastRow & ",MATCH(Database!B4,'Client Codes'!B2:B" & lastRow & ",0))")
    Last edited by skywriter; 05-21-2015 at 08:01 PM.

  5. #5
    Forum Contributor
    Join Date
    07-30-2014
    Location
    USA
    MS-Off Ver
    Version 14.4.3, Mac 2011
    Posts
    145

    Re: Automatically lookup and set an empty cell's value if adjacent cell is typed & vice ve

    I tried both methods and am stil finding a Run-time error 1004, Method 'Match' of object 'WorksheetFunction' Failed
    gah

    I'm reading it may have something to do with the range having over 912 characters which is interesting.
    Last edited by RedSummer; 05-22-2015 at 09:56 AM.

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,792

    Re: Automatically lookup and set an empty cell's value if adjacent cell is typed & vice ve

    Quote Originally Posted by RedSummer View Post
    I tried both methods and am stil finding a Run-time error 1004, Method 'Match' of object 'WorksheetFunction' Failed
    gah

    I'm reading it may have something to do with the range having over 912 characters which is interesting.
    Post a spreadsheet.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Automatically lookup and set an empty cell's value if adjacent cell is typed & vice ve

    Maybe:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x As Range
    If Not Intersect(Target, Range("B4:C4")) Is Nothing Then
        Select Case Target.Column
            Case Is = 2
                With Sheets("Client Codes")
                    Set x = .Columns(1).Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
                    If Not x Is Nothing Then
                        Target.Offset(, 1) = x.Offset(, 1)
                    End If
                    Set x = Nothing
                End With
            Case Is = 3
                With Sheets("Client Codes")
                    Set x = .Columns(2).Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
                    If Not x Is Nothing Then
                        Target.Offset(, -1) = x.Offset(, -1)
                    End If
                    Set x = Nothing
                End With
        End Select
    End If
    End Sub

  8. #8
    Forum Contributor
    Join Date
    07-30-2014
    Location
    USA
    MS-Off Ver
    Version 14.4.3, Mac 2011
    Posts
    145

    Re: Automatically lookup and set an empty cell's value if adjacent cell is typed & vice ve

    The sheet is a little sensitive and making a dummy one would take a considerable amount of time but thank you guys for your assistance thus far!
    John I think nailed it, ditching the INDEX MATCH entirely and just going for a find. It exceeds my expectations :D
    Definitely a lot easier to comprehend than making the worksheet formulas work.
    Best,
    Red

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA code to change info in one cell so that other cell changes, and vice versa
    By tarab in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-21-2016, 12:57 PM
  2. Replies: 1
    Last Post: 08-12-2014, 03:07 PM
  3. How do I make one cell blank if another cell has data in it and vice-versa?
    By Nosilla in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2014, 01:21 PM
  4. [SOLVED] Make value in cell A1 dependant on value in cell B1 and vice versa
    By tanktata in forum Excel General
    Replies: 9
    Last Post: 03-29-2012, 06:11 PM
  5. Page name from cell or vice versa
    By Audiguy82 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-19-2008, 06:21 PM

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