VBA Copy, Paste to Different Workbook, Save as then loop the action through slicer

krisoey

New Member
Joined
Aug 7, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have some VBA issues with looping slicer. I'm trying to copy and paste certain data from one workbook to another, save it by certain data (contract#). I'm trying to loop this action through slicer

Below is the current VBA that i frankensteined together from this forum.

Any help is appreciated :)


VBA Code:
Function SlicerFunction() As Single
  Dim wkb As Workbook
  Dim wkb2 As Workbook
  Dim strFileName As String, strFilePath As String
 Dim slItem As SlicerItem
    Dim i As Long
    
   
 
    strFilePath = Workbooks("AccrualtoTest.xlsm").Sheets("Macro").Range("AZ2")
    strFileName = "RPA PS Accrual Template.xlsx"
    strsecondFile = strFilePath & strFileName
    
 
    
    
    On Error Resume Next
    
        Set wkb = Workbooks(strFileName)
    Set wkb2 = Workbooks("AccrualtoTest.xlsm")
   
    
    If wkb Is Nothing Then
        
        Set wkb = Workbooks.Open(strsecondFile)
    
    
    End If
    
    If wkb Is Nothing Then
        MsgBox strsecondFile & "Not Found", vbCritical, "Not Found"
        Exit Function
        
        End If
        
    On Error GoTo 0
    
 

     
    Workbooks("AccrualtoTest.xlsm").Sheets("Macro").Activate
    
    Range("L3").Select
    Selection.Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("E1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("E5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("AccrualtoTest.xlsm").Activate
    Range("A3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("E2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Windows("AccrualtoTest.xlsm").Activate
    Range("C3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("E3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("AccrualtoTest.xlsm").Activate
    Range("K3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("E4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Windows("AccrualtoTest.xlsm").Activate
 Range("J3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
       Range("E6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("AccrualtoTest.xlsm").Activate
    Range("E3:E53").Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("A17").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
 
    Windows("AccrualtoTest.xlsm").Activate
    Range("N3:N53").Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("B17").PasteSpecial xlPasteValues
    
    Windows("AccrualtoTest.xlsm").Activate
    Range("O3:O53").Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("C17").PasteSpecial xlPasteValues
    
    Windows("AccrualtoTest.xlsm").Activate
    Range("D3:D53").Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("D17").PasteSpecial xlPasteValues
    Windows("AccrualtoTest.xlsm").Activate
    Range("I3:I53").Copy
    Windows("RPA PS Accrual Template.xlsx").Activate
    Range("E17").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("AccrualtoTest.xlsm").Activate
    

   
   
   Set MYPATH = Workbooks("AccrualtoTest.xlsm").Sheets("Macro").Range("AZ2")
   
   Dim part2 As String
   part2 = Workbooks("AccrualtoTest.xlsm").Sheets("Macro").Range("BA3").Value
   

    If Len(Dir(MYPATH & part2, vbDirectory)) = 0 Then
    MkDir MYPATH & part2
    
    
    End If
    
    

    Workbooks("RPA PS Accrual Template.xlsx").Activate
    Sheets("Sheet1").UsedRange.Replace What:="(blank)", Replacement:=""
        Application.DisplayAlerts = False
    wkb.SaveAs Filename:=MYPATH & part2 & "\" & part2 & ".xlsx", FileFormat:=xlWorkbookDefault, CreateBackup:=False
    Application.DisplayAlerts = True
    
   ActiveWorkbook.Close
  
    
      
    
End Function


Sub Step_Thru_SlicerItems2()
    Dim slItem As SlicerItem
    Dim i As Long
    
    Application.ScreenUpdating = False
    With Workbooks("AccrualtoTest.xlsm").Sheets("Macro").SlicerCaches("Slicer_Contract")
        '--deselect all items except the first
        .SlicerItems(1).Selected = True
        For Each slItem In .VisibleSlicerItems
            If slItem.Name <> .SlicerItems(1).Name Then _
                slItem.Selected = False
        Next slItem
        Call MyFunction(1)
        '--step through each item and run custom function
        For i = 2 To .SlicerItems.Count
            .SlicerItems(i).Selected = True
            .SlicerItems(i - 1).Selected = False
            Call MyFunction(i)
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Found the error...apparently my function is called differently in the sub 🤪
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,040
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top