Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-20-2024, 06:50 AM
Logit Logit is offline Copy/Paste Range Values To Other Sheet Windows 10 Copy/Paste Range Values To Other Sheet Office 2007
Expert
Copy/Paste Range Values To Other Sheet
 
Join Date: Jan 2017
Posts: 591
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default Copy/Paste Range Values To Other Sheet

Seeking help with code modification to perform the following ...

- Create individual sheets for each Sr. Director (1:6) ... Existing code works.

The following is the code portion that doesn't function as desired ...

- Copy "Cost Center Sheets" values, Column B of individual Sr. Director sheet and


paste to appropriate Sr. Director Sheet one range below the other with a row
separating each.

Repeat the process for all sheets listed in Column B, then move to the next Sr. Director sheet and repeat the process until all Sr. Director sheets have been processed.

Here is the macro code and the section of concern is positioned between the two lines of asterisks :

Code:
Option Explicit

Sub FilterAndCopyUniqueTerms()
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim lastRow As Long
    Dim uniqueTerms As Object
    Dim term As Variant
    Dim rng As Range
    Dim cell As Range
    
    'turn off as much background processes as possible
    'turn off as much background processes as possible
    With Excel.Application
            .ScreenUpdating = False
            .Calculation = Excel.xlCalculationManual
            .EnableEvents = False
    End With
    
    
    Set wsSource = ThisWorkbook.Sheets("Cost Centers")
    
    lastRow = wsSource.Cells(wsSource.Rows.Count, "D").End(xlUp).Row
    
    Set uniqueTerms = CreateObject("Scripting.Dictionary")
    
    
    
    For Each cell In wsSource.Range("D2:D" & lastRow)
        If Not uniqueTerms.exists(cell.Value) Then
            uniqueTerms.Add cell.Value, cell.Value
        End If
    Next cell
    
    For Each term In uniqueTerms.keys
        Set wsDest = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        wsDest.Name = term
        
        wsSource.Rows(1).EntireRow.Copy Destination:=wsDest.Rows(1)
        
        For Each cell In wsSource.Range("D1:D" & lastRow)
            If cell.Value = term Then
                cell.EntireRow.Copy Destination:=wsDest.Cells(wsDest.Cells(wsDest.Rows.Count, "D").End(xlUp).Row + 1, 1)
                Columns("E:H").Select
                Selection.Clear
                Range("A1").Select
                Dim i As Integer
                Cells.EntireColumn.AutoFit
                    For i = 1 To ActiveSheet.UsedRange.Columns.Count
                        Columns(i).ColumnWidth = Columns(i).ColumnWidth
                    Next i

                wsDest.Range("A:A").EntireRow.AutoFit
            End If
        Next cell
    Next term

 '*******************************************************************
'Code from here down to next line of asterisks

    Dim UsedRange As Range
    For Each cell In wsSource.Range("B2:B" & lastRow)
       If cell.Value = "" Then
          Exit For
       Else
          Sheets(cell.Value).Select
          Sheets(cell.Value).UsedRange.Select
          
        Sheets(cell.Value).Selection.Copy Destination:=wsDest.Range("A1:A" & Rows.Count).End(xlUp).Offset(1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        
        'wsDest.Range("A1:A" & Rows.Count).End(xlUp).Offset(1).Select
        'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        '    :=False, Transpose:=False
        
       End If
    Next 'sheet_name

'Code from here up to next line of asterisks    
 '*******************************************************************

   'End macro with ...
    With Excel.Application
            .ScreenUpdating = True
            .Calculation = Excel.xlAutomatic
            .EnableEvents = True
    End With
End Sub
Attached Files
File Type: xlsm Cost Centers.xlsm (128.8 KB, 4 views)
Reply With Quote
  #2  
Old 09-20-2024, 09:32 AM
NoSparks NoSparks is offline Copy/Paste Range Values To Other Sheet Windows 10 Copy/Paste Range Values To Other Sheet Office 2010
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

try this and move Next term down below it
Code:
'*******************************************************************
'Code from here down to next line of asterisks

    For Each cell In wsSource.Range("B2:B" & lastRow)
       If cell.Value = "" Then
          Exit For
       Else
          If cell.Offset(, 2) = wsDest.Name Then
            Sheets(cell.Value).UsedRange.Copy
            wsDest.Range("A" & wsDest.UsedRange.Rows.Count + 2).PasteSpecial Paste:=xlPasteValues
          End If
       End If
    Next cell

'Code from here up to next line of asterisks
 '*******************************************************************
Reply With Quote
  #3  
Old 09-20-2024, 01:05 PM
Logit Logit is offline Copy/Paste Range Values To Other Sheet Windows 10 Copy/Paste Range Values To Other Sheet Office 2007
Expert
Copy/Paste Range Values To Other Sheet
 
Join Date: Jan 2017
Posts: 591
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

NoSparks :

Thank you so much for the solution. Works like a charm !

Would you be so kind to look at one more thing ... emailing the individual Director sheets based on email addresses shown in the COST CENTERS sheet ?

The image should be attached ... Thank you !
Attached Images
File Type: jpg Emails.jpg (106.1 KB, 12 views)
Reply With Quote
  #4  
Old 09-20-2024, 06:20 PM
NoSparks NoSparks is offline Copy/Paste Range Values To Other Sheet Windows 10 Copy/Paste Range Values To Other Sheet Office 2010
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Sorry Logit I don't really know what to do for the emailing
but I do know that if you load the dictionary item with the email address
by change this
uniqueTerms.Add cell.Value, cell.Value
to
uniqueTerms.Add cell.Value, cell.offset(,1).Value

this will show the sheet and who to email
Code:
        'Code from here up to next line of asterisks
         '*******************************************************************
        ' info for email
            MsgBox "the sheet to email is " & wsDest.Name & vbLf & _
                   "the email goes to " & uniqueTerms(wsDest.Name)
           
    Next term
Reply With Quote
  #5  
Old 09-20-2024, 07:39 PM
Logit Logit is offline Copy/Paste Range Values To Other Sheet Windows 10 Copy/Paste Range Values To Other Sheet Office 2007
Expert
Copy/Paste Range Values To Other Sheet
 
Join Date: Jan 2017
Posts: 591
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

NoSparks :

Thank you for your input. I just now finished the final draft of the project which includes the ability to email the Sr Director sheets. Took me some time to figure it out ... been working all day on it.

I wasn't checking my emails or this forum posting so I missed the opportunity to let you know prior to you responding. My apologies.

You've always been a great resource when I've run into difficulties and I really want you know just how much I appreciate it. I know the full value of volunteers that assist as I am a volunteer as well. Lots of work occurs behind the scenes.

Thank you again sir. Have a great weekend !
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Code to copy data from two sheet ,paste into corresponding sheet through cmdbutton jackfruit88 Excel Programming 1 07-08-2022 09:27 PM
Copy/Paste Range Values To Other Sheet Copy Excel Range and Paste in PowerPoint Shapes leops PowerPoint 4 08-31-2017 06:47 AM
Copy/Paste Range Values To Other Sheet How to copy excel sheet withe HEADER and Paste into new sheet? cloudforgiven Excel Programming 6 01-05-2017 07:30 PM
VBA macro to copy range and paste in next blank row tune2grow Excel Programming 0 09-03-2014 08:25 PM
Word - Calculate and paste values from Excel sheet Augf87 Word 1 07-06-2009 10:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:57 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft