Excel VBA不显示结果

时间:2018-09-07 22:54:45

标签: excel

我是VBA的初学者,因此希望获得一些帮助。我正在尝试建立一个目录,以根据用户对“类别”和“子类别”的选择来显示结果。运行脚本时,没有错误消息,但看不到任何结果显示。我的代码有什么问题吗?

Sub Button5_Click()

'''Function to search data set

Dim wkbk As Workbook
Dim row_data As Range
Dim results_ws As Worksheet, data_ws As Worksheet
Dim n_row As Integer '''counter for row number
Dim main_ws As Worksheet
Dim category As String, subcategory As String

Set wkbk = ThisWorkbook
Set results_ws = wkbk.Sheets("Demo_Results")
Set data_ws = wkbk.Sheets("Demo_Data")
Set main_ws = ThisWorkbook.Sheets("Demo_Main")

'''Clear the results worksheet
results_ws.Cells.ClearContents
results_ws.Hyperlinks.Delete
results_ws.Cells.Font.Underline = False
results_ws.Columns("B:E").Font.Color = RGB(0, 0, 0)

category = main_ws.Cells(13, "F").Value
subcategory = main_ws.Cells(15, "F").Value
result_count = 0

final_data_row = data_ws.Range("A1000").End(xlUp).Row

For n_row = 2 To final_data_row:
    If data_ws.Cells(n_row, "A") = category And data_ws.Cells(n_row, "B") = subcategory Then
        With data_ws
            Set row_data = data_ws.Range(data_ws.Cells(n, 1), data_ws.Cells(n, 7))
            result_count = result_count + 1
            results_ws.Cells(Rows.Count, "E").End(xlUp).Offset(2, -3).Value = Str(result_count) + ".)"
            results_ws.Cells(Rows.Count, "E").End(xlUp).Offset(2, -2).Value = row_data.Cells(1, 3).Value

            '''Enter state
            results_ws.Cells(Rows.Count, "C").End(xlUp).Offset(1, 1).Value = "State:"
            results_ws.Cells(Rows.Count, "D").End(xlUp).Offset(0, 1).Value = row_data.Cells(1, 4).Value
        End With
    End If
Next n_row

'''Format table
Sheets("Demo_Results").Select
Call ResetFormatting
ActiveWindow.ScrollRow = 1

End Sub

Function ResetFormatting():
'''Resent font style and size for results column
Dim wsheet As Worksheet
Set wsheet = ThisWorkbook.Sheets("Demo_Results")

wsheet.Cells.Font.Name = "Verdana"
wsheet.Cells.Font.Size = 11

End Function

0 个答案:

没有答案