2016-12-28 70 views
1

这是我的代码。我正在计算许可证,我需要另一张表中每种类型的许可证总数。VBA EXCEL:我如何在另一张纸上输出

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 

点击按钮后,我想要在sheet2中输出。 你可以让我知道如何在另一个工作表中获得输出。 提前感谢你。 :)

+0

劳驾给反馈[此相关,你的前面的问题(http://stackoverflow.com/questions/41355766/VBA的Excel的操作方法 - 比较 - 内容的-A-细胞对特定的弦)。谢谢 – user3598756

回答

1

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

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 
+0

如何创建新的工作表? 如果“结果”表预先不存在于工作簿中,该怎么办? 我希望输出创建一个新的工作表,然后在该工作表中显示输出。 – user007

+0

查看编辑答案。如果这解决了你的问题,那么你可能想标记答案为接受,谢谢! – user3598756

0
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 

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

相关问题