基本上我需要从第一张纸上获取每种不同类型问题的清单,然后在第二张纸上显示。
第一张工作表有1行,它只是列的标题,然后是4行数据。
这是我的意思,但我认为我意外地改变了一些我现在无法解决的问题,如果有人能够看到一个问题,或者如果有更好的方法可以做到这一点,那么我会全神贯顺。
Sub ListQueryTypes()
'Create Variables'
Dim numberOfIssues As Integer
Dim numberOfMacroIssues As Integer
Dim numberOfReportIssues As Integer
Dim numberOfTechnicalIssues As Integer
Dim numberOfTrendIssues As Integer
Dim cellValue As String
'Set query register as active worksheet'
Sheets("Query Register").Activate
'set range for search'
Set myRange = Range("G:G")
'Minus 1 for the first row being column name'
numberOfIssues = Application.WorksheetFunction.CountA(myRange)
'Do, for as many issues as were found previously'
For i = 2 To numberOfIssues + 1
cellValue = ActiveSheet.Cells(i, 7).Value
Select Case cellValue
Case "Macro"
numberOfMacroIssues = numberOfMacroIssues + 1
Case "Report"
numberOfReportIssues = numberOfReportIssues + 1
Case "Technical"
numberOfTechnicalIssues = numberOfTechnicalIssues + 1
Case "Trend"
numberOfTrendIssues = numberOfTrendIssues + 1
End Select
Next i
Sheets("Inventory").Activate
ActiveCell = Cells(2, 2)
ActiveCell.Value = numberOfMacroIssues
ActiveCell.Offset(1).Value = numberOfReportIssues
ActiveCell.Offset(2).Value = numberOfTechnicalIssues
ActiveCell.Offset(3).Value = numberOfTrendIssues
答案 0 :(得分:0)
收集评论中提到的所有问题,以改进您的代码。看看代码中的注释
Option Explicit 'force to declare all variables correctly (this way you don't forget any)
Public Sub ListQueryTypes()
'Create Variables'
Dim numberOfIssues As Long 'was Integer but we can always use Long (only advantages) and Excel has more rows than Integer can handle
Dim numberOfMacroIssues As Long
Dim numberOfReportIssues As Long
Dim numberOfTechnicalIssues As Long
Dim numberOfTrendIssues As Long
'Dim cellValue As String 'you don't need that variable see below
'Set query register as active worksheet'
'Sheets("Query Register").Activate 'instead of activate we define that sheet as variable which is more save
'and we use Worksheets
Dim WsQueryReg As Worksheet
Set WsQueryReg = ThisWorkbook.Worksheets("Query Register")
'set range for search'
Dim myRange As Range 'we should to declare all variable properly
Set myRange = WsQueryReg.Range("G:G") 'we need to define in which sheet the range is
'Minus 1 for the first row being column name'
numberOfIssues = Application.WorksheetFunction.CountA(myRange)
'Do, for as many issues as were found previously'
Dim i As Long 'declare every variable first
For i = 2 To numberOfIssues + 1
'cellValue = WsQueryReg.Cells(i, 7).Value 'no need to write this in a variable we can use it directly in Select Case
Select Case WsQueryReg.Cells(i, 7).Value 'was cellValue before
Case "Macro"
numberOfMacroIssues = numberOfMacroIssues + 1
Case "Report"
numberOfReportIssues = numberOfReportIssues + 1
Case "Technical"
numberOfTechnicalIssues = numberOfTechnicalIssues + 1
Case "Trend"
numberOfTrendIssues = numberOfTrendIssues + 1
End Select
Next i
'we can use with instead of activating a sheet and selecting a cell
'this also specifies in which sheet and workbook the cell is
With ThisWorkbook.Worksheets("Inventory").Cells(2, 2)
.Value = numberOfMacroIssues
.Offset(1).Value = numberOfReportIssues
.Offset(2).Value = numberOfTechnicalIssues
.Offset(3).Value = numberOfTrendIssues
End With
End Sub
您可以计算For i = 2 To numberOfIssues + 1
,Macro
等,而不是遍历您的行Report
,而不是
With WsQueryReg
numberOfMacroIssues = Application.WorksheetFunction.CounfIf(.Range(.Cells(2, 7), .Cells(numberOfIssues + 1, 7)), "Macro")
numberOfReportIssues = Application.WorksheetFunction.CounfIf(.Range(.Cells(2, 7), .Cells(numberOfIssues + 1, 7)), "Report")
'… others here
End With