奇怪的activecell.offset输出

时间:2018-08-10 17:50:50

标签: excel vba excel-vba offset

Sub Link()


    Dim Turbidity As Long
    Dim RawTurbidity As Range


    'Sets variables Turbidity being the ActiveCell and RawTurbidity referring to the last captured cell in raw sheets'
    Turbidity = ActiveCell.Row
    Set RawTurbidity = Sheets("Raw Data").Range("C4").End(xlDown)


    'The formula assigning the last captured cell in Raw sheets to the active cell '
    Sheet1.Range(Sheet1.Cells(Turbidity, 4), Sheet1.Cells(Turbidity, 4)).Formula = RawTurbidity

End Sub

这是我拥有的代码,当前它正在执行预期的操作。我们有两个工作表atm sheet1和Raw Data。一个仪器将数据从C4开始,一直向下输出到Raw数据的C列中。我实质上编写的当前代码将仪器吐出的最新值粘贴到sheet1中的活动单元格中。我在Raw Data上有一个代码,该代码仅在对C4列及更低的列进行更改时才运行宏。它确实可以实现我想要的方式...

我的问题是,当我添加activecell.offset(1,0).select时,为了使activecell自动转到sheet1的下一行,而无需移动鼠标,宏复制并将相同的数据粘贴到接下来的4个单元格。如果我的仪器比这次再次吐出数据,它将占用具有相同数据的接下来的6行。

2 个答案:

答案 0 :(得分:1)

乔B,我认为您正在为此加倍努力。 工作表列中的最后一个值是否被复制到另一张工作表中指定列中的下一个打开行?是吗?

Option Explicit

Sub Link()

    Dim ws1 As Worksheet
    Dim wsRaw As Worksheet

    Dim ws1LastRow As Long ' "Turbidity"
    Dim wsRawLastRow As Long ' "RawTurbidity"

    ' I suggest you just name the sheets using the developer prop window
    'It cuts this whole part out as you can call them directly
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set wsRaw = ThisWorkbook.Worksheets("Raw Data")

    ws1LastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row 'lets say you are pasting to column A

    'ws1LastRow = ws1LastRow + 1
    'There you go the next writable cell row, this is wasted code though, see below you just increment when you need it

    wsRawLastRow = wsRaw.Cells(wsRaw.Rows.Count, "C").End(xlUp).Row 'This method doesn't care if your data starts in C4

    'No formula needed, it is a straight "copy" here, actually faster as its an assignment
    ws1.Cells(ws1LastRow + 1, "A").Value = wsRaw.Cells(wsRawLastRow, "C").Value
    'the next open cell (defined by row) in your sheet 1 column is equal to the last row of your Raw Data sheet column

End Sub

答案 1 :(得分:0)

问题是第一页中的数据未按顺序输入。一个人可能需要将计算出的数据存储到第10行,而下一个计算则需要存储在第20行,因此需要将数据复制到活动单元格中。

这对我不好,因为在最初的帖子中没有说明,这是产生这种奇怪公式的主要原因。