2014-02-24 17 views
3

我編寫了一個VBA宏,該宏嘗試從用戶窗體中的文本框中獲取值並將其複製到特定工作表上的單元格中。我還寫了一個countA函數,允許我在每次按下輸入按鈕時寫入新行。由於我無法理解的原因,無論我參考什麼表格,它都只會寫入活動工作表。請幫忙!宏只打印在與參考表相對的活動工作表中

Private Sub inputlight_Click() 

Dim emptyrow As Long 

'Find the first empty row after row 47 on sheet "T5 Input Sheet" 

emptyrow = 47 + WorksheetFunction.CountA(Sheets("T5 Input Sheet").Range("b48:b219")) + 1 

'transfer data 

    Cells(emptyrow, 2).Value = esize.Value 
    Cells(emptyrow, 3).Value = etype.Value 
    Cells(emptyrow, 4).Value = ewatt.Value 
    Cells(emptyrow, 5).Value = elamps.Value 
    Cells(emptyrow, 6).Value = eusage.Value 
    Cells(emptyrow, 7).Value = efixtures.Value 

End Sub 

我試圖將其更改爲COUNTA(工作表( 「T5輸入表」),(表(2)),(表( 「Sheet2的」)等,但它們都沒有打印到任何東西,但活動單元格。我在做什麼錯?

+0

您需要限定'Cells' – 2014-02-24 14:15:25

回答

0

你把使用​​功能,但是你也需要限定你的細胞,以堅持價值特定工作表正確的做法。

Private Sub inputlight_Click() 

    Dim emptyrow As Long 

    'Find the first empty row after row 47 on sheet "T5 Input Sheet" 
    emptyrow = 47 + WorksheetFunction.CountA(Sheets("T5 Input Sheet").Range("b48:b219")) + 1 

    'transfer data 
    With Sheets("SHEET_NAME") 

     .Cells(emptyrow, 2).Value = esize.Value 
     .Cells(emptyrow, 3).Value = etype.Value 
     .Cells(emptyrow, 4).Value = ewatt.Value 
     .Cells(emptyrow, 5).Value = elamps.Value 
     .Cells(emptyrow, 6).Value = eusage.Value 
     .Cells(emptyrow, 7).Value = efixtures.Value 
    End With 

End Sub 

那麼,它表示SHEET_NAME這是您粘貼目標工作表名稱的位置。

+0

沒關係我忘記了.Cells中的這段時間;非常感謝你回答我的問題! – Giraffe

+0

@ user3346984沒問題。 – 2014-02-24 14:29:31