Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 1st, 2013, 11:36 AM
Registered User
 
Join Date: Oct 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default VBA Extract & Paste

Hi, I wonder whether someone may be able to help me please.

I'm using the script below to copy data from one 'Source' sheet ("All Data"), creating a unique distinct list and pasting the values to several 'Destination' sheets.

Code:
Sub Extract()

    Dim i As Long, j As Long, m As Long, strProject As String, RLOB As String, RDate As Date, RVal As Single
    Dim BlnProjExists As Boolean, ws As Worksheet, DI As Worksheet, EH As Worksheet, IND As Worksheet, OVH As Worksheet, PRO As Worksheet, LastRow As Long
    Const StartRow As Long = 5
    
    Application.ScreenUpdating = False

    Set DI = Sheets("Direct Activities")
    Set EH = Sheets("Enhancements")
    Set IND = Sheets("Indirect Activities")
    Set OH = Sheets("Overheads")
    Set PRO = Sheets("Projects")
    
    With Sheets("AllData").Range("E3")
        For i = 1 To .CurrentRegion.Rows.Count - 1
            strProject = .Offset(i, 0)
            RDate = .Offset(i, 3)
            RVal = .Offset(i, 4)
            RLOB = .Offset(i, -3)
         If InStr(.Offset(i, 0), "Enhancements") > 0 And RVal > 0 Then
                strProject = .Offset(i, 0)
    
            With EH.Range("B1")
                If .CurrentRegion.Rows.Count = 1 Then
                    .Offset(1, 0) = strProject
                    j = 1
                Else
                    BlnProjExists = False
                        For j = 1 To .CurrentRegion.Rows.Count - 1
                            If .Offset(j, 0) = strProject Then
                                BlnProjExists = True
                                Exit For
                            End If
                        Next j
                            If BlnProjExists = False Then
                               .Offset(j, 0) = strProject
                            End If
                End If
                Select Case Format(RDate, "mmm yy")
                    Case "Apr 13"
                        m = 1
                    Case "May 13"
                        m = 2
                    Case "Jun 13"
                        m = 3
                    Case "Jul 13"
                        m = 4
                    Case "Aug 13"
                        m = 5
                    Case "Sep 13"
                        m = 6
                    Case "Oct 13"
                        m = 7
                    Case "Nov 13"
                        m = 8
                    Case "Dec 13"
                        m = 9
                    Case "Jan 14"
                        m = 10
                    Case "Feb 14"
                        m = 11
                    Case "Mar 14"
                        m = 12
                End Select
                  .Offset(j, m) = .Offset(j, m) + RVal
            End With
    
           ElseIf InStr(.Offset(i, 0), "DIR") > 0 And RVal > 0 Then
            strProject = .Offset(i, -1)
            With DI.Range("B1")
                If .CurrentRegion.Rows.Count = 1 Then
                    .Offset(1, 0) = strProject
                    j = 1
                Else
                    BlnProjExists = False
                    For j = 1 To .CurrentRegion.Rows.Count - 1
                        If .Offset(j, 0) = strProject Then
                            BlnProjExists = True
                            Exit For
                        End If
                    Next j
                        If BlnProjExists = False Then
                          .Offset(j, 0) = strProject
                        End If
                End If
                Select Case Format(RDate, "mmm yy")
                    Case "Apr 13"
                        m = 1
                    Case "May 13"
                        m = 2
                    Case "Jun 13"
                        m = 3
                    Case "Jul 13"
                        m = 4
                    Case "Aug 13"
                        m = 5
                    Case "Sep 13"
                        m = 6
                    Case "Oct 13"
                        m = 7
                    Case "Nov 13"
                        m = 8
                    Case "Dec 13"
                        m = 9
                    Case "Jan 14"
                        m = 10
                    Case "Feb 14"
                        m = 11
                    Case "Mar 14"
                        m = 12
                End Select
                 .Offset(j, m) = .Offset(j, m) + RVal
            End With
    
          ElseIf InStr(.Offset(i, 0), "IND") > 0 And RVal > 0 Then
            strProject = .Offset(i, -1)
            With IND.Range("B1")
                If .CurrentRegion.Rows.Count = 1 Then
                    .Offset(1, 0) = strProject
                    j = 1
                Else
                    BlnProjExists = False
                    For j = 1 To .CurrentRegion.Rows.Count - 1
                        If .Offset(j, 0) = strProject Then
                            BlnProjExists = True
                            Exit For
                        End If
                    Next j
                        If BlnProjExists = False Then
                          .Offset(j, 0) = strProject
                        End If
                End If
                Select Case Format(RDate, "mmm yy")
                    Case "Apr 13"
                        m = 1
                    Case "May 13"
                        m = 2
                    Case "Jun 13"
                        m = 3
                    Case "Jul 13"
                        m = 4
                    Case "Aug 13"
                        m = 5
                    Case "Sep 13"
                        m = 6
                    Case "Oct 13"
                        m = 7
                    Case "Nov 13"
                        m = 8
                    Case "Dec 13"
                        m = 9
                    Case "Jan 14"
                        m = 10
                    Case "Feb 14"
                        m = 11
                    Case "Mar 14"
                        m = 12
                End Select
                 .Offset(j, m) = .Offset(j, m) + RVal
            End With
            
          ElseIf InStr(.Offset(i, 0), "OVH") > 0 And RVal > 0 Then
            strProject = .Offset(i, -1)
            With OH.Range("B1")
                If .CurrentRegion.Rows.Count = 1 Then
                    .Offset(1, 0) = strProject
                    j = 1
                Else
                    BlnProjExists = False
                    For j = 1 To .CurrentRegion.Rows.Count - 1
                        If .Offset(j, 0) = strProject Then
                            BlnProjExists = True
                            Exit For
                        End If
                    Next j
                        If BlnProjExists = False Then
                          .Offset(j, 0) = strProject
                        End If
                End If
                Select Case Format(RDate, "mmm yy")
                    Case "Apr 13"
                        m = 1
                    Case "May 13"
                        m = 2
                    Case "Jun 13"
                        m = 3
                    Case "Jul 13"
                        m = 4
                    Case "Aug 13"
                        m = 5
                    Case "Sep 13"
                        m = 6
                    Case "Oct 13"
                        m = 7
                    Case "Nov 13"
                        m = 8
                    Case "Dec 13"
                        m = 9
                    Case "Jan 14"
                        m = 10
                    Case "Feb 14"
                        m = 11
                    Case "Mar 14"
                        m = 12
                End Select
                 .Offset(j, m) = .Offset(j, m) + RVal
            End With
    
       ElseIf InStr(.Offset(i, 0), "DIR") + InStr(.Offset(i, 0), "Enhancements") + InStr(.Offset(i, 0), "IND") + InStr(.Offset(i, 0), "OVH") = 0 And RVal > 0 Then
          strProject = .Offset(i, 0)
          With PRO.Range("B1")
              If .CurrentRegion.Rows.Count = 1 Then
                  .Offset(1, 0) = strProject
                  j = 1
              Else
                  BlnProjExists = False
                  For j = 1 To .CurrentRegion.Rows.Count - 1
                      If .Offset(j, 0) = strProject Then
                          BlnProjExists = True
                          Exit For
                      End If
                  Next j
                      If BlnProjExists = False Then
                        .Offset(j, 0) = strProject
                      End If
              End If
              Select Case Format(RDate, "mmm yy")
                  Case "Apr 13"
                      m = 1
                  Case "May 13"
                      m = 2
                  Case "Jun 13"
                      m = 3
                  Case "Jul 13"
                      m = 4
                  Case "Aug 13"
                      m = 5
                  Case "Sep 13"
                      m = 6
                  Case "Oct 13"
                      m = 7
                  Case "Nov 13"
                      m = 8
                  Case "Dec 13"
                      m = 9
                  Case "Jan 14"
                      m = 10
                  Case "Feb 14"
                      m = 11
                  Case "Mar 14"
                      m = 12
              End Select
               .Offset(j, m) = .Offset(j, m) + RVal
          End With
                
         End If
        Next i
    End With
        
        For Each ws In Worksheets(Array("Direct Activities", "Enhancements", "Indirect Activities", "Overheads", "Projects"))
            LastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
            If LastRow >= StartRow Then
                ws.Range("B5:B" & LastRow).NumberFormat = "@"
                ws.Range("C5:P" & LastRow).NumberFormat = "0.00"
                ws.Range("C5:P" & LastRow).HorizontalAlignment = xlCenter
                ws.Range("O5:P" & LastRow).Font.Bold = True
                ws.Range("O5:O" & LastRow).FormulaR1C1 = "=Sum(RC3:RC14)/24"
                ws.Range("P5:P" & LastRow).FormulaR1C1 = "=RC15/7.24"
            End If
            ws.Columns("B:Q").AutoFit
            
        Next ws
     Application.ScreenUpdating = True
    End Sub
