【问题标题】:Macro is only printing in active worksheet opposed to the referenced sheet宏仅在与引用工作表相反的活动工作表中打印
【发布时间】:2014-03-26 06:17:17
【问题描述】:

我编写了一个 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(Worksheets("T5 Input Sheet"), (Sheets(2)), (Sheets("Sheet2") 等,但它们都不会打印到活动单元格之外的任何内容。什么我做错了吗?

【问题讨论】:

  • 您需要资格Cells

标签: vba excel


【解决方案1】:

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

【讨论】:

  • 没关系,我忘记了 .Cells 中的句号;非常感谢您回答我的问题!
  • @user3346984 没问题。
猜你喜欢
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多