+ Reply to Thread
Results 1 to 5 of 5

Copy each row data of row headers with same value in one column. Help?

Hybrid View

zoenightshade Copy each row data of row... 05-05-2015, 09:17 AM
simarui Re: Copy each row data of row... 05-05-2015, 09:38 AM
zoenightshade Re: Copy each row data of row... 05-05-2015, 09:55 AM
simarui Re: Copy each row data of row... 05-05-2015, 10:32 AM
zoenightshade Re: Copy each row data of row... 05-05-2015, 10:39 AM
  1. #1
    Registered User
    Join Date
    05-02-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    47

    Copy each row data of row headers with same value in one column. Help?

    So first I will choose the source file. Say I want to enter a name from the given sample. Then, I want to take all the amount and purpose under that name and paste it to the corresponding cells in the worksheet where i'm running vba. I dont know how to modify the code. Any help or leads on how I will start? Thanks in advance.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    01-10-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    651

    Re: Copy each row data of row headers with same value in one column. Help?

    hi zoenightshade,

    i don't understand what you're trying to accomplish? this code looks like it's working fine (in fact i learned a few things while reading it). in what way are you trying to modify it?
    Hope I could help - if your post has been solved don't forget to mark it as such.

    Clicking the * below is a great way to say thanks!

    "Drowning in 10 feet of water isn?t any different than drowning in a million feet. And if you can swim, it doesn?t matter how deep the ocean is. At some level, once you realize you?re in water that?s too deep to stand, you have to have a very different approach," - Joi Ito

  3. #3
    Registered User
    Join Date
    05-02-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    47

    Re: Copy each row data of row headers with same value in one column. Help?

    hi! thank you for responding. If you havent noticed, for example, in column C, i have multiple Rose Dix. However, the values of amount and purpose of each Rose Dix varies in every row. I want to get the other two values of amount and purpose and paste it in the corresponding cells instead of just the first one.

  4. #4
    Valued Forum Contributor
    Join Date
    01-10-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    651

    Re: Copy each row data of row headers with same value in one column. Help?

    ah! i missed that. looks like you'll need the findall function from our good friend Chip Pearson (who's website seems to be down, or perhaps my company firewall is blocking it otherwise i'd post the link... google chip pearson findall for his site and description of the code)

    paste this into module 1:
    Function FindAll(SearchRange As Range, _
                    FindWhat As Variant, _
                   Optional LookIn As XlFindLookIn = xlValues, _
                    Optional LookAt As XlLookAt = xlWhole, _
                    Optional SearchOrder As XlSearchOrder = xlByRows, _
                    Optional MatchCase As Boolean = False, _
                    Optional BeginsWith As String = vbNullString, _
                    Optional EndsWith As String = vbNullString, _
                    Optional BeginEndCompare As VbCompareMethod = vbTextCompare) As Range
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' FindAll
    ' This searches the range specified by SearchRange and returns a Range object
    ' that contains all the cells in which FindWhat was found. The search parameters to
    ' this function have the same meaning and effect as they do with the
    ' Range.Find method. If the value was not found, the function return Nothing. If
    ' BeginsWith is not an empty string, only those cells that begin with BeginWith
    ' are included in the result. If EndsWith is not an empty string, only those cells
    ' that end with EndsWith are included in the result. Note that if a cell contains
    ' a single word that matches either BeginsWith or EndsWith, it is included in the
    ' result.  If BeginsWith or EndsWith is not an empty string, the LookAt parameter
    ' is automatically changed to xlPart. The tests for BeginsWith and EndsWith may be
    ' case-sensitive by setting BeginEndCompare to vbBinaryCompare. For case-insensitive
    ' comparisons, set BeginEndCompare to vbTextCompare. If this parameter is omitted,
    ' it defaults to vbTextCompare. The comparisons for BeginsWith and EndsWith are
    ' in an OR relationship. That is, if both BeginsWith and EndsWith are provided,
    ' a match if found if the text begins with BeginsWith OR the text ends with EndsWith.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Dim FoundCell As Range
    Dim FirstFound As Range
    Dim LastCell As Range
    Dim ResultRange As Range
    Dim XLookAt As XlLookAt
    Dim Include As Boolean
    Dim CompMode As VbCompareMethod
    Dim Area As Range
    Dim MaxRow As Long
    Dim MaxCol As Long
    Dim BeginB As Boolean
    Dim EndB As Boolean
    
    
    CompMode = BeginEndCompare
    If BeginsWith <> vbNullString Or EndsWith <> vbNullString Then
        XLookAt = xlPart
    Else
        XLookAt = LookAt
    End If
    
    ' this loop in Areas is to find the last cell
    ' of all the areas. That is, the cell whose row
    ' and column are greater than or equal to any cell
    ' in any Area.
    
    For Each Area In SearchRange.Areas
        With Area
            If .Cells(.Cells.Count).Row > MaxRow Then
                MaxRow = .Cells(.Cells.Count).Row
            End If
            If .Cells(.Cells.Count).Column > MaxCol Then
                MaxCol = .Cells(.Cells.Count).Column
            End If
        End With
    Next Area
    Set LastCell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)
    
    On Error GoTo 0
    Set FoundCell = SearchRange.Find(What:=FindWhat, _
            After:=LastCell, _
            LookIn:=LookIn, _
            LookAt:=XLookAt, _
            SearchOrder:=SearchOrder, _
            MatchCase:=MatchCase)
    
    If Not FoundCell Is Nothing Then
        Set FirstFound = FoundCell
        Do Until False ' Loop forever. We'll "Exit Do" when necessary.
            Include = False
            If BeginsWith = vbNullString And EndsWith = vbNullString Then
                Include = True
            Else
                If BeginsWith <> vbNullString Then
                    If StrComp(Left(FoundCell.Text, Len(BeginsWith)), BeginsWith, BeginEndCompare) = 0 Then
                        Include = True
                    End If
                End If
                If EndsWith <> vbNullString Then
                    If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, BeginEndCompare) = 0 Then
                        Include = True
                    End If
                End If
            End If
            If Include = True Then
                If ResultRange Is Nothing Then
                    Set ResultRange = FoundCell
                Else
                    Set ResultRange = Application.Union(ResultRange, FoundCell)
                End If
            End If
            Set FoundCell = SearchRange.FindNext(After:=FoundCell)
            If (FoundCell Is Nothing) Then
                Exit Do
            End If
            If (FoundCell.Address = FirstFound.Address) Then
                Exit Do
            End If
    
        Loop
    End If
        
    Set FindAll = ResultRange
    
    End Function
    then you have to make just a few tweaks to your original code, this seems to be working:
    Private Sub CommandButton1_Click()
    
    If TextBox1.Text = "" Then
        MsgBox "Please enter a name."
    Else
        If IsNumeric(TextBox1.Text) Then
            MsgBox "Please enter a valid name!"
                Else
                    Dim Source As String
                    Dim fnd As Range
                    Dim rng As Range
                    Dim i As Long
                    i = 0
                                    
                    Source = UserForm1.Label2
                    Set wb = Workbooks.Open(Source)
                    Set sht = wb.Sheets("Sheet1")
                    Set fnd = FindAll(sht.Range("C:C"), TextBox1.Value)
                         If Not fnd Is Nothing Then
                             For Each rng In Range(fnd.Address)
                             i = i + 1
                             r = rng.Row: UserForm2.Hide
                             Dim wt As Worksheet: Set wt = ThisWorkbook.Sheets("Sheet1")
                             wt.Cells(5, 2) = sht.Cells(r, 1)
                             wt.Cells(5, 5) = sht.Cells(r, 2)
                             wt.Cells(6, 2) = sht.Cells(r, 3)
                             wt.Cells(7 + i, 2) = sht.Cells(r, 5)
                             wt.Cells(7 + i, 4) = sht.Cells(r, 4)
                             wt.Cells(23, 4) = sht.Cells(r, 6)
                             wt.Cells(5, 5).NumberFormat = "m/d/yyyy"
                             wt.Cells(7 + i, 4).NumberFormat = "0,000.00"
                             Next rng
                         Else: MsgBox "Customer not found": End If
                         End If
    End If
        wb.Close False
    End Sub
    couple things i'm noticing - there doesn't appear to be any place to store the invoice #, date or person approved for the subsequent instances of Rose Dix... this code is currently capturing the last instance... is that ok?

  5. #5
    Registered User
    Join Date
    05-02-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    47

    Re: Copy each row data of row headers with same value in one column. Help?

    no, sir! it's definitely fine! all i need is the subsequent amount and purpose. im gonna try it later. thank you so much sir!
    Last edited by zoenightshade; 05-09-2015 at 01:54 AM.

+ 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. Help required with Macro to Copy Data based on Column Headers - please!
    By Andycostin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-02-2014, 05:45 PM
  2. Replies: 2
    Last Post: 09-18-2014, 05:42 AM
  3. [SOLVED] How to Copy a filter data with headers, excluding the headers?
    By Elomaldo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-16-2014, 03:35 PM
  4. [SOLVED] VBA code to copy column data only if headers match
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2012, 11:23 PM
  5. Match Headers from raw worksheet and copy all data from that column
    By tms12 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2012, 08:00 AM

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