+ Reply to Thread
Results 1 to 9 of 9

Loop issues - Find value in range that fits value in defined name range

Hybrid View

Sc0tt1e Loop issues - Find value in... 10-31-2014, 09:30 AM
xlbiznes Re: Loop issues - Find value... 10-31-2014, 09:53 AM
pareshj Re: Loop issues - Find value... 10-31-2014, 09:54 AM
Sc0tt1e Re: Loop issues - Find value... 10-31-2014, 10:07 AM
pareshj Re: Loop issues - Find value... 10-31-2014, 10:12 AM
Sc0tt1e Re: Loop issues - Find value... 10-31-2014, 10:48 AM
pareshj Re: Loop issues - Find value... 10-31-2014, 11:14 AM
Sc0tt1e Re: Loop issues - Find value... 10-31-2014, 11:19 AM
Sc0tt1e Re: Loop issues - Find value... 10-31-2014, 11:33 AM
  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Loop issues - Find value in range that fits value in defined name range

    I have a ton of data to filter and copy out to new workbooks based on a portfolio code.

    I am attempting to loop through "rng" which is the unique numbers from the data, and I am looking it up in "wsd" which is the raw data sheets colum E which holds each lines portfolio code.

    This is my first attempt at a loop with vba and I'm having an issue with looking up a range and referencing it against another range. The below code is giving me a runtime error 13 "Type Mismatch" message but I can't figure out why. I have a few ideas but none I can work out how to create a fix. Any suggestions as always welcomed

    Dim spath As String
    Dim wb2 As Workbook
    Dim wsd As Range, rng As Range
    
    Sub Portfolio()
    
    Application.ScreenUpdating = False
    
    Set rng = Sheets("Stats").Range("K2:K3")
    Set wsd = Sheets("Data").Range("E:E")
    spath = "K:\Project Rainbow 3\08. Design\00. Nov2012 Mobilisation\02. FO & C\02 - Project Documents\2013 Branch & Directs RSK 340\02. PROJECT DOCUMENTS\Bank Manager\Private Banking\PF_Cleanse\"
    
    
    '   Start Loop
    For Each row In rng
    
    '   Searches for portfolio number in data and copies line if found
        If wsd = rng Then        Selection.End(xlToLeft).Select
                Selection.Copy
        End If
        
        '   Need to insert if rng value not found in wds then next row else keep running code
    
    '   Pastes values in new workbook
    Set wb2 = Workbooks.Add
            With wb2
        Sheets("Sheet1").Range("A:A").End (xlUp) - 1
            .Value2 = .Value2
            End With
    
        wb2.Close
        wb2.SaveAs Filename:=spath & rng & ".xlsx"
    
    
    Next row
    
    Application.ScreenUpdating = True
    
    End Sub
    At the moment I am testing but when done K2:K3 becomes "portf" which is a defined name range of 200 odd lines in the G column of sheets "Stats"
    Last edited by Sc0tt1e; 10-31-2014 at 09:35 AM.

  2. #2
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Loop issues - Find value in range that fits value in defined name range

    Hi,

    try this approach of using ado to extract based on a sql statement.
    Attached Files Attached Files
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  3. #3
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Loop issues - Find value in range that fits value in defined name range

    Hi,

    You cannot compare two range like this. You need to check cells wise. You can do something like below:


    For Each cell1 In range1  
            For Each cell2 In range2  
                If cell1 = cell2 Then  
                   '' DO what you want here
                End If  
            Next y  
        Next x

    Regards,
    Paresh J
    Last edited by pareshj; 10-31-2014 at 10:12 AM.
    Click on "* Add Reputation" as a way to say thanks

  4. #4
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Loop issues - Find value in range that fits value in defined name range

    Paresh

    Just to confirm,

    I want row one of rng to run through every row of "wds" then once the end has been reached row 2 or "rng" will run through every row of "wds" and so on and so on until "rng" has been exhausted.

    Does the code you have provided loop in this order?

    By the way you have put
    If cell1 = cell1 Then
    would one of them not need to be cell2?

  5. #5
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Loop issues - Find value in range that fits value in defined name range

    Yes, exactly it runs the same way as you said. And my apologies, it should be
    cell1 = cell2
    Regards,
    Paresh J

  6. #6
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Loop issues - Find value in range that fits value in defined name range

    I'm trying to paste the values copied within the loop to no avail, any ideas of the correct syntax or method?

    Set wb2 = Workbooks.Add
    
            With wb2
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
    
        wb2.Close
        wb2.SaveAs Filename:=spath & rng & ".xlsx"

  7. #7
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Loop issues - Find value in range that fits value in defined name range

    What exactly you want to paste. Is it a range of cells or single cell?

    As your code has changed, you need to modify this part too.

    What you need to do is get the union of range of cells when your are in for loop and select that range for copy and the your further code will work.

    Regards,
    Paresh J

  8. #8
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Loop issues - Find value in range that fits value in defined name range

    I want to run through "rng" one line at a time and have it run through all of "wsd", I want one book to be opened and whenever I have a match between "rng" value and a value in "wds" I want it to copy the line and paste it into wb2.

    When the loop has run through all of "wsd" I want the wb2 book to be closed and saved in the "spath" location with the name of the "rng".xlsx value that was being searched.

  9. #9
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    Re: Loop issues - Find value in range that fits value in defined name range

    Code as is stands

    Dim spath As String
    Dim wb2 As Workbook
    Dim ws2 As Worksheet
    Dim wsd As Range, rng As Range
    
    Sub Portfolio()
    
    Application.ScreenUpdating = False
    
    Set rng = Sheets("Stats").Range("K2:K3")
    Set wsd = Sheets("Data").Range("E:E")
    spath = "K:\Project Rainbow 3\08. Design\00. Nov2012 Mobilisation\02. FO & C\02 - Project Documents\2013 Branch & Directs RSK 340\02. PROJECT DOCUMENTS\Bank Manager\Private Banking\PF_Cleanse\"
    
    
    '   Start Loop
    For Each row In rng
        Set wb2 = Workbooks.Add
            For Each row1 In wsd
    
    '   Searches for portfolio number in data and copies line if found
        If row = row1 Then
            Selection.End(xlToLeft).Select
                Selection.Copy
        End If
        
        '   Need to insert if rng value not found in wds then next row else keep running code
    
    '   Pastes values in new workbook
            With wb2
                Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
            
        Next row1
    
            wb2.Close
            wb2.SaveAs Filename:=spath & rng & ".xlsx"
    
    Next row
    
    Application.ScreenUpdating = True
    
    End Sub

+ 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. [SOLVED] Formula to determine if a number fits in a range
    By hwishman in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-03-2014, 06:06 PM
  2. Find most recent date within a defined range of dates
    By xltbob in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 12-10-2012, 02:21 PM
  3. Loop through worksheets which are defined in a range of cells
    By OilMan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-23-2010, 02:37 AM
  4. help to create loop in a defined range
    By azegurb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-10-2009, 03:24 AM
  5. Find names not in defined range
    By Chuck N in forum Excel General
    Replies: 1
    Last Post: 06-08-2007, 09:16 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