Solved

VBA to get details from sheet2

Posted on 2014-10-10
7
535 Views
Last Modified: 2014-10-15
I have attached a file that hopefully will assist with my question.

I need to copy certain data from Sheet2 to Sheet1 but only where the cells in Column P equal -2 and there are two rules. 1) where there is a single Reference in column C in Sheet2 and 2) where there is a duplicate ref in column C

From Row11 in Sheet2

Only where there is a -2 in column P

If there is a single references in column C in Sheet2  then :
Populate column B in Sheet1 with the reference from Column C from Sheet2
Populate column C in Sheet1 with the data from Column T from Sheet2
Populate column D in Sheet1 with the data from Column Q from Sheet2
Populate column E in Sheet1 with the data from Column R from Sheet2
Populate column G with 0
Populate column J in Sheet1 with the data from Column U from Sheet2

If there are two references the same in column C in Sheet2 then [from the first Ref row in Sheet2]
Populate column B in Sheet1 with the reference from Column C from Sheet2
Populate column C in Sheet1 with the data from Column T from Sheet2
Populate column D in Sheet1 with the data from Column Q from Sheet2
Populate column E in Sheet1 with the data from Column R from Sheet2
Populate column G in Sheet1 with the data from Column Q from Sheet2
Populate column J in Sheet1 with the data from Column U from Sheet2

thank you in advance
0
Comment
Question by:Jagwarman
  • 4
7 Comments
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40373677
looks confusing to me.
0
 

Author Comment

by:Jagwarman
ID: 40373928
and you're the expert how do you think I feel
0
 

Author Comment

by:Jagwarman
ID: 40373975
This may make it a little clearer I have attached a new file with Sheet1 completed

Also, with regards to the calculated field the calculation is not yet agreed with the user and I will add those later so they should not be part of the VBA

Many thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Jagwarman
ID: 40373977
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 500 total points
ID: 40381326
OK.  I think I understand this, but it will get more complicated if my assumptions are incorrect.

Assumption 1:  All duplicates will be sorted together.  i.e. I only need to check the line below the current line to look for a duplicate.

Assumption 2:  That duplicate lines will always have the same value in Column P

If so, then you can try this code:

Public Sub CopyToSheet1()
    ' ExpertExchange Question ID 28535327
    ' http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28535327.html
    ' Expert: ltlbearand3 [http://www.experts-exchange.com/M_2469312.html]
    '
    Dim intStartRow As Integer
    Dim intEndRow As Integer
    Dim rngCurrent As Range
    Dim i As Integer
    
    Application.ScreenUpdating = False
    
    ' Because of the nature of your data, we have to be a little tricky in grabbing the data
    ' Set our starting Row as 11
    intStartRow = "11"
    
    ' Then find the last row of data
    ' Set Column and starting cell values
    Worksheets("Sheet2").Select
    intEndRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    ' Find the last used row on Sheet 1
    Set rngCurrent = Worksheets("Sheet1").UsedRange.End(xlDown).Offset(1)
    
    ' Loop Through Each row to look at the Data
    ' Using a For loop so we can skip the next line if a duplicate
    For i = intStartRow To intEndRow
        ' Check the Value of Column P
        If Range("P" & i).Value = -2 Then
            
            ' Copy Column C Sheet 2 to Column B Sheet 1
            rngCurrent.Value = Range("C" & i).Value
            
            ' Copy Column T Sheet 2 to Column C Sheet 1
            rngCurrent.Offset(ColumnOffset:=1).Value = Range("T" & i).Value
            
            ' Copy Column Q Sheet 2 to Column D Sheet 1
            rngCurrent.Offset(ColumnOffset:=2).Value = Range("Q" & i).Value
            
            ' Copy Column R Sheet 2 to Column E Sheet 1
            rngCurrent.Offset(ColumnOffset:=3).Value = Range("R" & i).Value
            
            ' Check if this is a duplicate
            ' And Copy the Correct Value to Column G in Sheet 1
            If Range("C" & i).Value = Range("C" & i + 1).Value Then
                rngCurrent.Offset(ColumnOffset:=5).Value = Range("Q" & i).Value
                
                ' Skip the next row
                i = i + 1
            Else
                rngCurrent.Offset(ColumnOffset:=5).Value = 0
            End If
            
            ' Copy Column U Sheet 2 to Column J in Sheet 1
            rngCurrent.Offset(ColumnOffset:=8).Value = Range("U" & i).Value
            
            ' Move to Next row on Sheet 1
            Set rngCurrent = rngCurrent.Offset(1)
        End If
    Next i
    
    Debug.Print "Done"
    
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:Jagwarman
ID: 40381671
ltlbearand that is perfect many thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now