コンボボックス付きのユーザーフォームがあります。このコンボボックスは別のブックからのリストで埋められます。ソースワークシートへのアクセス権がある場合、ユーザーフォームのコンボボックスが表示されます。
私の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
エラー処理を使用する必要があります。私には欠落している権利のためのエラーコードはありませんが、簡単に見つけることができます(ソースブックにアクセスできないコンピュータの1つをテストしてください) – R3uK
私はそれを再構築しようとしましたソースファイルを開くことができる場合にのみ、ボックスをクリアし、ボックスを埋めるよう試みます。 debug.printを使ってみましたが、それを動作させるスキルはありません – Mick17
"最後に更新されたバージョン"とは何を意味していますか?そのバージョンを実行してソースファイルに正常にアクセスしたときに他の誰かのコンピュータのメモリにあるバージョンを意味するのであれば、そのリストを新しいユーザがアクセスできるどこかにコピーせずにはできません。 ofのソースファイル)。コードを最後に更新したときにコード内に存在していたリストのバージョンを意味する場合、それは機能します。これはエラー処理を追加するだけです。 – YowE3K