Bekijk de onderstaande video om te zien hoe je onze site als een web app op je startscherm installeert.
Opmerking: Deze functie is mogelijk niet beschikbaar in sommige browsers.
Sub KopieerModuleNaarAnderFile()
Dim wbBron As Workbook
Dim wbDoel As Workbook
Dim modulNaam As String
Dim exportPad As String
Dim comp As VBIDE.VBComponent
' === Aanpassen ===
modulNaam = "Module1" ' Naam van de module die je wilt kopiëren
exportPad = "C:\Temp\temp_module.bas" ' Tijdelijk exportpad
Set wbBron = ThisWorkbook ' Werkboek MET de nieuwe macro
Set wbDoel = Workbooks("DoelFile.xlsm") ' Werkboek dat geüpdatet moet worden
' --- Stap 1: Exporteer module uit bronbestand ---
On Error GoTo FoutAfhandeling
wbBron.VBProject.VBComponents(modulNaam).Export exportPad
' --- Stap 2: Verwijder oude module in doelbestand (als die bestaat) ---
On Error Resume Next
Set comp = wbDoel.VBProject.VBComponents(modulNaam)
If Not comp Is Nothing Then
wbDoel.VBProject.VBComponents.Remove comp
End If
On Error GoTo FoutAfhandeling
' --- Stap 3: Importeer module in doelbestand ---
wbDoel.VBProject.VBComponents.Import exportPad
' --- Stap 4: Tijdelijk bestand opruimen ---
Kill exportPad
MsgBox "Module '" & modulNaam & "' succesvol overgezet naar " & wbDoel.Name, vbInformation
Exit Sub
FoutAfhandeling:
MsgBox "Fout " & Err.Number & ": " & Err.Description, vbCritical
End Sub
| Scenario | Wat aanpassen |
|---|---|
| Doel-file is al open | Workbooks("DoelFile.xlsm") — gewoon de naam |
| Doel-file moet nog geopend worden | Set wbDoel = Workbooks.Open("C:\pad\DoelFile.xlsm") |
| Meerdere modules tegelijk | Loop over een Array("Module1", "Module2", ...) |
Geen code nodig!
- Exporteer de module handmatig (VBA Editor → rechtermuisklik → Exporteren)
- Stuur het .bas bestandje op
- Ontvanger importeert het zelf (rechtermuisklik → Importeren)
| Methode | Andere pc | Instelling nodig |
|---|---|---|
| VBA Extensibility (code) | Ja, per pc | |
| .bas exporteren/importeren | Nee | |
| Personal.xlsb | Nee | |
| Add-in .xlam | Nee |
Dat schreef Claude ook."Toegang tot het VBA-projectobjectmodel vertrouwen"
Zonder die vinkje krijg je de fout:
Programmatische toegang tot Visual Basic Project is niet vertrouwd
'# HIERONDER DATA OVERZETTEN ##############################################################################
Dim bronPad As String, bronBestand As String, wbBron As Workbook, wbDoel As Workbook
Dim wsBron As Worksheet, wsDoel As Worksheet, blok As Variant, e As Long, j As Long, bladNaam As String, bereik As String
'===Pas bestandslocatie aan===
bronPad = ActiveWorkbook.Worksheets("Control").Range("D3")
bronBestand = VolOldNaamTB & ".xls "
If Dir(bronPad & bronBestand) = "" Then
MsgBox "BronBestand bestaat niet: " & bronPad & bronBestand, vbCritical
Exit Sub
End If
Set wbDoel = ThisWorkbook ' Doel = huidig Bestand
Set wbBron = Workbooks.Open(bronPad & bronBestand, ReadOnly:=True) ' Bronbestand openen
'===Lijst met: Array's in Bronblad, Bronbereik, Doelblad, Doelbereik===
blok = Array( _
Array("Blad1", "A6:E41", "H6:L41", "O6:Q41", "A44", "I44", "A45:E83", "H45:L83", "O45:Q83", "S6:S41", "S45:S83", "W3:Y14", "AA3:AA14", "AD33", "AI3:AP60", "AI63", "AI64:AP122", "AQ3:AQ122", "AR3:AR122"), _
Array("Kasboek", "A3:G60", "A63", "A64:G121"), _
Array("Control", "L22", "L26", "L30", "E28", "I14", "L22", "L26", "L30"), _
Array("Data", "A2:A60", "C2:C60") _
)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' ===LOOP OVER BLOKKEN===
For e = LBound(blok) To UBound(blok) ' Range proberen te laden
bladNaam = blok(e)(0) ' eerste element = werkbladnaam
On Error Resume Next
'===Blad ophalenuit Bron en Doel===
Set wsBron = wbBron.Sheets(bladNaam)
Set wsDoel = wbDoel.Sheets(bladNaam)
On Error GoTo FoutAfhandeling
If wsBron Is Nothing Or wsDoel Is Nothing Then
MsgBox "Blad '" & bladNaam & " ' niet gevondenin bron of doel.", vbExclamation
Set wsBron = Nothing
Set wsDoel = Nothing
GoTo VolgendeBlok
End If
wsDoel.Unprotect
For j = 1 To UBound(blok(e)) 'Loop over bereiken(vanaf index1, want 0 = bladnaam
bereik = blok(e)(j)
On Error Resume Next
wsDoel.Range(bereik).Value = wsBron.Range(bereik).Value
If Err.Number <> 0 Then
MsgBox "Fout bij bereik '" & bereik & "' op blad '" & bladNaam & "': " & Err.Description, vbExclamation
Err.Clear
End If
On Error GoTo FoutAfhandeling
Next j
wsDoel.Protect
VolgendeBlok:
Set wsBron = Nothing
Set wsDoel = Nothing
Next e
'===Bronbestand sluiten===
wbBron.Close SaveChanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
########################################################################
Array("Blad1", "A6:E41", CDbl("H6HL41"), "O6:Q41", "A44", "I44", "A45:E83", "H45:L83", "O45:Q83", "S6:S41", "S45:S83", "W3:Y14", "AA3:AA14", "AD33", "AI3:AP60", "AI63", "AI64:AP122", "AQ3:AQ122", "AR3:AR122"),
Columns("A").NumberFormat = "0.0000"
We gebruiken essentiële cookies om deze site te laten werken, en optionele cookies om de ervaring te verbeteren.