+ Reply to Thread
Results 1 to 7 of 7

Record data from one worksheet to another based on conditions

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-28-2011
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    114

    Exclamation Record data from one worksheet to another based on conditions

    Hi all

    I have macro button that records the data from one worksheet into another. My coding is in a total mess right now! I have the following worksheets:

    1 - Input - The information is entered in the template

    2 - View - The information is copied through from a macro button based on the order id and Category - I have attached an example which may not make sense but may give the idea to what i'm trying to achieve.

    Thanks.
    Attached Files Attached Files
    Last edited by sgp; 12-13-2011 at 08:58 AM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Record data from one worksheet to another based on conditions

    Sub Button4_Click()
    Dim source As Worksheet
    Dim cellCat As Range
    Dim cellOrder As Range
    
        Set source = Worksheets("Input")
                       
        With Worksheets("View")
        
            Set cellCat = .Columns(1).Find(source.Range("B3").Value)
            If Not cellCat Is Nothing Then
            
                Set cellOrder = .Rows(2).Find(source.Range("B2").Value)
                If Not cellOrder Is Nothing Then
                
                    .Cells(cellCat.Row, cellOrder.Column).Value = source.Range("B5").Value
                    .Cells(cellCat.Row + 1, cellOrder.Column).Value = source.Range("B6").Value
                    .Cells(cellCat.Row + 2, cellOrder.Column).Value = source.Range("B7").Value
                End If
            End If
        End With
    End Sub
    Haven't we already done this one?

  3. #3
    Forum Contributor
    Join Date
    10-28-2011
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    114

    Re: Record data from one worksheet to another based on conditions

    Thanks Bob Phillips!

    I did do something similar to this but it was through userforms.

  4. #4
    Forum Contributor
    Join Date
    10-28-2011
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    114

    Re: Record data from one worksheet to another based on conditions

    Just a quick one - is there a way to inform the user that the data has been transfered? i.e. msgbox

    Regards.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Record data from one worksheet to another based on conditions

    Should the data be copied to columns E, F & G of your View tab?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Record data from one worksheet to another based on conditions

    Sub Button4_Click()
    Dim source As Worksheet
    Dim cellCat As Range
    Dim cellOrder As Range
    
        Set source = Worksheets("Input")
                       
        With Worksheets("View")
        
            Set cellCat = .Columns(1).Find(source.Range("B3").Value)
            If Not cellCat Is Nothing Then
            
                Set cellOrder = .Rows(2).Find(source.Range("B2").Value)
                If Not cellOrder Is Nothing Then
                
                    .Cells(cellCat.Row, cellOrder.Column).Value = source.Range("B5").Value
                    .Cells(cellCat.Row + 1, cellOrder.Column).Value = source.Range("B6").Value
                    .Cells(cellCat.Row + 2, cellOrder.Column).Value = source.Range("B7").Value
                    
                    MsgBox "Data transferred for Category " _
                         & source.Range("B3").Value _
                         & ", Order #" & source.Range("B2").Value, vbOKOnly, "Data Transfer"
                End If
            End If
        End With
    End Sub

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Record data from one worksheet to another based on conditions

    Never saw the replies, I'll post it for what it's worth.
    Here's another way
    Run this with your button
    Sub TransferData()
        Dim RowNo As Long, ColNo As Long
        Dim rngRow As Range, rngCol As Range
        Dim wsInput As Worksheet, wsView As Worksheet
        
        Set wsInput = Sheets("Input")
        Set wsView = Sheets("View")
        
        If wsInput.Cells(2, "B") = "" Or wsInput.Cells(3, "B") = "" Then
            MsgBox "Incomplete Data", vbCritical
            Exit Sub
        End If
        
        On Error Resume Next
        With WorksheetFunction
            RowNo = .Match(wsInput.Range("B3"), wsView.Range("A:A"), 0)
            Err.Clear
            ColNo = .Match(wsInput.Range("B2"), wsView.Range("2:2"), 0)
            Err.Clear
        End With
        If RowNo = 0 Then
            MsgBox "Category not found", vbCritical
            Exit Sub
        End If
        If ColNo = 0 Then
            MsgBox "Order Number not found", vbCritical
            Exit Sub
        End If
        For n = 0 To 2
            wsView.Cells(RowNo + n, ColNo) = wsInput.Cells(5 + n, "B")
        Next
        MsgBox "Order Transferred", vbInformation
        
    End Sub
    Last edited by Marcol; 12-13-2011 at 09:19 AM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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