To be more specific, the script looks at column E of the' Source' sheet for a number of differing values which are "DIR", "Enhancements", "IND", "OVH" and finally any value which is not the aforementioned 4 values.

DIR
When this value is found in column E, the script moves one column to the left, copies the value creates a unique distinct list for values with multiple occurrences and pastes this to the "Direct Activities" sheet.

In addition, it also sums a 'Man hour' figure from column I of the 'Source' sheet and pastes these figures under the relevant monthly heading.

Enhancements

When this value is found in column E, the script copies the value creates a unique distinct list for values with multiple occurrences and pastes this to the "Enhancements" 'Destination sheet.

As before the script sums a 'Man hour' figure from column I of the 'Source' sheet and pastes these figures under the relevant monthly heading on the 'Enhancements" 'Destination' sheet.

IND
When this value is found in column E, the script moves one column to the left, copies the value creates a unique distinct list for values with multiple occurrences and pastes this to the "Indirect Activities" sheet.

As with the other two values a 'Man hour' figure is summed from column I of the 'Source' sheet and pastes these figures under the relevant monthly heading.

OVH
When this value is found in column E, the script moves one column to the left, copies the value creates a unique distinct list for values with multiple occurrences and pastes this to the "Overheads" sheet.

Again, in addition a 'Man hour' figure is summed from column I of the 'Source' sheet and pastes these figures under the relevant monthly heading.

