arjoderoon
Gebruiker
- Lid geworden
- 2 mei 2007
- Berichten
- 476
ik heb een hele lijst met bestanden die in principe allemaal dezelfde macro's bevatten. Als er nu iets moet wijzigen, moet ik iedere keer in ieder bestand de code gaan wijzigen dus ik had al bedacht dat het vast mogelijk zou moeten zijn om dit in 1 bestand op te slaan en alle bestanden aan dat ene bestand te linken.
Ik heb gezocht en vond dat je een xlam kunt maken waarin de codes opgeslagen zijn en waaraan gelinkt kan worden.
Ik heb alle code die nodig was gekopieerd en in een nieuwe workbook in de vba editor geplakt en vervolgens het bestand als xlam opgeslagen.
Alleen, hoe zorg ik er nu voor dat ik de code (ik zal de volledige code die ik wil plakken hieronder zetten) dat deze vanuit de andere bestanden nu aangeroept kan worden? Moet ik daarvoor bij ieder bestand ervoor zorgen dat de xlam als invoegtoepassing is geselecteerd? Of kan ik in de code ook een linkje maken naar het bestand?)
hierin maak ik al gebruik van een invoegtoepassing, namelijk nitro.xlam. in principe worden alle templates die ik gemaak standaard geopent met een verwijzing naar nitro.xlam, dus dat is gedekt.
Ik heb gezocht en vond dat je een xlam kunt maken waarin de codes opgeslagen zijn en waaraan gelinkt kan worden.
Ik heb alle code die nodig was gekopieerd en in een nieuwe workbook in de vba editor geplakt en vervolgens het bestand als xlam opgeslagen.
Alleen, hoe zorg ik er nu voor dat ik de code (ik zal de volledige code die ik wil plakken hieronder zetten) dat deze vanuit de andere bestanden nu aangeroept kan worden? Moet ik daarvoor bij ieder bestand ervoor zorgen dat de xlam als invoegtoepassing is geselecteerd? Of kan ik in de code ook een linkje maken naar het bestand?)
Code:
Sub vernieuwalles()
Call NITRO.RefreshDataAllWorksheets
Call datawissen
ThisWorkbook.Save
End Sub
Sub datawissen()
Application.ScreenUpdating = False
With Sheets("schaduwblad")
.Cells.ClearContents
End With
Application.ScreenUpdating = True
Call dataplaatsen
End Sub
Sub dataplaatsen()
Application.ScreenUpdating = False
Sheetnames = Array("food-drug", "food-drug (2)", "aswatson", "aswatson (2)", "food", "food (2)")
For i = LBound(Sheetnames) To UBound(Sheetnames)
With Sheets(Sheetnames(i))
.Range(.Range("U2"), .Cells(.Rows.Count, 1).End(xlUp)).Copy Sheets("Schaduwblad").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End With
Next i
Application.ScreenUpdating = True
Call kolomtitels
End Sub
Sub kolomtitels()
With Sheets("schaduwblad")
Sheets("food-drug").Range("a1", "F1").Copy Sheets("schaduwblad").Range("A1", "F1")
.[g1].Value = "4w periode -2"
.[h1].Value = "4w periode -1"
.[i1].Value = "4w periode"
.[j1].Value = "--"
.[k1].Value = "Last 12 wks -2"
.[l1].Value = "Last 12 wks -1"
.[m1].Value = "Last 12 wks 0"
.[n1].Value = "--"
.[o1].Value = "YTD-2"
.[p1].Value = "YTD-1"
.[q1].Value = "YTD-0"
.[r1].Value = "--"
.[s1].Value = "MAT-2"
.[t1].Value = "MAT-1"
.[u1].Value = "MAT-0"
.[v1].Value = "waarde 4w positief"
.[w1].Value = "waarde 12w positief"
.[x1].Value = "waarde YTD positief"
.[y1].Value = "waarde MAT positief"
.[z1].Value = "merk ja/nee"
.[aa1].Value = "item ja/nee"
End With
Call toevoegen
End Sub
Sub toevoegen()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With Worksheets("schaduwblad")
For Each cel In .Range(.[a2], .[a1000000].End(xlUp))
For i = 0 To 3
If cel.Offset(0, 8 + i * 4) > 0 Then
cel.Offset(0, 21 + i) = "ja"
Else
cel.Offset(0, 21 + i) = "nee"
End If
Next i
If cel.Offset(0, 3) = "" Then
cel.Offset(0, 25) = "nee"
Else
cel.Offset(0, 25) = "ja"
End If
If cel.Offset(0, 4) = "" Then
cel.Offset(0, 26) = "nee"
Else
cel.Offset(0, 26) = "ja"
End If
Next cel
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Call maaktabel
End Sub
Sub maaktabel()
Dim rLastCell As Range
With Sheets("schaduwblad")
Set rLastCell = .Cells.Find(What:="*", After:=.Cells(1, 1), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
'MsgBox ("The last used column is: " & rLastCell.Address)
'Maak tabel gewoon bereik
'.ListObjects("Tabel3").Unlist
.ListObjects.Add(xlSrcRange, .Range("A1", rLastCell.Address), _
, xlYes).Name = "Tabel3"
End With
End Sub
Sub Change_slicers()
'---changes source data of pivots connected to specified slicers
Dim dicPivotIDs As Object
Dim vSlicers() As Variant, vSlicerList() As Variant, vKey As Variant
Dim PT As PivotTable, PT1 As PivotTable
Dim sPivotID As String, sNewSource As String
Dim iSlicer As Long, iPivot As Long, lItem As Long
'--edit list of slicers. They must share the same PivotCache.
' they don't need to be connected to the same PivotTables
vSlicerList = Array("Slicer_FCT1", "Slicer_PROD", "Slicer_MKT", "Slicer_MERK_GEZ1")
'--edit with range reference to new PivotCache datasource
On Error GoTo ErrHandler
' example1: reference an existing Named Range with Workbook scope
'sNewSource = "MyPivotData"
' example2: reference an existing Table (ListObject)
sNewSource = "Tabel3"
' example3: other range reference
'sNewSource = Sheets("Sheet1").Range("A1").CurrentRegion.Address
Set dicPivotIDs = CreateObject("Scripting.Dictionary")
ReDim vSlicers(LBound(vSlicerList) To UBound(vSlicerList))
'--build array of arrays mapping each Slicer's connected PivotTables
For iSlicer = LBound(vSlicerList) To UBound(vSlicerList)
With ActiveWorkbook.SlicerCaches(vSlicerList(iSlicer))
If .PivotTables.Count Then
ReDim vPivots(1 To .PivotTables.Count)
For Each PT In .PivotTables
iPivot = iPivot + 1
Set vPivots(iPivot) = PT
'--add unique pivot identifiers to dictionary
sPivotID = "'" & PT.Parent.Name & "'!" & _
PT.TableRange1.Cells(1).Address
If Not dicPivotIDs.Exists(sPivotID) Then
lItem = lItem + 1
dicPivotIDs.Add sPivotID, lItem
End If
'--disconnect from slicer
.PivotTables.RemovePivotTable (PT)
Next PT
vSlicers(iSlicer) = vPivots
iPivot = 0
End If
End With
Next iSlicer
'---change datasource of all pivots
For Each vKey In dicPivotIDs.Keys
If PT1 Is Nothing Then
Set PT1 = Range(vKey).PivotTable
PT1.ChangePivotCache _
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=sNewSource)
Else
Range(vKey).PivotTable.CacheIndex = PT1.CacheIndex
End If
Next vKey
'--reconnect Pivots to Slicers using stored mapping
For iSlicer = LBound(vSlicers) To UBound(vSlicers)
If Not IsEmpty(vSlicers(iSlicer)) Then
With ActiveWorkbook.SlicerCaches(vSlicerList(iSlicer)).PivotTables
For iPivot = LBound(vSlicers(iSlicer)) To UBound(vSlicers(iSlicer))
.AddPivotTable vSlicers(iSlicer)(iPivot)
Next iPivot
End With
End If
Next iSlicer
MsgBox "The PivotTables' data source have been updated"
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
Sub select4wk()
Application.Goto Sheets("Dashboard 4wks period").ScrollRow = 11
End Sub
Sub select12wk()
Application.Goto Sheets("Dashboard 12wks period").ScrollRow = 11
End Sub
Sub selectYTD()
Application.Goto Sheets("Dashboard YTD").ScrollRow = 11
End Sub
Sub selectMAT()
Application.Goto Sheets("Dashboard MAT").ScrollRow = 11
End Sub
Sub Printsheets()
ActiveSheet.PrintOut
End Sub
Sub deletesheet()
Application.DisplayAlerts = False
ThisWorkbook.Unprotect Password:="TM"
Sheets("samenvoeging").Delete
ThisWorkbook.Unprotect Password:="TM"
Application.DisplayAlerts = True
ThisWorkbook.Save
End Sub
Sub list_Pivots()
Dim pvt As PivotTable
Dim iSht As Long
Dim iRow As Integer
Application.ScreenUpdating = False
Set objnewsheet = Worksheets.Add
objnewsheet.Activate
iRow = 2
iSht = 2
'SET TITLES
Range("A1").FormulaR1C1 = "Name"
Range("B1").FormulaR1C1 = "Source"
Range("C1").FormulaR1C1 = "Refreshed by"
Range("D1").FormulaR1C1 = "Refreshed"
Range("E1").FormulaR1C1 = "Sheet"
Range("F1").FormulaR1C1 = "Location"
'GET PIVOT DETAILS
For Each Worksheet In Worksheets
For Each pvt In Worksheet.PivotTables
objnewsheet.Cells(iRow, 1).Value = pvt.Name
objnewsheet.Cells(iRow, 2).Value = pvt.SourceData
objnewsheet.Cells(iRow, 3).Value = pvt.RefreshName
objnewsheet.Cells(iRow, 4).Value = pvt.RefreshDate
objnewsheet.Cells(iRow, 5).Value = Worksheet.Name
objnewsheet.Cells(iRow, 6).Value = pvt.TableRange1.Address
iRow = iRow + 1
Next
Next
objnewsheet.Activate
Application.ScreenUpdating = True
End Sub
Sub MultiplePivotSlicerCaches()
Dim oSlicer As Slicer
Dim oSlicercache As SlicerCache
Dim oPT As PivotTable
Dim oSh As Worksheet
Set objnewsheet = Worksheets.Add
objnewsheet.Activate
iRow = 1
For Each oSlicercache In ThisWorkbook.SlicerCaches
For Each oPT In oSlicercache.PivotTables
objnewsheet.Cells(iRow, 1) = oSlicercache.Name
objnewsheet.Cells(iRow, 2) = oPT.Name
objnewsheet.Cells(iRow, 3) = oPT.Parent.Name
iRow = iRow + 1
Next
Next
End Sub
hierin maak ik al gebruik van een invoegtoepassing, namelijk nitro.xlam. in principe worden alle templates die ik gemaak standaard geopent met een verwijzing naar nitro.xlam, dus dat is gedekt.
Laatst bewerkt: