VBA EXCEL:我如何在另一张纸上获得输出

时间:2016-12-28 09:00:19

标签: excel vba excel-vba

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

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中输出。 你可以告诉我如何在另一张工作表中获取输出。 非常感谢你。 :)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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,您可以直接将其用作值。我希望这会有所帮助。