Consulting

Results 1 to 9 of 9

Thread: Fix Broken Code

  1. #1
    VBAX Contributor
    Joined
    Nov 2015
    Location
    Minnesota
    Posts
    101
    Location

    Fix Broken Code

    I could use some help fixing my broken code, there are some missing pieces that I'm not quite sure how to code. I try and explain what I am trying to accomplish in my code given below.

    I am using two named ranges ([OIB_STD_OPS] & [Product_Line_To_Dept_Num]). I defined these using Name Manager. [OIB_STD_OPS] has duplicate data. I do not want these added line after line, but I do want the number captured in column D (See image below).
    One thing to note is that the array [OIB_STD_OPS] will not always be sorted by department sequence number.

    Capture.JPG

    Sub Quick_Ref()
    Dim i
    Dim j
    Dim k 'Selected Department
    Dim d As Range 'Number of departments
    Dim rd 'Number of rows with selected departments in named range [OIB_STD_OPS] (Could have multiple departments in product line)
    
    d = Range("[Product_Line_To_Dept_Num]").Rows.Count
    rd = ??? 'Number of rows with selected departments in named range [OIB_STD_OPS] (Could have multiple departments in product line)
    
    For j = 1 To d
        k = WorksheetFunction.HLookup(Range("B1"), [Product_Line_To_Dept_Num], j + 1, 0)
        
            For i = 1 To rd
                Range("Cells(3+i, 1)") = ??? '"Code" with selected "variable k" department
                Range("Cells(3+i, 2)") = WorksheetFunction.VLookup(Range("Cells(3+i, 1)"), [OIB_STD_OPS], 2, 0)
                Range("Cells(3+i, 3)") = WorksheetFunction.VLookup(Range("Cells(3+i, 1)"), [OIB_STD_OPS], 3, 0)
                Range("Cells(3+i, 4)") = ??? '# of rows "Code:(Cells(3+i, 1)" appears in [OIB_STD_OPS]
            Next i
    Next j
    End Sub
    '--------------------------
    Sub Delete_Ref()
    ???
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    You didn't say what part wasn't working

    That's not really the way I prefer to use named ranges. This is a little simpler

    I put data in A1:A10 and called it 'TestName'


     
    Sub TestNames()
        Dim i As Long
        
        MsgBox Range("TestName").Address
        MsgBox [TestName].Address
        MsgBox Range("TestName").Cells(3, 1).Value
    
        'fails ------------------------------------------------
    '    For i = 1 To 10
    '        MsgBox Range("Cells(i, 1)")
    '    Next I
    
        For i = 1 To 10
            MsgBox Cells(i, 1).Value
            Cells(i, 1).Value = Cells(i, 1).Value & "abcdef"
        Next i
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb() 
      sn=[OIB_STD_OPS].resize(,[OIB_STD_OPS].columns.count+1)
      
      With CreateObject("scripting.dictionary")
         For j = 1 To UBound(sn)
           sp = Application.Index(sn, j)
           If .exists(sn(j, 1)) Then sp(UBound(sp)) = .Item(sn(j, 1))(UBound(sp)) - (.Item(sn(j, 1))(UBound(sp)) = "") + 1
           .Item(sn(j, 1)) = sp
         Next
         
         Cells(20, 1).Resize(.count, UBound(sn, 2)) = Application.Index(.items, 0, 0)
      End With
    End Sub

  4. #4
    VBAX Contributor
    Joined
    Nov 2015
    Location
    Minnesota
    Posts
    101
    Location
    Thank you for the reply.
    Technically, none of it works. I haven't been able to test it because I don't know how to define "rd".

    But all the code with ???, I need help with.

    Quote Originally Posted by Paul_Hossler View Post
    You didn't say what part wasn't working

    That's not really the way I prefer to use named ranges. This is a little simpler

    I put data in A1:A10 and called it 'TestName'


      
     Sub TestNames()
         Dim i As Long
         
         MsgBox Range("TestName").Address
         MsgBox [TestName].Address
         MsgBox Range("TestName").Cells(3, 1).Value
    
         'fails ------------------------------------------------
     '    For i = 1 To 10
     '        MsgBox Range("Cells(i, 1)")
     '    Next I
    
         For i = 1 To 10
             MsgBox Cells(i, 1).Value
             Cells(i, 1).Value = Cells(i, 1).Value & "abcdef"
         Next i
    
     End Sub

  5. #5
    VBAX Contributor
    Joined
    Nov 2015
    Location
    Minnesota
    Posts
    101
    Location
    Thanks for the reply.
    This code kind of works. It does retrieve data from my [OIB_STD_OPS] table. However, it does not filter based on named range [Product_Line_To_Dept_Num]. Also, the data retrieved shows nearly all my columns in [OIB_STD_OPS], when I only want the few specified above in my code.

    Quote Originally Posted by snb View Post
    Sub M_snb() 
      sn=[OIB_STD_OPS].resize(,[OIB_STD_OPS].columns.count+1)
      
      With CreateObject("scripting.dictionary")
         For j = 1 To UBound(sn)
           sp = Application.Index(sn, j)
           If .exists(sn(j, 1)) Then sp(UBound(sp)) = .Item(sn(j, 1))(UBound(sp)) - (.Item(sn(j, 1))(UBound(sp)) = "") + 1
           .Item(sn(j, 1)) = sp
         Next
         
         Cells(20, 1).Resize(.count, UBound(sn, 2)) = Application.Index(.items, 0, 0)
      End With
    End Sub
    Last edited by Nick72310; 02-22-2016 at 02:12 PM.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You have my permission to adapt the code to your requirements.

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    You have declared "d" as a Range, but are using it as a number.
    You have declared "k" as a Variant, and set it to a number or a numerical string (?), then you don't use it.

    I, for one, cannot figure out either the structure of your Named Ranges, or what you are trying to do with them.

    Maybe you are trying to list the unique "Codes" (Cut, Sand, & Fin) and the Departments they belong to (1234 & 43231), and the number of instances each "Code" is used.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Contributor
    Joined
    Nov 2015
    Location
    Minnesota
    Posts
    101
    Location
    k is indented to be used in the first line of my for I= 1 to rd loop. (As noted in my comment within the code)

    My named ranges are just tables. [OIB_STD_OPS] is a table with my "Code" in the first column. The same code I want to retrieve, like seen in the image above. [Product_Line_To_Dept_Num] is a table that has the product lines in the first row, and the corresponding numbers below them. So referring to my image, Cell B1 is the product line I want to look up in named range [Product_Line_To_Dept_Num]. In return, it will retrieve department numbers 1234 & 4321 (See image).

    I want the retrieved code to be based on the selected department, from [Product_Line_To_Dept_Num] (Cell B1). So every code with that department number should be retrieved.

    Quote Originally Posted by SamT View Post
    You have declared "d" as a Range, but are using it as a number.
    You have declared "k" as a Variant, and set it to a number or a numerical string (?), then you don't use it.

    I, for one, cannot figure out either the structure of your Named Ranges, or what you are trying to do with them.

    Maybe you are trying to list the unique "Codes" (Cut, Sand, & Fin) and the Departments they belong to (1234 & 43231), and the number of instances each "Code" is used.

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Please stop quoting every post.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

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