IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Aide pour Macro Indirect pour classeur fermé


Sujet :

Macros et VBA Excel

  1. #1
    Membre averti
    Homme Profil pro
    Contrôleur de gestion
    Inscrit en
    Novembre 2014
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Contrôleur de gestion

    Informations forums :
    Inscription : Novembre 2014
    Messages : 22
    Par défaut Aide pour Macro Indirect pour classeur fermé
    Bonjour,

    Je me permets de créer un nouveau post pour ce sujet largement abordé sur tous les forums parlant d'Excel et qui concerne l'utilisation de la fonction INDIRECT() pour récupérer les informations d'un classeur fermé. Oui je sais que INDIRECT() ne permet pas ce type de sorcellerie en revanche beaucoup de personnes se sont penchées sur le sujet et ont écrit des macros plutôt intéressantes.

    Il y a tout d'abord l'add-in de Laurent Longre qui fonctionne très bien mais que je n'ai pas retenu car pour l'utilisation que je souhaite faire, il faut que le code VBA soit contenu dans le fichier Excel qui va utiliser la macro.

    Ensuite, il y a la macro de Wilson So. qui fonctionne très bien ainsi que la macro de Harlan Grove reprise plusieurs fois pour des fix mineurs. (je vous mettrais les 2 codes en fin de post)
    Mon problèmes est que ces 2 solutions qui sont celles que je souhaite retenir utilise Set vExcel = CreateObject("Excel.Application") qui, de ce que j'ai compris (je suis débutant), crée à chaque fois une nouvelle instance Excel ce qui alourdi énormément l'utilisation de la fonction. Ceci est un réel problème pour moi car mon tableau contiendra facilement 1 000 appels de la fonction Pull() ou IndirectEx().

    Cependant, une personne du nom de Pappu Pager à donné une piste pour accélerer le traitement fait par la fonction pull, voici le quote en question:
    Hi harlan grove,

    This function is slow for only one reason. The Set xlapp = CreateObject(“Excel.Application”) has been defined in the function, which creates a new object every time a cell is changed.

    We need to initialize it only once. So you can just declare – – public xlapp as object – – in the module just above the line – – Function pull(xref As String) As Variant
    and
    PUT THE – – Set xlapp = CreateObject(“Excel.Application”) within workbook_open event under THISWORKBOOK. Believe me it works awesome. I have tried it and pulled 100 rows each from 20 files in 15 seconds.

    I can send you my excel files which are working with awesome speed. Please give me your email id.

    Pappu Pager.
    (From India that is Bharat)
    Cependant, je ne comprend pas ce qu'il nous explique et je ne sais pas le mettre en pratique. C'est pourquoi je me tourne vers vous afin de savoir si quelqu'un aurait l'extrême gentillesse de :
    - Soit modifier l'un des 2 codes que je vais fournir pour mettre en pratique les modifications de Pappu Pager afin que la macro soit plus rapide
    - Soit m'expliquer très concrètement comment faire ces modifications afin que je puisse les faire moi-même.

    Merci par avance pour toute l'aide que vous pourrez m'apporter

    MACRO Pull() de Harlan Grove:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    'Various and sundry utility functions
    Option Explicit
    Option Base 1
    Const MyDebug As Boolean = False
     
    Dim text2num_table As Object
     
     
    '----- begin VBA -----
    Function pull(xref As String) As Variant
      'inspired by Bob Phillips and Laurent Longre
      'but written by Harlan Grove
      '-----------------------------------------------------------------
      'Copyright (c) 2003 Harlan Grove.
      '
      'This code is free software; you can redistribute it and/or modify
      'it under the terms of the GNU General Public License as published
      'by the Free Software Foundation; either version 2 of the License,
      'or (at your option) any later version.
      '-----------------------------------------------------------------
      '2005-05-02
      'fixed InStrRev syntax. Now using XL2K+ syntax.
      '-----------------------------------------------------------------
      '2005-04-18
      'added logic to check for date values from open workbooks, then
      'adjust for 1904 date system in source workbooks
      '-----------------------------------------------------------------
      '2004-05-30
      'still more fixes, this time to address apparent differences between
      'XL8/97 and later versions. Specifically, fixed the InStrRev call,
      'which is fubar in later versions and was using my own hacked version
      'under XL8/97 which was using the wrong argument syntax. Also either
      'XL8/97 didn't choke on CStr(pull) called when pull referred to an
      'array while later versions do, or I never tested the 2004-03-25 fix
      'against multiple cell references.
      '-----------------------------------------------------------------
      '2004-05-28
      'fixed the previous fix - replaced all instances of 'expr' with 'xref'
      'also now checking for initial single quote in xref, and if found
      'advancing past it to get the full pathname [dumb, really dumb!]
      '-----------------------------------------------------------------
      '2004-03-25
      'revised to check if filename in xref exists - if it does, proceed;
      'otherwise, return a #REF! error immediately - this avoids Excel
      'displaying dialogs when the referenced file doesn't exist
      '-----------------------------------------------------------------
      Const DS1904DIFF As Long = 1461
     
      Dim xlapp As Object, xlwb As Workbook
      Dim b As String, r As Range, c As Range, n As Long, ds1904 As Boolean
     
      '** begin 2004-05-30 changes **
      '** begin 2004-05-28 changes **
      '** begin 2004-03-25 changes **
      '** 2005-05-02 change - XL2K+ syntax **
      n = InStrRev((xref), "\")
     
      If n > 0 Then
        If Mid(xref, n, 2) = "\[" Then
          b = Left(xref, n)
          n = InStr(n + 2, xref, "]") - n - 2
          If n > 0 Then b = b & Mid(xref, Len(b) + 2, n)
     
        Else
          '** 2005-05-02 change - XL2K+ syntax **
          n = InStrRev((xref), "!")
          If n > 0 Then b = Left(xref, n - 1)
     
        End If
     
        '** key 2004-05-28 addition **
        If Left(b, 1) = "'" Then b = Mid(b, 2)
        '** key 2006-07-27 addition **
        If Right(b, 1) = "'" Then b = Left(b, Len(b) - 1)
     
        On Error Resume Next
        If n > 0 Then If Dir(b) = "" Then n = 0
        Err.Clear
        On Error GoTo 0
     
      End If
     
      If n <= 0 Then
        pull = CVErr(xlErrRef)
        Exit Function
      End If
      '** end 2004-03-25 changes **
      '** end 2004-05-28 changes **
     
      pull = Evaluate(xref)
     
      '** begin 2005-04-18 changes **
      If Not IsError(pull) Then
        On Error Resume Next
        ds1904 = Workbooks(Right(b, n)).Date1904
        Err.Clear
        On Error GoTo 0
      End If
     
      '** key 2004-05-30 addition **
      '** changed in 2005-04-18 changes **
      If IsArray(pull) Then
        If ds1904 Then
          Dim a As Variant, i As Long, j As Long
     
          a = pull
          For i = LBound(a, 1) To UBound(a, 1)
            For j = LBound(a, 2) To UBound(a, 2)
              If VarType(a(i, j)) = vbDate Then a(i, j) = a(i, j) + DS1904DIFF
            Next j
          Next i
          pull = a
     
        End If
     
        Exit Function
     
      ElseIf ds1904 And VarType(pull) = vbDate Then
        pull = pull + DS1904DIFF
     
      End If
      '** end 2004-05-30 changes **
      '** end 2005-04-18 changes **
     
      If CStr(pull) = CStr(CVErr(xlErrRef)) Then
        On Error GoTo CleanUp   'immediate clean-up at this point
     
        Set xlapp = CreateObject("Excel.Application")
        Set xlwb = xlapp.Workbooks.Add  'needed by .ExecuteExcel4Macro
     
        On Error Resume Next    'now clean-up can wait
     
        n = InStr(InStr(1, xref, "]") + 1, xref, "!")
        b = Mid(xref, 1, n)
     
        Set r = xlwb.Sheets(1).Range(Mid(xref, n + 1))
     
        If r Is Nothing Then
          pull = xlapp.ExecuteExcel4Macro(xref)
     
        Else
          For Each c In r
            c.Value = xlapp.ExecuteExcel4Macro(b & c.Address(1, 1, xlR1C1))
          Next c
     
          pull = r.Value
     
        End If
     
    CleanUp:
        If Not xlwb Is Nothing Then xlwb.Close 0
        If Not xlapp Is Nothing Then xlapp.Quit
        Set xlapp = Nothing
     
      End If
     
    End Function
     
     
    'extension to CELL providing 123 @CELL/@CELLPOINTER functionality as
    'well as access to most Range properties
    '1st arg determines the property of characteristic being sought
    '2nd arg [OPTIONAL] specifies cell reference - AcitveCell if missing
    '3rd arg [OPTIONAL] specifies whether to return an array or not
    '    True = return array result for .Areas(1)
    '    False/missing = return scalar result for .Areas(1).Cells(1, 1)
    '
    'across
    'address
    'backgroundcolor
    'bold
    'bottomborder
    'bottombordercolor
    'col
    'color
    'columnhidden
    'comment
    'contents
    'coord
    'currentarray
    'currentregion
    'datatype
    'filedate
    'filename
    'fontface
    'fontsize
    'format
    'formula
    'formulaarray
    'formulahidden
    'formulalocal
    'formular1c1
    'formular1c1local
    'halign
    'hasarray
    'hasformula
    'hashyperlink
    'height
    'hidden
    'hyperlinkaddress
    'indentlevel
    'italic
    'left
    'leftborder
    'leftbordercolor
    'locked
    'mergearea
    'mergecells
    'name
    'numberformat
    'numberformatlocal
    'orientation
    'parentheses
    'pattern
    'patterncolor
    'prefix
    'rightborder
    'rightbordercolor
    'row
    'rowhidden
    'scrollarea
    'sheet
    'sheetname
    'sheetcount
    'shrinktofit
    'stylename
    'text
    'textcolor
    'top
    'topborder
    'topbordercolor
    'underline
    'usedrange
    'usestandardheight
    'usestandardwidth
    'valign
    'visible
    'width
    'workbookfullname
    'workbookname
    'workbookpath
    'wrap
    '
    Function ExtCell( _
      prop As String, _
      Optional rng As Variant, _
      Optional rar As Boolean = False _
    ) As Variant
        'Copyright (C) 2002, 2003, Harlan Grove
        'This is free software. It's use in derivative works is covered
        'under the terms of the Free Software Foundation's GPL. See
        'http://www.gnu.org/copyleft/gpl.html
     
        Dim ws As Worksheet, wb As Workbook, rv As Variant
        Dim i As Long, j As Long, m As Long, n As Long, t As String
     
        Application.Volatile True
     
        If TypeOf rng Is Range Then
            If rar Then
                Set rng = rng.Areas(1)
            Else
                Set rng = rng.Areas(1).Cells(1, 1)
            End If
        ElseIf IsMissing(rng) Then
            Set rng = ActiveCell
        Else
            ExtCell = CVErr(xlErrRef)
            Exit Function
        End If
     
        prop = LCase(prop)
     
        m = rng.rows.Count
        n = rng.Columns.Count
        rv = rng.Value
     
        Set ws = rng.Worksheet
        Set wb = ws.Parent
     
        Select Case prop
     
        Case "across"  'from later 123 versions - limited usefulness!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -( _
                          rng.Cells(i, j).HorizontalAlignment = _
                          xlHAlignCenterAcrossSelection _
                        )
                    Next j
                Next i
            Else
                rv = -( _
                  rng.HorizontalAlignment = _
                  xlHAlignCenterAcrossSelection _
                )
            End If
     
        Case "address"  'from CELL - limited usefulness!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Address
                    Next j
                Next i
            Else
                rv = rng.Address
            End If
     
        Case "backgroundcolor"  'from later 123 versions - USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Interior.ColorIndex
                    Next j
                Next i
            Else
                rv = rng.Interior.ColorIndex
            End If
     
        Case "bold"  'from later 123 versions - USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).Font.Bold)
                    Next j
                Next i
            Else
                rv = -(rng.Font.Bold)
            End If
     
     
        Case "bottomborder"  'from later 123 versions - USEFUL!
        'Note: many possible return values! wrap inside SIGN to test T/F
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeBottom).LineStyle - _
                          xlLineStyleNone
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeBottom).LineStyle - xlLineStyleNone
            End If
     
        Case "bottombordercolor"  'from later 123 versions - USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeBottom).ColorIndex
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeBottom).ColorIndex
            End If
     
        Case "col", "column"  'from CELL - pointless - use COLUMN instead!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Column
                    Next j
                Next i
            Else
                rv = rng.Column
            End If
     
        Case "color"  'from CELL - limited usefulness
        'NOTE: differences between Excel & 123 - Excel's returns 1 whenever
        'there's a color specified for EITHER positive OR negative values
        'in the number format, e.g., 1 for format "[Black]0;-0;0" but not
        'for format "0;-0;[Green]0"
        'Another place where Excel doesn't conform to it's documentation!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = Evaluate( _
                          "=CELL(""Color""," & _
                          rng.Cells(i, j).Address(True, True, xlA1, True) & _
                          ")" _
                        )
                    Next j
                Next i
            Else
                rv = Evaluate( _
                  "=CELL(""Color""," & _
                  rng.CellsAddress(True, True, xlA1, True) & _
                  ")" _
                )
            End If
     
        Case "columnhidden"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -rng.Cells(i, j).EntireColumn.Hidden
                    Next j
                Next i
            Else
                rv = -rng.EntireColumn.Hidden
            End If
     
        Case "comment"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        If Not rng.Cells(i, j).Comment Is Nothing Then
                            rv(i, j) = rng.Cells(i, j).Comment.text
                        Else
                            rv(i, j) = ""
                        End If
                    Next j
                Next i
            Else
                If Not rng.Comment Is Nothing Then
                    rv = rng.Comment.text
                Else
                    rv = ""
                End If
            End If
     
        Case "contents", "value"  'absolutely pointless - compatibility only
            'DOME - nothing more to do!
     
        Case "coord"  'from later 123 versions - USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = "'" & ws.Name & "'!" & _
                          rng.Cells(i, j).Address
                    Next j
                Next i
            Else
                rv = "'" & ws.Name & "'!" & rng.Address
            End If
     
        Case "currentarray"  'NOTE: returns Range addresses!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).CurrentArray.Address
                    Next j
                Next i
            Else
                rv = rng.CurrentArray.Address
            End If
     
        Case "currentregion"  'NOTE: returns Range addresses!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).CurrentRegion.Address
                    Next j
                Next i
            Else
                rv = rng.CurrentRegion.Address
            End If
     
        'different characteristics grouped for efficiency
        'TYPE needed for backward compatibility w/123 but otherwise useless
        'DATATYPE and FORMULATYPE are options in later 123 versions' @CELL
        'no need for them but included to make 123 conversion easier
        Case "datatype", "formulatype", "type"
            t = Left(prop, 1)
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = IIf( _
                          t = "f" And rng.Cells(i, j).HasFormula, _
                          "f", _
                          "" _
                        )
     
                        If rng.Cells(i, j).Formula = "" Then
                            rv(i, j) = rv(i, j) & "b"
                        ElseIf IsNumeric("0" & CStr(rng.Cells(i, j).Value)) _
                          Or (t = "t" And IsError(rng.Cells(i, j).Value)) Then
                            rv(i, j) = rv(i, j) & "v"
                        ElseIf rng.Cells(i, j).Value = CVErr(xlErrNA) Then
                            rv(i, j) = rv(i, j) & "n"
                        ElseIf IsError(rng.Cells(i, j).Value) Then
                            rv(i, j) = rv(i, j) & "e"
                        Else
                            rv(i, j) = rv(i, j) & "l"
                        End If
                    Next j
                Next i
            Else
                rv = IIf( _
                  t = "f" And rng.HasFormula, _
                  "f", _
                  "" _
                )
     
                If rng.Formula = "" Then
                    rv = rv & "b"
                ElseIf IsNumeric("0" & CStr(rng.Value)) _
                  Or (t = "t" And IsError(rng.Value)) Then
                    rv = rv & "v"
                ElseIf IsError(rng.Value) Then
                    rv = rv & IIf(Application.WorksheetFunction.IsNA(rng.Value), "n", "e")
                Else
                    rv = rv & "l"
                End If
            End If
     
        Case "filedate"  'from later 123 versions - limited usefulness!
            t = wb.BuiltinDocumentProperties("Last Save Time")  'invariant!
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "filename"  'from CELL - limited usefulness!
        'A testament to Microsoft's hypocracy! They could include this from
        '123R2.2 (it wasn't in 123R2.0x), modify it in Excel 4.0 to include
        'the worksheet name, but they can't make any other changes to CELL?!
            t = Evaluate( _
              "=CELL(""Filename""," & _
              rng.Address(True, True, xlA1, True) & _
              ")" _
            )  'invariant!
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "fontface", "fontname", "typeface"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Font.Name
                    Next j
                Next i
            Else
                rv = rng.Font.Name
            End If
     
        Case "fontsize", "pitch", "typesize"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Font.Size
                    Next j
                Next i
            Else
                rv = rng.Font.Size
            End If
     
        Case "format"  'from CELL
        'Backwards compatibility w/123 - unnecessary
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = Evaluate( _
                          "=CELL(""Format""," & _
                          rng.Cells(i, j).Address(True, True, xlA1, True) & _
                          ")" _
                        )
                    Next j
                Next i
            Else
                rv(i, j) = Evaluate( _
                  "=CELL(""Format""," & _
                  rng.Address(True, True, xlA1, True) & _
                  ")" _
                )
            End If
     
        Case "formula"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Formula
                    Next j
                Next i
            Else
                rv = rng.Formula
            End If
     
        Case "formulaarray"  'questionable usefulness
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).FormulaArray
                    Next j
                Next i
            Else
                rv = rng.FormulaArray
            End If
     
        Case "formulahidden"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).FormulaHidden)
                    Next j
                Next i
            Else
                rv = -(rng.FormulaHidden)
            End If
     
        Case "formulalocal"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).FormulaLocal
                    Next j
                Next i
            Else
                rv = rng.FormulaLocal
            End If
     
        Case "formular1c1"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).FormulaR1C1
                    Next j
                Next i
            Else
                rv = rng.FormulaR1C1
            End If
     
        Case "formular1c1local"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).FormulaR1C1Local
                    Next j
                Next i
            Else
                rv = rng.FormulaR1C1Local
            End If
     
        Case "halign", "horizontalalignment"  'from later 123 versions
        'Note: different return values than 123. 0 = general alignment
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).HorizontalAlignment - _
                          xlHAlignGeneral
                    Next j
                Next i
            Else
                rv = rng.HorizontalAlignment - xlHAlignGeneral
            End If
     
        Case "hasarray"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).HasArray)
                    Next j
                Next i
            Else
                rv = -(rng.HasArray)
            End If
     
        Case "hasformula"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).HasFormula)
                    Next j
                Next i
            Else
                rv = -(rng.HasFormula)
            End If
     
        Case "hashyperlink", "hashyperlinks"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).Hyperlinks.Count > 0)
                    Next j
                Next i
            Else
                rv = -(rng.Hyperlinks.Count > 0)
            End If
     
        Case "height", "rowheight"  'from later 123 versions - USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Height
                    Next j
                Next i
            Else
                rv = rng.Height
            End If
     
        Case "hidden"  'see ColumnHidden and RowHidden - this is less useful
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).Hidden)
                    Next j
                Next i
            Else
                rv = -(rng.Hidden)
            End If
     
        Case "hyperlinkaddress"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Hyperlinks(1).Address
                    Next j
                Next i
            Else
                rv = rng.Hyperlinks(1).Address
            End If
     
        Case "indentlevel"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).rng.IndentLevel
                    Next j
                Next i
            Else
                rv = rng.rng.IndentLevel
            End If
     
        Case "italic"  'from later 123 versions - USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).Font.Italic)
                    Next j
                Next i
            Else
                rv = -(rng.Font.Italic)
            End If
     
        Case "left"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Left
                    Next j
                Next i
            Else
                rv = rng.Left
            End If
     
        Case "leftborder"  'from later 123 versions
        'Note: many possible return values! wrap inside SIGN to test T/F
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeLeft).LineStyle - _
                          xlLineStyleNone
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeLeft).LineStyle - xlLineStyleNone
            End If
     
        Case "leftbordercolor"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeLeft).ColorIndex
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeLeft).ColorIndex
            End If
     
        Case "locked", "protect"  'from CELL
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).Locked)
                    Next j
                Next i
            Else
                rv = -(rng.Locked)
            End If
     
        Case "mergearea"  'NOTE: returns Range addresses!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).MergeArea.Address
                    Next j
                Next i
            Else
                rv = rng.MergeArea.Address
            End If
     
        Case "mergecells"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).MergeCells)
                    Next j
                Next i
            Else
                rv = -(rng.MergeCells)
            End If
     
        Case "name"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Name
                    Next j
                Next i
            Else
                rv = rng.Name
            End If
     
        Case "numberformat"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).NumberFormat
                    Next j
                Next i
            Else
                rv = rng.NumberFormat
            End If
     
        Case "numberformatlocal"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).NumberFormatLocal
                    Next j
                Next i
            Else
                rv = rng.NumberFormatLocal
            End If
     
        Case "orientation", "rotation"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Orientation
                    Next j
                Next i
            Else
                rv = rng.Orientation
            End If
     
        Case "parentheses"  'from CELL
        'Backwards compatibility w/123 - unnecessary
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = Evaluate( _
                          "=CELL(""Parentheses""," & _
                          rng.Cells(i, j).Address(True, True, xlA1, True) & _
                          ")" _
                        )
                    Next j
                Next i
            Else
                rv = Evaluate( _
                  "=CELL(""Parentheses""," & _
                  rng.Address(True, True, xlA1, True) & _
                  ")" _
                )
            End If
     
        Case "pattern"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Interior.Pattern - _
                          xlPatternNone
                    Next j
                Next i
            Else
                rv = rng.Interior.Pattern - xlPatternNone
            End If
     
        Case "patterncolor"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Interior.PatternColorIndex
                    Next j
                Next i
            Else
                rv = rng.Interior.PatternColorIndex
            End If
     
        Case "prefix", "prefixcharacter"  'from CELL
        'Backwards compatibility w/123 - unnecessary
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = Evaluate( _
                          "=CELL(""Prefix""," & _
                          rng.Cells(i, j).Address(True, True, xlA1, True) & _
                          ")" _
                        )
                    Next j
                Next i
            Else
                rv = Evaluate( _
                  "=CELL(""Prefix""," & _
                  rng.Address(True, True, xlA1, True) & _
                  ")" _
                )
            End If
     
        Case "rightborder"  'from later 123 versions
        'Note: many possible return values! wrap inside SIGN to test T/F
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeRight).LineStyle - _
                          xlLineStyleNone
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeRight).LineStyle - xlLineStyleNone
            End If
     
        Case "rightbordercolor"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeRight).ColorIndex
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeRight).ColorIndex
            End If
     
        Case "row"  'from CELL - pointless - use ROW instead!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Row
                    Next j
                Next i
            Else
                rv = rng.Row
            End If
     
        Case "rowhidden"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).EntireRow.Hidden)
                    Next j
                Next i
            Else
                rv = -(rng.EntireRow.Hidden)
            End If
     
        Case "scrollarea"
        'Who needs consistency?! Why doesn't this return a Range object?
            t = ws.ScrollArea  'invariant!
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "sheet", "worksheet"  'from later 123 versions - USEFUL!
            t = ws.Index  'invariant!
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "sheetname", "worksheetname"  'from later 123 versions - USEFUL!
            t = ws.Name  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "sheetcount", "sheetscount", "worksheetcount", "worksheetscount"
            t = wb.Worksheets.Count  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "shrinktofit"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).ShrinkToFit)
                    Next j
                Next i
            Else
                rv = -(rng.ShrinkToFit)
            End If
     
        Case "stylename"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Style.Name
                    Next j
                Next i
            Else
                rv = rng.Style.Name
            End If
     
        Case "text"  'USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).text
                    Next j
                Next i
            Else
                rv = rng.text
            End If
     
        Case "textcolor"  'from later 123 versions - USEFUL!
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Font.ColorIndex
                    Next j
                Next i
            Else
                rv = rng.Font.ColorIndex
            End If
     
        Case "top"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Top
                    Next j
                Next i
            Else
                rv = rng.Top
            End If
     
        Case "topborder"  'from later 123 versions
        'Note: many possible return values! wrap inside SIGN to test T/F
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeTop).LineStyle - _
                          xlLineStyleNone
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeTop).LineStyle - xlLineStyleNone
            End If
     
        Case "topbordercolor"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Borders(xlEdgeTop).ColorIndex
                    Next j
                Next i
            Else
                rv = rng.Borders(xlEdgeTop).ColorIndex
            End If
     
        Case "underline"  'from later 123 versions - USEFUL!
        'Note: many possible return values! wrap inside SIGN to test T/F
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).Font.Underline - _
                          xlUnderlineStyleNone
                    Next j
                Next i
            Else
                rv = rng.Font.Underline - xlUnderlineStyleNone
            End If
     
        Case "usedrange"  'NOTE: returns Range addresses!
            t = ws.UsedRange.Address  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "usestandardheight"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).UseStandardHeight)
                    Next j
                Next i
            Else
                rv = -(rng.UseStandardHeight)
            End If
     
        Case "usestandardwidth"
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).UseStandardWidth)
                    Next j
                Next i
            Else
                rv = -(rng.UseStandardWidth)
            End If
     
        Case "valign", "verticalalignment"  'from later 123 versions
        'Note: different return values than 123. 0 = Bottom-aligned
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = _
                          rng.Cells(i, j).VerticalAlignment - _
                          xlVAlignBottom
                    Next j
                Next i
            Else
                rv = rng.VerticalAlignment - xlVAlignBottom
            End If
     
        Case "visible", "sheetvisible", "worksheetvisible"
            t = -(ws.Visible)  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "width", "columnwidth"  'from CELL
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = rng.Cells(i, j).Width
                    Next j
                Next i
            Else
                rv = rng.Width
            End If
     
        Case "workbookfullname"  'same as FileName in later 123 versions
            t = wb.FullName  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "workbookname"
            t = wb.Name  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "workbookpath"
            t = wb.path  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        Case "wrap", "wraptext"  'from later 123 versions
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = -(rng.Cells(i, j).WrapText)
                    Next j
                Next i
            Else
                rv = -(rng.WrapText)
            End If
     
        Case Else  'invalid property/characteristic
            t = CVErr(xlErrValue)  'invariant
     
            If rar Then
                For i = 1 To m
                    For j = 1 To n
                        rv(i, j) = t
                    Next j
                Next i
            Else
                rv = t
            End If
     
        End Select
     
        ExtCell = rv
    End Function
    Macro IndirectEx() de Wilson So.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    '------------------------------------
    'Extended INDIRECT Function v1.0
    '------------------------------------
    'Copyright (c) 2009 Wilson So.
    'E-mail: shwskm@yahoo.com.hk
    '------------------------------------
    'Credits:
    '- Designed and written by Wilson So.
    '- The 'CreateObject("Excel.Application")' trick was inspired by Harlan Grove's PULL function source code.
    '------------------------------------
    'This is an open source. You can freely redistribute and modify it, but please kindly give credit to the contributers.
    'Please also kindly report any bugs/suggestions through e-mail or in the forums where I posted it.
    '------------------------------------
    'How to use:
    '- Basically same as INDIRECT() in Excel - the same concept for the ref_text parameter.
    '- To update the static memory for a particular reference,
    '  type TRUE in the second parameter (just one of the IndirectEx() containing that reference)
    '  and calculate it once.
    '------------------------------------
    'Features:
    '- You can refer to the closed workbook data.
    '- The retrieved closed workbook data will be stored in the static memory,
    '  so in the next time, the closed workbook will not be opened again for fast retrieve.
    '- A range instead of an array will be returned if the path is omitted in the ref_text,
    '  so it still works fine if the user refers to an enormous array, e.g. "Sheet1!1:65536".
    '- You can use it inside INDEX(), VLOOKUP(), MATCH() etc.
    '- You can use it with OFFSET(), but only for opened workbook data.
    '- The procedure will not blindly retrieve all the data as requested;
    '  it will not retrieve data beyond the "Ctrl + End" cell, in order to keep the memory as small as possible.
    '- #NUM! will be returned in case of lack of memory.
    '- #REF! will be returned in case of a wrong path.
    '- #VALUE! will be returned in case of other errors.
    '------------------------------------
    'Known issues:
    '- Due to the use of SpecialCells(), #VALUE! will be returned if the worksheet for a closed workbook is protected.
    '------------------------------------
     
    Function IndirectEx(ref_text As String, Optional refresh_memory As Boolean = False) As Variant
        On Error GoTo ClearObject
     
        Dim RefName As String
        Dim SheetName As String
        Dim WBName As String
        Dim FolderName As String
     
        Dim vExcel As Object
        Dim vWB As Workbook
     
        Static dbOutput() As Variant
        Static dbKey() As String
        Static dbTotalOutput As Integer
        Dim dbIndex As Integer
     
        Dim UserEndRow As Long, UserEndCol As Integer
        Dim RealEndRow As Long, RealEndCol As Integer
        Dim EndRow As Long, EndCol As Integer
        Dim RangeHeight As Long, RangeWidth As Integer
     
        GetNames ref_text, RefName, SheetName, WBName, FolderName
     
        If dbTotalOutput = 0 Then
            ReDim dbOutput(1 To 1) As Variant
            ReDim dbKey(1 To 1) As String
        End If
     
        For i = 1 To dbTotalOutput
            If dbKey(i) = FolderName & WBName & "!" & SheetName & "!" & RefName Then
                dbIndex = i
            End If
        Next
     
        If dbIndex = 0 Or refresh_memory Then
            If dbIndex = 0 Then
                dbTotalOutput = dbTotalOutput + 1
                dbIndex = dbTotalOutput
                ReDim Preserve dbOutput(1 To dbTotalOutput) As Variant
                ReDim Preserve dbKey(1 To dbTotalOutput) As String
                dbKey(dbIndex) = FolderName & WBName & "!" & SheetName & "!" & RefName
            End If
            If FolderName = "" Then
                Set dbOutput(dbIndex) = Workbooks(WBName).Worksheets(SheetName).Range(RefName)
            ElseIf Dir(FolderName & WBName) <> "" Then
                Set vExcel = CreateObject("Excel.Application")
                Set vWB = vExcel.Workbooks.Open(FolderName & WBName)
                With vWB.Sheets(SheetName)
                    On Error GoTo ClearObject
                    UserEndRow = .Range(RefName).Row + .Range(RefName).Rows.count - 1
                    UserEndCol = .Range(RefName).Column + .Range(RefName).Columns.count - 1
                    RealEndRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
                    RealEndCol = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
                    EndRow = IIf(UserEndRow < RealEndRow, UserEndRow, RealEndRow)
                    EndCol = IIf(UserEndCol < RealEndCol, UserEndCol, RealEndCol)
                    RangeHeight = EndRow - .Range(RefName).Row + 1
                    RangeWidth = EndCol - .Range(RefName).Column + 1
                    On Error Resume Next
                    dbOutput(dbIndex) = .Range(RefName).Resize(RangeHeight, RangeWidth).Value
                    If Err.Number <> 0 Then
                        IndirectEx = CVErr(xlErrNum)
                        GoTo ClearObject
                    End If
                End With
                On Error GoTo ClearObject
                vWB.Close False
                vExcel.Quit
                Set vExcel = Nothing
            Else
                IndirectEx = CVErr(xlErrRef)
                Exit Function
            End If
        End If
     
        If TypeOf dbOutput(dbIndex) Is Range Then
            Set IndirectEx = dbOutput(dbIndex)
        Else
            IndirectEx = dbOutput(dbIndex)
        End If
     
        Exit Function
     
    ClearObject:
        On Error Resume Next
        If Not (vExcel Is Nothing) Then
            vWB.Close False
            vExcel.Quit
            Set vExcel = Nothing
        End If
    End Function
     
    Private Sub GetNames(ByVal ref_text As String, ByRef RefName As String, ByRef SheetName As String, ByRef WBName As String, ByRef FolderName As String)
        Dim P_e As Integer
        Dim P_b1 As Integer
        Dim P_b2 As Integer
        Dim P_s As Integer
     
        P_e = InStr(1, ref_text, "!")
        P_b1 = InStr(1, ref_text, "[")
        P_b2 = InStr(1, ref_text, "]")
        P_s = InStr(1, ref_text, ":\")
     
        If P_e = 0 Then
            RefName = ref_text
        Else
            RefName = Right$(ref_text, Len(ref_text) - P_e)
        End If
        RefName = Replace$(RefName, "$", "")
     
        If P_e = 0 Then
            SheetName = Application.Caller.Parent.Name
        ElseIf P_b1 = 0 Then
            SheetName = Left$(ref_text, P_e - 1)
        Else
            SheetName = Mid$(ref_text, P_b2 + 1, P_e - P_b2 - 1)
        End If
        SheetName = Replace$(SheetName, "'", "")
     
        If P_b1 = 0 Then
            WBName = Application.Caller.Parent.Parent.Name
        Else
            WBName = Mid$(ref_text, P_b1 + 1, P_b2 - P_b1 - 1)
        End If
     
        If P_s = 0 Then
            FolderName = ""
        Else
            FolderName = Left$(ref_text, P_b1 - 1)
        End If
        If Left$(FolderName, 1) = "'" Then FolderName = Right$(FolderName, Len(FolderName) - 1)
    End Sub

  2. #2
    Expert confirmé
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 208
    Par défaut
    Bonjour,

    Je n'ai pas décortiqué le roman que tu publies. Je suppose que ça signifie que tu crées l'instance d'Excel à l'ouverture du classeur contenant la macro, en mettant dans le module "ThisWorkbook" la macro :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Workbook_Open()
       Set vExcel = CreateObject("Excel.Application")
    End Sub

  3. #3
    Membre averti
    Homme Profil pro
    Contrôleur de gestion
    Inscrit en
    Novembre 2014
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Contrôleur de gestion

    Informations forums :
    Inscription : Novembre 2014
    Messages : 22
    Par défaut
    Bonjour,

    J'ai essayé la solution que vous me proposez et elle ne fonctionne pas.
    Je vais essayé de faire court vu que personnes n'aime lire des romans, et personnes n'aime les écrire pour rien

    Vous trouverez en PJ mes 2 fichiers de test. La macro fonctionne correctement mais est très lente de base.
    En déclarant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Workbook_Open()
       Set vExcel = CreateObject("Excel.Application")
    End Sub
    et en mettant en commentaire la ligne identique dans la macro principale, la formule IndirectEx() me renvoie 0.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
      ElseIf Dir(FolderName & WBName) <> "" Then
                Set vExcel = CreateObject("Excel.Application") 'ligne a mettre en commentaire
     
                Set vWB = vExcel.Workbooks.Open(FolderName & WBName)
    Quelqu'un aurait-il une solution autre que celle de Daniel.C ?

    Merci par avance

    Source.xlsx

    test.xlsm

  4. #4
    Expert confirmé
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 208
    Par défaut
    Bonjour,

    Je me mettrais des claques ! Il faut bien sûr déclarer "vExcel" en tant que variable publique dans un module standard, sinon la variable ne dure que jusqu'à la fin de la macro :

    Mets :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Public vExcel As Object
    en tête d'un module standard.

  5. #5
    Membre averti
    Homme Profil pro
    Contrôleur de gestion
    Inscrit en
    Novembre 2014
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Contrôleur de gestion

    Informations forums :
    Inscription : Novembre 2014
    Messages : 22
    Par défaut
    Bonjour,

    Tout d'abord, merci pour votre réponse

    J'ai donc fait:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Public vExcel As Object
    au début de mon module 1 (avant le début de ma macro)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Workbook_Open()
       Set vExcel = CreateObject("Excel.Application")
    End Sub
    Dans mon ThisWorkBook

    Et j'ai mis en commentaire la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Set vExcel = CreateObject("Excel.Application") 'ligne a mettre en commentaire
    Dans ma macro.

    Malheureusement, après ces modifications, la formule IndirectEx() me renvoie 0 (alors qu'elle fonctionne si je ne fait aucune modification).
    Ai-je loupé quelque chose dans vos explications ?

  6. #6
    Expert confirmé
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 208
    Par défaut
    Dans le module ThisWorkbook, tu conserves la macro :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Workbook_Open()
       Set vExcel = CreateObject("Excel.Application")
    End Sub
    Pour la variable, c'est OK. Le fait de déclarer vExcel comme variable publique permet son utilisation tant qu'elle n'est pas effacée.

  7. #7
    Membre averti
    Homme Profil pro
    Contrôleur de gestion
    Inscrit en
    Novembre 2014
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Contrôleur de gestion

    Informations forums :
    Inscription : Novembre 2014
    Messages : 22
    Par défaut
    Je suis désolé, mais je ne comprend pas ce que vous me demandez de faire dans votre dernier message.

    A l'heure actuelle, Thisworkbook contient :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Workbook_Open()
       Set vExcel = CreateObject("Excel.Application")
    End Sub
    Ma macro est dans le module 1, la déclaration de la variable publique vExcel est également dans le module 1 (c'est la première ligne de mon module).

    Et avec tout cela, quand j'essaye d'utiliser IndirectEx() dans mon classeur, la formule me renvoie 0 alors qu'elle est censé me renvoyer quelque chose.

    Ou est ce que je me suis loupé pour que cela ne fonctionne pas ?

  8. #8
    Expert confirmé
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 208
    Par défaut
    Conserve les modifs telles que je te les ai données et modifie la fonction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    Function IndirectEx(ref_text As String, Optional refresh_memory As Boolean = False) As Variant
        On Error GoTo ClearObject
     
        Dim RefName As String
        Dim SheetName As String
        Dim WBName As String
        Dim FolderName As String
     
        Dim vExcel As Object
        Dim vWB As Workbook
     
        Static dbOutput() As Variant
        Static dbKey() As String
        Static dbTotalOutput As Integer
        Dim dbIndex As Integer
     
        Dim UserEndRow As Long, UserEndCol As Integer
        Dim RealEndRow As Long, RealEndCol As Integer
        Dim EndRow As Long, EndCol As Integer
        Dim RangeHeight As Long, RangeWidth As Integer
     
        GetNames ref_text, RefName, SheetName, WBName, FolderName
     
        If dbTotalOutput = 0 Then
            ReDim dbOutput(1 To 1) As Variant
            ReDim dbKey(1 To 1) As String
        End If
     
        For i = 1 To dbTotalOutput
            If dbKey(i) = FolderName & WBName & "!" & SheetName & "!" & RefName Then
                dbIndex = i
            End If
        Next
     
        If dbIndex = 0 Or refresh_memory Then
            If dbIndex = 0 Then
                dbTotalOutput = dbTotalOutput + 1
                dbIndex = dbTotalOutput
                ReDim Preserve dbOutput(1 To dbTotalOutput) As Variant
                ReDim Preserve dbKey(1 To dbTotalOutput) As String
                dbKey(dbIndex) = FolderName & WBName & "!" & SheetName & "!" & RefName
            End If
            If FolderName = "" Then
                Set dbOutput(dbIndex) = Workbooks(WBName).Worksheets(SheetName).Range(RefName)
            ElseIf Dir(FolderName & WBName) <> "" Then
    '            Set vExcel = CreateObject("Excel.Application") 'ligne a modifier
     
                Set vWB = vExcel.Workbooks.Open(FolderName & WBName)
                With vWB.Sheets(SheetName)
                    On Error GoTo ClearObject
                    UserEndRow = .Range(RefName).Row + .Range(RefName).Rows.count - 1
                    UserEndCol = .Range(RefName).Column + .Range(RefName).Columns.count - 1
                    RealEndRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
                    RealEndCol = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
                    EndRow = IIf(UserEndRow < RealEndRow, UserEndRow, RealEndRow)
                    EndCol = IIf(UserEndCol < RealEndCol, UserEndCol, RealEndCol)
                    RangeHeight = EndRow - .Range(RefName).Row + 1
                    RangeWidth = EndCol - .Range(RefName).Column + 1
                    On Error Resume Next
                    dbOutput(dbIndex) = .Range(RefName).Resize(RangeHeight, RangeWidth).Value
                    If Err.Number <> 0 Then
                        IndirectEx = CVErr(xlErrNum)
                        GoTo ClearObject
                    End If
                End With
                On Error GoTo ClearObject
                vWB.Close False
                vExcel.Quit
                Set vExcel = Nothing
            Else
                IndirectEx = CVErr(xlErrRef)
                Exit Function
            End If
        End If
     
        If TypeOf dbOutput(dbIndex) Is Range Then
            Set IndirectEx = dbOutput(dbIndex)
        Else
            IndirectEx = dbOutput(dbIndex)
        End If
     
        Exit Function
     
    ClearObject:
        On Error Resume Next
        If Not (vExcel Is Nothing) Then
            vWB.Close False
            vExcel.Quit
            Set vExcel = Nothing
        End If
    End Function
    J'obtiens, en colonne A :
    Nom : Capture.PNG
Affichages : 1299
Taille : 2,0 Ko

  9. #9
    Membre averti
    Homme Profil pro
    Contrôleur de gestion
    Inscrit en
    Novembre 2014
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Contrôleur de gestion

    Informations forums :
    Inscription : Novembre 2014
    Messages : 22
    Par défaut
    Merci pour votre retour.

    alors je suis sincèrement désolé, mais je n'y arrive pas. Donc je suis reparti de zéro puis j'ai mis la macro workbook_open dans ThisWorkBook, j'ai créé un module ou j'ai déclaré vExcel en publique et j'ai copier coller la macroq ue vous avez modifié (je n'ai pas remarqué les modifications mis à part la mise en commentaire du Set vExcel.

    Si ça à fonctionné chez vous, je ne comprends pas ce que j'ai mal fait pour que ça ne fonctionne pas chez moi

    J'espère que vous pourrez m'aider à éclaircir la situation.

    en image :

    Nom : excel1.PNG
Affichages : 1580
Taille : 40,7 Ko

    Nom : VBA1.PNG
Affichages : 2095
Taille : 70,5 Ko

    et voici la totalité du code contenu dans le module 1:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    Public vExcel As Object
     
    '------------------------------------
    'Extended INDIRECT Function v1.0
    '------------------------------------
    'Copyright (c) 2009 Wilson So.
    'E-mail: shwskm@yahoo.com.hk
    '------------------------------------
    'Credits:
    '- Designed and written by Wilson So.
    '- The 'CreateObject("Excel.Application")' trick was inspired by Harlan Grove's PULL function source code.
    '------------------------------------
    'This is an open source. You can freely redistribute and modify it, but please kindly give credit to the contributers.
    'Please also kindly report any bugs/suggestions through e-mail or in the forums where I posted it.
    '------------------------------------
    'How to use:
    '- Basically same as INDIRECT() in Excel - the same concept for the ref_text parameter.
    '- To update the static memory for a particular reference,
    '  type TRUE in the second parameter (just one of the IndirectEx() containing that reference)
    '  and calculate it once.
    '------------------------------------
    'Features:
    '- You can refer to the closed workbook data.
    '- The retrieved closed workbook data will be stored in the static memory,
    '  so in the next time, the closed workbook will not be opened again for fast retrieve.
    '- A range instead of an array will be returned if the path is omitted in the ref_text,
    '  so it still works fine if the user refers to an enormous array, e.g. "Sheet1!1:65536".
    '- You can use it inside INDEX(), VLOOKUP(), MATCH() etc.
    '- You can use it with OFFSET(), but only for opened workbook data.
    '- The procedure will not blindly retrieve all the data as requested;
    '  it will not retrieve data beyond the "Ctrl + End" cell, in order to keep the memory as small as possible.
    '- #NUM! will be returned in case of lack of memory.
    '- #REF! will be returned in case of a wrong path.
    '- #VALUE! will be returned in case of other errors.
    '------------------------------------
    'Known issues:
    '- Due to the use of SpecialCells(), #VALUE! will be returned if the worksheet for a closed workbook is protected.
    '------------------------------------
     
    Function IndirectEx(ref_text As String, Optional refresh_memory As Boolean = False) As Variant
        On Error GoTo ClearObject
     
        Dim RefName As String
        Dim SheetName As String
        Dim WBName As String
        Dim FolderName As String
     
        Dim vExcel As Object
        Dim vWB As Workbook
     
        Static dbOutput() As Variant
        Static dbKey() As String
        Static dbTotalOutput As Integer
        Dim dbIndex As Integer
     
        Dim UserEndRow As Long, UserEndCol As Integer
        Dim RealEndRow As Long, RealEndCol As Integer
        Dim EndRow As Long, EndCol As Integer
        Dim RangeHeight As Long, RangeWidth As Integer
     
        GetNames ref_text, RefName, SheetName, WBName, FolderName
     
        If dbTotalOutput = 0 Then
            ReDim dbOutput(1 To 1) As Variant
            ReDim dbKey(1 To 1) As String
        End If
     
        For i = 1 To dbTotalOutput
            If dbKey(i) = FolderName & WBName & "!" & SheetName & "!" & RefName Then
                dbIndex = i
            End If
        Next
     
        If dbIndex = 0 Or refresh_memory Then
            If dbIndex = 0 Then
                dbTotalOutput = dbTotalOutput + 1
                dbIndex = dbTotalOutput
                ReDim Preserve dbOutput(1 To dbTotalOutput) As Variant
                ReDim Preserve dbKey(1 To dbTotalOutput) As String
                dbKey(dbIndex) = FolderName & WBName & "!" & SheetName & "!" & RefName
            End If
            If FolderName = "" Then
                Set dbOutput(dbIndex) = Workbooks(WBName).Worksheets(SheetName).Range(RefName)
            ElseIf Dir(FolderName & WBName) <> "" Then
    '            Set vExcel = CreateObject("Excel.Application") 'ligne a modifier
     
                Set vWB = vExcel.Workbooks.Open(FolderName & WBName)
                With vWB.Sheets(SheetName)
                    On Error GoTo ClearObject
                    UserEndRow = .Range(RefName).Row + .Range(RefName).Rows.Count - 1
                    UserEndCol = .Range(RefName).Column + .Range(RefName).Columns.Count - 1
                    RealEndRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
                    RealEndCol = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
                    EndRow = IIf(UserEndRow < RealEndRow, UserEndRow, RealEndRow)
                    EndCol = IIf(UserEndCol < RealEndCol, UserEndCol, RealEndCol)
                    RangeHeight = EndRow - .Range(RefName).Row + 1
                    RangeWidth = EndCol - .Range(RefName).Column + 1
                    On Error Resume Next
                    dbOutput(dbIndex) = .Range(RefName).Resize(RangeHeight, RangeWidth).Value
                    If Err.Number <> 0 Then
                        IndirectEx = CVErr(xlErrNum)
                        GoTo ClearObject
                    End If
                End With
                On Error GoTo ClearObject
                vWB.Close False
                vExcel.Quit
                Set vExcel = Nothing
            Else
                IndirectEx = CVErr(xlErrRef)
                Exit Function
            End If
        End If
     
        If TypeOf dbOutput(dbIndex) Is Range Then
            Set IndirectEx = dbOutput(dbIndex)
        Else
            IndirectEx = dbOutput(dbIndex)
        End If
     
        Exit Function
     
    ClearObject:
        On Error Resume Next
        If Not (vExcel Is Nothing) Then
            vWB.Close False
            vExcel.Quit
            Set vExcel = Nothing
        End If
    End Function
     
    Private Sub GetNames(ByVal ref_text As String, ByRef RefName As String, ByRef SheetName As String, ByRef WBName As String, ByRef FolderName As String)
        Dim P_e As Integer
        Dim P_b1 As Integer
        Dim P_b2 As Integer
        Dim P_s As Integer
     
        P_e = InStr(1, ref_text, "!")
        P_b1 = InStr(1, ref_text, "[")
        P_b2 = InStr(1, ref_text, "]")
        P_s = InStr(1, ref_text, ":\")
     
        If P_e = 0 Then
            RefName = ref_text
        Else
            RefName = Right$(ref_text, Len(ref_text) - P_e)
        End If
        RefName = Replace$(RefName, "$", "")
     
        If P_e = 0 Then
            SheetName = Application.Caller.Parent.Name
        ElseIf P_b1 = 0 Then
            SheetName = Left$(ref_text, P_e - 1)
        Else
            SheetName = Mid$(ref_text, P_b2 + 1, P_e - P_b2 - 1)
        End If
        SheetName = Replace$(SheetName, "'", "")
     
        If P_b1 = 0 Then
            WBName = Application.Caller.Parent.Parent.Name
        Else
            WBName = Mid$(ref_text, P_b1 + 1, P_b2 - P_b1 - 1)
        End If
     
        If P_s = 0 Then
            FolderName = ""
        Else
            FolderName = Left$(ref_text, P_b1 - 1)
        End If
        If Left$(FolderName, 1) = "'" Then FolderName = Right$(FolderName, Len(FolderName) - 1)
    End Sub

  10. #10
    Expert confirmé
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 208
    Par défaut
    Essaie en remplaçant le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    Function IndirectEx(ref_text As String, Optional refresh_memory As Boolean = False) As Variant
    '    On Error GoTo ClearObject
     
        Dim RefName As String
        Dim SheetName As String
        Dim WBName As String
        Dim FolderName As String
     
    '    Dim vExcel As Object
        Dim vWB As Workbook
     
        Static dbOutput() As Variant
        Static dbKey() As String
        Static dbTotalOutput As Integer
        Dim dbIndex As Integer
     
        Dim UserEndRow As Long, UserEndCol As Integer
        Dim RealEndRow As Long, RealEndCol As Integer
        Dim EndRow As Long, EndCol As Integer
        Dim RangeHeight As Long, RangeWidth As Integer
     
        GetNames ref_text, RefName, SheetName, WBName, FolderName
     
        If dbTotalOutput = 0 Then
            ReDim dbOutput(1 To 1) As Variant
            ReDim dbKey(1 To 1) As String
        End If
     
        For i = 1 To dbTotalOutput
            If dbKey(i) = FolderName & WBName & "!" & SheetName & "!" & RefName Then
                dbIndex = i
            End If
        Next
     
        If dbIndex = 0 Or refresh_memory Then
            If dbIndex = 0 Then
                dbTotalOutput = dbTotalOutput + 1
                dbIndex = dbTotalOutput
                ReDim Preserve dbOutput(1 To dbTotalOutput) As Variant
                ReDim Preserve dbKey(1 To dbTotalOutput) As String
                dbKey(dbIndex) = FolderName & WBName & "!" & SheetName & "!" & RefName
            End If
            If FolderName = "" Then
                Set dbOutput(dbIndex) = Workbooks(WBName).Worksheets(SheetName).Range(RefName)
            ElseIf Dir(FolderName & WBName) <> "" Then
    '            Set vExcel = CreateObject("Excel.Application") 'ligne a modifier
     
                Set vWB = vExcel.Workbooks.Open(FolderName & WBName)
                With vWB.Sheets(SheetName)
                    On Error GoTo ClearObject
                    UserEndRow = .Range(RefName).Row + .Range(RefName).Rows.count - 1
                    UserEndCol = .Range(RefName).Column + .Range(RefName).Columns.count - 1
                    RealEndRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
                    RealEndCol = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
                    EndRow = IIf(UserEndRow < RealEndRow, UserEndRow, RealEndRow)
                    EndCol = IIf(UserEndCol < RealEndCol, UserEndCol, RealEndCol)
                    RangeHeight = EndRow - .Range(RefName).Row + 1
                    RangeWidth = EndCol - .Range(RefName).Column + 1
                    On Error Resume Next
                    dbOutput(dbIndex) = .Range(RefName).Resize(RangeHeight, RangeWidth).Value
                    If Err.Number <> 0 Then
                        IndirectEx = CVErr(xlErrNum)
                        GoTo ClearObject
                    End If
                End With
                On Error GoTo ClearObject
                vWB.Close False
    '            vExcel.Quit
    '            Set vExcel = Nothing
            Else
                IndirectEx = CVErr(xlErrRef)
                Exit Function
            End If
        End If
     
        If TypeOf dbOutput(dbIndex) Is Range Then
            Set IndirectEx = dbOutput(dbIndex)
        Else
            IndirectEx = dbOutput(dbIndex)
        End If
     
        Exit Function
     
    ClearObject:
        On Error Resume Next
        If Not (vExcel Is Nothing) Then
            vWB.Close False
    '        vExcel.Quit
    '        Set vExcel = Nothing
        End If
    End Function
     
    Private Sub GetNames(ByVal ref_text As String, ByRef RefName As String, ByRef SheetName As String, ByRef WBName As String, ByRef FolderName As String)
        Dim P_e As Integer
        Dim P_b1 As Integer
        Dim P_b2 As Integer
        Dim P_s As Integer
     
        P_e = InStr(1, ref_text, "!")
        P_b1 = InStr(1, ref_text, "[")
        P_b2 = InStr(1, ref_text, "]")
        P_s = InStr(1, ref_text, ":\")
     
        If P_e = 0 Then
            RefName = ref_text
        Else
            RefName = Right$(ref_text, Len(ref_text) - P_e)
        End If
        RefName = Replace$(RefName, "$", "")
     
        If P_e = 0 Then
            SheetName = Application.Caller.Parent.Name
        ElseIf P_b1 = 0 Then
            SheetName = Left$(ref_text, P_e - 1)
        Else
            SheetName = Mid$(ref_text, P_b2 + 1, P_e - P_b2 - 1)
        End If
        SheetName = Replace$(SheetName, "'", "")
     
        If P_b1 = 0 Then
            WBName = Application.Caller.Parent.Parent.Name
        Else
            WBName = Mid$(ref_text, P_b1 + 1, P_b2 - P_b1 - 1)
        End If
     
        If P_s = 0 Then
            FolderName = ""
        Else
            FolderName = Left$(ref_text, P_b1 - 1)
        End If
        If Left$(FolderName, 1) = "'" Then FolderName = Right$(FolderName, Len(FolderName) - 1)
    End Sub

  11. #11
    Membre averti
    Homme Profil pro
    Contrôleur de gestion
    Inscrit en
    Novembre 2014
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Contrôleur de gestion

    Informations forums :
    Inscription : Novembre 2014
    Messages : 22
    Par défaut
    Bonjour,

    Je viens à l'instant de tester votre solution, et ça avance ! J'ai maintenant des #VALEUR à la place des 0 mais c'est une bonne nouvelle, ca à changé par rapport à hier !
    En revanche, la macro ne me renvoie rien d'autre que les #VALEUR.

    Je vais finir par penser qu'il est impossible de modifier cette fonction IndirectEx()

    En tout cas, merci pour votre aide Daniel.C, et si vous avez d'autres idées, je suis preneur !

    Bonne journée

  12. #12
    Modérateur
    Avatar de AlainTech
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Mai 2005
    Messages
    4 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2005
    Messages : 4 235
    Par défaut
    Bonjour,

    Je n'ai pas fait de test mais je n'ai pas compris le but de créer une instance d'Excel alors qu'on est déjà dans l'application.

    De plus, je ne vois pas d'Application.Volatile. Utile quand une fonction est utilisée dans une formule Excel.
    N'oubliez pas de cliquer sur quand vous avez obtenu ou trouvé vous-même la réponse à votre question.
    Si vous trouvez seul, pensez à poster votre solution. Elle peut servir à d'autres!
    Pensez aussi à voter pour les réponses qui vous ont aidés.
    ------------
    Je dois beaucoup de mes connaissances à mes erreurs!

  13. #13
    Membre averti
    Homme Profil pro
    Contrôleur de gestion
    Inscrit en
    Novembre 2014
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Contrôleur de gestion

    Informations forums :
    Inscription : Novembre 2014
    Messages : 22
    Par défaut
    Bonjour,

    Excusez moi, n'étant pas doué en VBA, en quoi peut me servire ce que vous dites AlainTech ?
    Par rapport à ce que vous dites, auriez-vous vue une possible solution ?

    Bonne journée,

  14. #14
    Modérateur
    Avatar de AlainTech
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Mai 2005
    Messages
    4 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2005
    Messages : 4 235
    Par défaut
    Bonjour,

    J'ai compris pourquoi lancer une 2ème instance d'Excel.
    Il est impossible d'ouvrir un classeur dans l'instance en cours car une cellule est en mode édition.
    Je continuerai les tests dès que j'aurai du temps. Si personne d'autre ne trouve de solution entretemps...
    N'oubliez pas de cliquer sur quand vous avez obtenu ou trouvé vous-même la réponse à votre question.
    Si vous trouvez seul, pensez à poster votre solution. Elle peut servir à d'autres!
    Pensez aussi à voter pour les réponses qui vous ont aidés.
    ------------
    Je dois beaucoup de mes connaissances à mes erreurs!

  15. #15
    Expert confirmé
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 208
    Par défaut
    Bonjour,

    En recopiant le tout dans un nouveau classeur :

    test2.xlsm

  16. #16
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Décembre 2013
    Messages : 38
    Par défaut
    Citation Envoyé par Daniel.C Voir le message
    Bonjour,

    En recopiant le tout dans un nouveau classeur :

    test2.xlsm
    Bonjour Daniel,

    Désolé de déterrer le sujet.

    J'ai regardé le fichier "test2.xlsm" que vous avez joint.

    Toutefois, il ne résout pas le problème. On obtient toujours des 0 en retour.

    Les fonctions Pull() et IndirectEx() fonctionnent toutes les deux bien en effet. Toutefois, à l'ouverture du fichier, rien que pour ces quelques lignes, ça un peu plus d'une minute à ouvrir (Et Excel semble crasher un instant).

    Serait il possible de résoudre ce problème de lenteur?

    D'avance merci @tous!

  17. #17
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Décembre 2013
    Messages : 38
    Par défaut
    Bonjour @toute l'équipe!

    Une petite relance svp

  18. #18
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Décembre 2013
    Messages : 38
    Par défaut
    Up

  19. #19
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Décembre 2013
    Messages : 38
    Par défaut
    Personne?

Discussions similaires

  1. Aide pour Macro VBA copie lignes entre 2 classeur
    Par magicsismic dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 30/03/2015, 21h13
  2. Macro pour copier feuille voulu d'un classeur fermé sur classeur ouvert
    Par Nicojez dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 07/06/2012, 15h46
  3. macro pour reporter dans classeur fermé
    Par varik dans le forum Macros et VBA Excel
    Réponses: 10
    Dernier message: 29/03/2012, 21h04
  4. conception pour macro gérant d'autres macros et pour macros gérées
    Par buzz73 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 29/12/2008, 17h39
  5. Aide sur les macros Excel pour recopie auto de données
    Par nicoduhavre dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 15/11/2005, 08h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo