+ Reply to Thread
Results 1 to 10 of 10

Help with fixing match problem in code

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Help with fixing match problem in code

    Hi

    I have code that is not writing the expected result and so I could really use some help with making the necessary adjustments. I don’t want to change the code.

    Using the Excel image below the expected written result is the grey highlight in the D5:H17 box area. The data gets written based on any set of numbers in the range E6:E17 and comes from the data source cells M5 to O17. So as an example when cell E8 (3rd line down) has the 10-1 in it the code would search the data source (3rd line down) and write from the data source cells M8/N8/O8 to cells F8/G8/H8. I have also uploaded my Excel workbook.

    Thanks so much for any help.
    Sub PlaceNumbers()
    
    Dim c As Range, rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
    Dim last1 As Long, last2 As Long, rtar As Long, xtar As Long
     
    Application.ScreenUpdating = False
    
    With ActiveSheet
        'create arrays
        arr1 = Array(.Range("D5:H17"))
        arr2 = Array(.Range("L5:O17))                                                             '
        'loop through arrays
        For i = LBound(arr1) To UBound(arr1)
            Set rng1 = arr1(i)
            Set rng3 = arr2(i)                                                      
            last1 = .Cells(.Rows.Count, ColLetter(rng1.Columns(1).Column)).End(xlUp).Row
            last2 = .Cells(.Rows.Count, ColLetter(rng3.Columns(1).Column)).End(xlUp).Row
     
            For Each c In rng1.Offset(1, 1).Resize(, 1)
                If c <> "" Then
                    rtar = Evaluate("=MATCH(" & ColLetter(rng1.Columns(2).Column) & rng1.Row & "&" & ColLetter(rng1.Columns(3).Column) & rng1.Row & "," & ColLetter(rng3.Columns(1).Column) & "1:" & ColLetter(rng3.Columns(1).Column) & last2 & "&" & ColLetter(rng3.Columns(3).Column) & "1:" & ColLetter(rng3.Columns(3).Column) & last2 & ",0)")
                    xtar = Application.Match(c.Offset(0, -2), Range(ColLetter(rng3.Columns(1).Column) & rtar & ":" & ColLetter(rng3.Columns(1).Column) & last2), 0)
                    With Application.WorksheetFunction
                        c.Offset(0, 1) = .Index(Range(ColLetter(rng3.Columns(2).Column) & rtar & ":" & ColLetter(rng3.Columns(2).Column) & last2), xtar)
                        c.Offset(0, 2) = .Index(Range(ColLetter(rng3.Columns(3).Column) & rtar & ":" & ColLetter(rng3.Columns(3).Column) & last2), xtar)
                        c.Offset(0, 3) = .Index(Range(ColLetter(rng3.Columns(4).Column) & rtar & ":" & ColLetter(rng3.Columns(4).Column) & last2), xtar)
                    End With
                End If
            Next c
        Next
    End With
    
    Application.ScreenUpdating = True
     
    End Sub
     
    Function ColLetter(Collet As Integer) As String
    
    ColLetter = Split(Cells(1, Collet).Address, "$")(1)
    
    End Function

    WRITE PROBLEM.png
    Attached Files Attached Files

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: Help with fixing match problem in code

    Quick question (I don't understand fully this code), if in E8 will be something else than 10-1, do you want to still copy M8:O8 into F8:H8?
    Is it any relation between values in col E or just if it is not empty do copies?
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Help with fixing match problem in code

    Why do you need all that VBA - couldn't you just use some simple formulae?

  4. #4
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Help with fixing match problem in code

    Hi

    Thanks for your replies. To KOKOSEK question on something other than 10-1. It could be any set of numbers 99-8 or whatever is the criteria for the code to write the numbers from the data source. On "relation between values in col E or just if it is not empty do copies? " if the cell range is empty no writing from the data source occurs.

    To StephanR: Need all this VBA because this is just one example of data. When you have maybe a hundred or more in the future it's better to have code. Also other issues.

  5. #5
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: Help with fixing match problem in code

    So, if it does not matter what's in E run by this:

    Sub macro1()
    Dim cell As Range
    For Each cell In Range("E6:E17")  'create range as you need
        If cell.Value <> "" Then
            cell.Offset(0, 1).Value = cell.Offset(0, 8).Value
            cell.Offset(0, 2).Value = cell.Offset(0, 9).Value
            cell.Offset(0, 3).Value = cell.Offset(0, 10).Value
        End If
    Next cell
    End Sub
    a bit shorter and works

  6. #6
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Help with fixing match problem in code

    I really appreciate your help but as I said in my post I really have to use the posted code. I just have to get it to write to the proper cells. My very small example is just that. My real sheet is massive and I'll have hundreds at these of this. This code will allow me to expand my data boxes.

    Thanks
    Last edited by CHRISTINEKENDALL93; 04-18-2019 at 11:54 AM. Reason: spell

  7. #7
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: Help with fixing match problem in code

    I'll try to analyse your code but it is quite hard especially if it does not working.

  8. #8
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Help with fixing match problem in code

    I think it's the match that's the issue.

    Thanks so much

  9. #9
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: Help with fixing match problem in code

    I was trying trace variables but:

    that's xtar:
    Formula: copy to clipboard

    $E$8 =Match(,L5:L5,0)
    $E$14 =Match(,L5:L5,0)

    obviosly gives error because whole col C is empty.

    that's rtar:
    Formula: copy to clipboard

    $E$8 =MATCH(E5&F5,L1:L5&N1:N5,0)
    $E$14 =MATCH(E5&F5,L1:L5&N1:N5,0)

    put this formula into cell, giving also error.

    I don't know how you get this code, but sorry I give up.

  10. #10
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Help with fixing match problem in code

    Hi KOKOSEK

    I tried but it didn't work. Thanks so much for your attempt. Please close this posting.

+ 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. Create MsgBox If Cell is already populated
    By MariPip in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2014, 07:27 PM
  2. Matching data between two sheets
    By MariPip in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-09-2014, 06:24 PM
  3. Need HELP fixing this code
    By Dewbo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-29-2014, 09:16 AM
  4. [SOLVED] Fixing the VBA code
    By brownie4321 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-24-2012, 09:35 AM
  5. help fixing a calculation problem
    By barrfly in forum Excel General
    Replies: 3
    Last Post: 11-04-2005, 12:10 PM
  6. fixing problem with my tab
    By Zane in forum Excel General
    Replies: 2
    Last Post: 08-06-2005, 03:05 PM
  7. fixing code
    By timmy64 - ExcelForums.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2005, 04:48 AM

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