enter image description here我有一個工作簿,其中包含針對S & P 500中的每個行業部門的不同工作表,即Tech,Energy,Ect。我創建了一個帶有兩個列表框的用戶窗體,允許用戶首先選擇一個扇區,然後選擇該扇區獨有的子扇區。列表框工作得很好,但現在我想創建一個命令按鈕,用於選擇用戶選擇的任何子扇區,並使活動工作表中包含該子扇區的第一行數據成爲活動單元格。使用列表框選擇選擇並轉到單元格
Private Sub GoToSectorButton_Click()
'Declare variables
Dim SubIndustry As String
Dim IntRow As Integer
'Set list box value equal to the variable
SubIndustry = lstSubIndustry.Value
'Locate the first occurance of the Sub Industry
IntRow = 3
'Select the row that contains
ActiveSheet.cell(SubIndustry).Select
End Sub
Private Sub UserForm_Initialize()
'declare variable
Dim shtIndustry As Worksheet
'shows Industries in lstIndustry that aren't the first set of sets
For Each shtIndustry In Application.Workbooks("VBA_Finance_Project_KEZE6983.xlsm").Worksheets
If shtIndustry.Name <> "Welcome" And shtIndustry.Name <> "Name Or Sector" And shtIndustry.Name <> "Name" And shtIndustry.Name <> "Sector" And shtIndustry.Name <> "Filter" And shtIndustry.Name <> "Master" Then
lstIndustry.AddItem (shtIndustry.Name)
End If
Next shtIndustry
'select default list box item
lstIndustry.ListIndex = 0
End Sub
Private Sub lstIndustry_Click()
'declare variables
Dim strSI As String, rngData As Range, rngCell As Range, shtSubIndustry As Worksheet
'clear list box
lstSubIndustry.Clear
'Save relevant worksheets to a vaiable so that we can use the vaiable in the rest of the program as a shortcut
Set shtSubIndustry = Application.Workbooks("VBA_Finance_Project_KEZE6983.xlsm").Worksheets(lstIndustry.ListIndex + 5)
'activate worksheet clicked
shtSubIndustry.Activate
'assign address of Industry data to rngData variable
Set rngData = Application.Workbooks("VBA_Finance_Project_KEZE6983.xlsm").ActiveSheet.Range("A3").CurrentRegion
'assign Column heading to srtSI variable
strSI = "GICS Sub Industry"
'Add the Sub Industry
For Each rngCell In rngData.Columns(14).Cells
If rngCell.Value <> strSI And rngCell.Value <> "" Then
lstSubIndustry.AddItem rngCell.Value
strSI = rngCell.Value
End If
Next rngCell
'select default list box item
lstSubIndustry.ListIndex = 0
End Sub
這並不工作跟它無效的或不合格的參考。任何想法如何解決這個問題? –
Eric K在我的答案中編輯了代碼,現在可以工作。如果你需要進一步的幫助,你必須更具體。你在哪一行得到錯誤?什麼是突出顯示? – CHawk