![]() |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
|||
|
|||
|
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
'*******************************************************************
|
|
#3
|
|||
|
|||
|
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 ! |
|
#4
|
|||
|
|||
|
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
|
|
#5
|
|||
|
|||
|
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 ! |
|
| Thread Tools | |
| Display Modes | |
|
|
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 Excel Range and Paste in PowerPoint Shapes
|
leops | PowerPoint | 4 | 08-31-2017 06:47 AM |
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 |