Projects

This element of the script is a little different. When a value which does not equal "DIR", "Enhancements", "IND" or "OVH" value is found in column E, the script copies the value creates a unique distinct list for values with multiple occurrences and pastes this to the "Projects" 'Destination sheet.

Again, the the script also sums a 'Man hour' figure from column I of the 'Source' sheet and pastes these figures under the relevant monthly heading on the "Projects" 'Destination' sheet.

I'd now like to change this, so instead of the unique list being created on just one column, I'd like this to occur on multiple columns, but still keep the functionality which sums the 'Man hour' figure.

DIR
When this value is found in column E, I'd like to create the unique distinct lists using the value in columns "D" and "B" from the 'Source' sheet, pasting this information into columns "B" and "C" on the 'Direct Activities in addition to the summed 'Man hour' figure'.

Enhancements
When this value is found in column E, I'd like to create the unique distinct lists using the value in columns "E" and "B" from the 'Source' sheet, pasting this information into columns "B" and "C" in addition to the summed 'Man hour' figure'.

IND
When this value is found in column E, I'd like to create the unique distinct lists using the value in columns "D" and "B" from the 'Source' sheet, pasting this information into columns "B" and "C" in addition to the summed 'Man hour' figure'.

OVH
When this value is found in column E, I'd like to create the unique distinct lists using the value in columns "D" and "B" from the 'Source' sheet, pasting this information into columns "B" and "C" in addition to the summed 'Man hour' figure'.

Projects
Again,as before I'd like to still search for the values which does not equal "DIR", "Enhancements", "IND" or "OVH" value is found in column E, but I'd like to create the unique distinct lists using the value in columns "E" and "B" and F from the 'Source' sheet, pasting this information into columns "B" and "C" and D, in addition to the summed 'Man hour' figure'.

I've been trying for some time now to make the changes as above, but without any luck.

I just wondered whether someone may be able to look at this please and offer some guidance on how I may go about achieving this.

I appreciate that this is a long post with a lot of detail, so please find the link to the file here https://www.dropbox.com/s/5xjqno4fzm...%20Module.xlsm which may perhaps illustrate the current and desired functionality a little better than my explanation.

I have set up the "All Data" source sheet, and then the 'Destination' sheets which are pertinent to the current script. If you click on the button on the "Macros" sheet, you can run the macro.

In addition, I have also included the sheets which show how I'd like the information to be extracted using the revised code. These are shown as "....Output"

Many thanks and kind regards
 
Old December 6th, 2013, 08:47 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Hope you had solved it by now. If not, please post one problem at a time so that others are not discouraged by a larger problem

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VBA copy & paste wensmail Excel VBA 1 September 17th, 2007 11:13 PM
Excel VBA Problems with copy and paste bripers Excel VBA 6 September 1st, 2006 05:14 AM
Copy contents of Word doc & Paste in Excel - How?? robear Javascript How-To 1 August 23rd, 2006 03:22 PM
How to Copy contents of Word doc & Paste in Excel robear Pro PHP 0 August 21st, 2006 12:25 PM
Disabling copy/paste in both IE & Netscape Mekala HTML Code Clinic 0 July 2nd, 2004 04:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.