2017-02-13 9 views
-1

コンボボックス付きのユーザーフォームがあります。このコンボボックスは別のブックからのリストで埋められます。ソースワークシートへのアクセス権がある場合、ユーザーフォームのコンボボックスが表示されます。

私のuserformを使用する必要のあるユーザーには、コンボボックスソースブックを開く権限がありません。

私がしたいことは、正しい権利を持つ誰かがuserformを使用するたびにコンボボックスを更新するようにコードを再構築することです。

誰かが権利を持っていない場合は、コンボボックスのアップをスキップして、最後に更新されたバージョンで作業する必要があります。

これは私の必要性を満たすためにどのように変更する必要があるか、私のコードです。

Dim ListItems As Variant, i As Integer 
Dim SourceWB As Workbook 
    With Me.cmbOperators 
    .Clear ' remove existing entries from the listbox 
    ' turn screen updating off, 
    ' prevent the user from seeing the source workbook being opened 
    Application.ScreenUpdating = False 
    ' open the source workbook as ReadOnly 
    Set SourceWB = Workbooks.Open("path", _ 
     False, True) 
    ListItems = SourceWB.Worksheets(1).Range("B2:B121").Value 
    ' get the values you want 
    SourceWB.Close False ' close the source workbook without saving changes 
    Set SourceWB = Nothing 
    ListItems = Application.WorksheetFunction.Transpose(ListItems) 
    ' convert values to a vertical array 
    For i = 1 To UBound(ListItems) 
     .AddItem ListItems(i) ' populate the listbox 
    Next i 
    .ListIndex = -1 ' no items selected, set to 0 to select the first item 
    Application.ScreenUpdating = True 
End With 

私はこのファイルには権利がありませんので、今すぐテストできません。次のことができますか?最後に始まり

On error goto errorhandler 

errorhandler: 
check=0 
resume next 

私の変更されたコード:

Dim ListItems As Variant, i As Integer 
Dim SourceWB As Workbook 
With Me.cmbOperators 
check = 1 
    '.Clear ' remove existing entries from the listbox 
    ' turn screen updating off, 
    ' prevent the user from seeing the source workbook being opened 
    Application.ScreenUpdating = False 
    ' open the source workbook as ReadOnly 
    Set SourceWB = Workbooks.Open("\\rsdfp1\mteam\Bestanden SVs\Employees overview\Masterfile Employees Production.xlsx", _ 
     False, True) 
    If check = 1 Then .Clear 
    ListItems = SourceWB.Worksheets(1).Range("B2:B121").Value 
    ' get the values you want 
    SourceWB.Close False ' close the source workbook without saving changes 
    Set SourceWB = Nothing 
    ListItems = Application.WorksheetFunction.Transpose(ListItems) 
    ' convert values to a vertical array 
    For i = 1 To UBound(ListItems) 
     .AddItem ListItems(i) ' populate the listbox 
    Next i 
    .ListIndex = -1 ' no items selected, set to 0 to select the first item 
    Application.ScreenUpdating = True 
End With 
+0

エラー処理を使用する必要があります。私には欠落している権利のためのエラーコードはありませんが、簡単に見つけることができます(ソースブックにアクセスできないコンピュータの1つをテストしてください) – R3uK

+0

私はそれを再構築しようとしましたソースファイルを開くことができる場合にのみ、ボックスをクリアし、ボックスを埋めるよう試みます。 debug.printを使ってみましたが、それを動作させるスキルはありません – Mick17

+0

"最後に更新されたバージョン"とは何を意味していますか?そのバージョンを実行してソースファイルに正常にアクセスしたときに他の誰かのコンピュータのメモリにあるバージョンを意味するのであれば、そのリストを新しいユーザがアクセスできるどこかにコピーせずにはできません。 ofのソースファイル)。コードを最後に更新したときにコード内に存在していたリストのバージョンを意味する場合、それは機能します。これはエラー処理を追加するだけです。 – YowE3K

