VBA EXCEL:我怎样才能在另一张纸上输出

这是我的代码。 我正在计算许可证,我需要另一张表中每种types的许可证总数。

Sub Button1_Click() 'Initialising no of licenses to 0 Dim transientLicense As Integer transientLicense = 0 Dim steadyLicense As Integer steadyLicense = 0 Dim staticLicense As Integer staticLicense = 0 'checking conditions transientLicense = Sum(CountIfs(Channeltype, "'=Radial Vibration' Or '=Acceleration' Or '=Acceleration2' Or '=Velocity' Or '=Velocity'", Keyphasor, "=Yes", ActiveInactive, "=Active")) steadyLicense = Sum(CountIfs(Channeltype, "'=Radial Vibration' Or '=Acceleration' Or '=Acceleration2' Or '=Velocity' Or '=Velocity'", Keyphasor, "=No", ActiveInactive, "=Active")) staticLicense = Sum(CountIfs(Channeltype, "'=Thrust Position' Or '=Temperature' Or '=Pressure'", ActiveInactive, "=Active")) Application.ScreenUpdating = False Sheet2.Visible = xlSheetVisible ' changes the format of sheet 3 to text 'Sheet2.Cells.NumberFormat = "@" Sheets("Sheet2").Activate 'Writes header Sheet2.Select Range("b2").Value = "Transient Licenses" Range("c2").Value = "Steady Licenses" Range("d2").Value = "Static Licenses" 'writes new table in sheet 2 Columns("B:B").Select Selection.ColumnWidth = 20 Columns("C:C").Select Selection.ColumnWidth = 20 Columns("D:D").Select Selection.ColumnWidth = 20 End Sub 

点击button后,我想要在sheet2中输出。 你可以让我知道如何在另一个工作表中输出。 提前感谢你。 🙂

说你想写输出到工作表“结果”:

  With Worksheets.Add .Name = "Results" .Columns("B:D").ColumnWidth = 20 .Range("B2:D2").Value = Array("Transient Licenses", "Steady Licenses", "Static Licenses") .Range("B3:D3").Value = Array(transientLicense, steadyLicense, staticLicense) End With 
 Sheets("Sheet2").Activate 'Writes header Sheet2.Select Range("b2").Value = "Transient Licenses" Range("c2").Value = "Steady Licenses" Range("d2").Value = "Static Licenses" 

避免使用.Activate.Select尽可能。

最好的方式来改革你已经做了什么,并包括下面的值是:

 'Writes header With Sheets(2) .Range("b2").Value = "Transient Licenses" .Range("c2").Value = "Steady Licenses" .Range("d2").Value = "Static Licenses" .Columns("B:D").ColumnWidth = 20 .Range("b3").Value = transientLicense .Range("c3").Value = steadyLicense .Range("d3").Value = staticLicense End With 

正如你所看到的,你可以直接引用一个单元格的值而不必select它,因为你有一个命名的Integer,你可以直接使用它作为一个值。 我希望这有帮助。