2016-12-28 35 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,你可以直接使用它作爲一個值。我希望這有幫助。

相關問題