答えて

0

は、この場合のエラー処理のために、簡単なOn Error Resume Next使用し、その後、SourceWBが開かれているかどうかをテストすることができます。それはあなたの元のコードに存在していたよう

Dim ListItems As Variant, i As Integer 
Dim SourceWB As Workbook 
    With Me.cmbOperators 
     ' turn screen updating off, 
     ' prevent the user from seeing the source workbook being opened 
     Application.ScreenUpdating = False 
     ' open the source workbook as ReadOnly 
     On Error Resume Next 
     Set SourceWB = Workbooks.Open("path", _ 
             False, True) 
     On Error GoTo 0 
     If Not SourceWB Is Nothing Then 
      .Clear ' remove existing entries from the listbox 
      ListItems = SourceWB.Worksheets(1).Range("B2:B121").Value 
      ' get the values you want 
      SourceWB.Close False ' close the source workbook without saving changes 
      Set SourceWB = Nothing 
      ListItems = Application.WorksheetFunction.Transpose(ListItems) 
      ' convert values to a vertical array 
      For i = 1 To UBound(ListItems) 
       .AddItem ListItems(i) ' populate the listbox 
      Next i 
     End If 
     .ListIndex = -1 ' no items selected, set to 0 to select the first item 
     Application.ScreenUpdating = True 
    End With 

しかし、これは、リストでユーザーを残します - それは、誰かが成功したソースファイルにアクセスした最後の時間だったので、それはは、リストを与えることはありません。

+0

もし私が.clearを削除し、もしそうでなければme.cmboperators.clearを追加したら、最後に更新されたバージョンのようなリストにはなりませんか? – Mick17

+0

私の謝罪 - 私は 'If'の中で' .Clear'を動かすべきだった。誰かがマクロを実行し、 "ソース"ファイルにアクセスできない場合、それらが表示されるリストは、最後にコードを保存したときにリストをハードコードしたものになります。これは、以前のユーザー(「ソース」ファイルへのアクセス権を持っていたユーザー)が見たものではありません。私。前のユーザーがあなたのコードを更新していない、彼らはちょうど彼らのコンピュータのメモリ内のリストを更新しています。 – YowE3K

0

ここでは、あなたのケースでエラー処理を使用する方法である:

私はそれだけではわからないファイルにアクセスするための十分な権限がないというエラーコードは75です。しかし、簡単に変更できます! ;)

Sub test_Mick17() 


    Dim ListItems As Variant, i As Integer 
    Dim SourceWB As Workbook 
    With Me.cmbOperators 

     '.Clear ' remove existing entries from the listbox 
     ' turn screen updating off, 
     ' prevent the user from seeing the source workbook being opened 
     Application.ScreenUpdating = False 



    On Error GoTo ErrorHandler 
     ' open the source workbook as ReadOnly 
     Set SourceWB = Workbooks.Open("\\rsdfp1\mteam\Bestanden SVs\Employees overview\Masterfile Employees Production.xlsx", _ 
     False, True) 
     If Check = 1 Then .Clear 
     ListItems = SourceWB.Worksheets(1).Range("B2:B121").value 
     ' get the values you want 
     SourceWB.Close False ' close the source workbook without saving changes 
     Set SourceWB = Nothing 
     ListItems = Application.WorksheetFunction.Transpose(ListItems) 
     ' convert values to a vertical array 
     For i = 1 To UBound(ListItems) 
     .AddItem ListItems(i) ' populate the listbox 
     Next i 


NoRigths: 
     .ListIndex = -1 ' no items selected, set to 0 to select the first item 
     Application.ScreenUpdating = True 
    End With 


    Exit Sub 
ErrorHandler: 
    If Err.Number <> 75 Then 
     MsgBox "Error " & Err.Number & vbCrLf & _ 
       Err.Description, vbCritical + vbOKOnly 
     Exit Sub 
    Else 
     Resume NoRigths 
    End If 

    End Sub 
関連する問題