Основы офисного программирования и язык VBA


Пример 2.3


Private Sub CommandButton5_Click()

ChooseBook

End Sub

Public Sub ChooseBook()

'Выбор книги, с которой будет работать пользователь

Const PathDir As String = "e:\O2000\CD2000\Ch2\"

Dim Answer As Variant

Dim CurrentBook As String

Dim Book As Workbook

Dim Found As Boolean

Answer = InputBox(prompt:= _

"Выберите книгу, с которой будете работать (1/2/3)", Default:=1)

Select Case Answer

Case 1

CurrentBook = "BookOne.xls"

Case 2

CurrentBook = "BookTwo.xls"

Case 3

CurrentBook = "BookThree.xls"

Case Else

CurrentBook = "I don't know such book"

End Select

MsgBox ("Your choose is " & CurrentBook)

'Проверяем есть ли уже книга в коллекции

Found = False

For Each Book In Workbooks

If Book.Name = CurrentBook Then

Found = True

Exit For

End If

Next Book

If Found Then

'Активизируем выбранную книгу

Workbooks(CurrentBook).Activate

ElseIf CurrentBook = "I don't know such book" Then

MsgBox ("I don't know such book")

Else 'Добавляем книгу в коллекцию

Workbooks.Open PathDir & CurrentBook

Workbooks(CurrentBook).Activate

End If

End Sub




Начало  Назад  Вперед