macros只在活动工作表中打印,与被引用的工作表相反

我写了一个VBAmacros,试图从用户窗体中的文本框中input值并将其复制到特定工作表上的单元格中。 我还写了一个countA函数,允许我每次按下inputbutton时写入一个新行。 由于我无法理解的原因,无论我参考什么表格,它只会写入活动工作表。 请帮忙!

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(工作表(“T5input表”),(表(2)),(表(“表2​​”)等,但没有打印到任何东西,但活动单元格。错误?

您使用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 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这是你坚持目标工作表名称的地方。