【问题标题】:Keep Reference Cell in New Sheet在新工作表中保留参考单元格
【发布时间】:2017-08-21 22:43:14
【问题描述】:

我基本上是想让在一张纸上采取的行动反映在另一张纸上。将 A1 复制并粘贴到 Sheet1 上的 B1?将 A1 复制并粘贴到 Sheet2 上的 B1。问题是,它总是需要引用活动单元格,我不知道如何以可用的格式保存活动单元格的地址。

这是我想要完成的场景,用非常简单的英语:

  1. 如果 Cell 是 Sheet1 上的 ActiveCell,则在 Sheet2 上的单元格行下方插入行(例如,如果 Sheet1!A1 是活动单元格,则在 Sheet2 第 1 行下方插入行)。

  2. 在 Sheet1 上:复制 ActiveCell.Row 并插入到 ActiveCell.Row 下方。

  3. 在 Sheet2 上:执行相同的操作,但在 Sheet2 上的相应行上,除了我想将其粘贴到步骤 1 中新插入的行中。因此,如果我复制 Sheet1 第 1 行并将其插入到第 1 行下方2,我想复制Sheet2第1行并将其粘贴到步骤1的新行中。

  4. 返回Sheet1,使用InputBox从用户那里获取一个值,将该值插入Range("D" & (ActiveCell.Row))

除了Sheet2 部分外,我已经完成了所有工作,如果在Sheet1 上复制/插入之前无法输入该行,它会破坏公式。我已经手动完成了这些步骤,如果我可以对其进行编码,这一切都可以工作。

子按钮18_Click()

Dim Row_Source As Range
Dim WS As Worksheet, WS2 As Worksheet
Dim Day_Num As String
Dim Day_Dest As Range
Dim PRL As String
Dim Address As String
Dim RowNum As Long

Dim Cell As Range
Set Cell = ActiveCell ' just in case you'll decide to give-up on the "bad practice" of using ActiveCell
RowNum = Cell.Row

Set WS = ThisWorkbook.Sheets("Protocols")
Set WS2 = ThisWorkbook.Sheets("Formulas")

With WS
    PRL = .Range("B" & RowNum).Value

    Day_Num = InputBox("Please enter a day number to add to: " & PRL, "Add New Day")
    If Day_Num <> "" Then
        Set Row_Source = .Rows(RowNum)
    End If
End With

With WS2
    If Day_Num <> "" Then
        Row_Source.Offset(1).Insert Shift:=xlDown
        Application.CutCopyMode = False
    End If
End With

With WS
    If Day_Num <> "" Then
        Row_Source.Copy

        Row_Source.Offset(1).Insert Shift:=xlDown
        Application.CutCopyMode = False

        .Range("D" & RowNum + 1).Value = Day_Num
    End If
End With

With WS2
    If Day_Num <> "" Then
        Set Row_Source = .Rows(RowNum)
        Row_Source.Copy

        Row_Source.Offset(1).Select
        Row_Source.PasteSpecial
        Application.CutCopyMode = False
    End If
End With

结束子

【问题讨论】:

  • re: '我有这一切工作......' 有什么工作?我没有看到代码。阅读minimal reproducible example
  • 糟糕,忘记粘贴了。谢谢。
  • 澄清一下,这只是工作代码。我一直试图将参考地址转移到另一张纸上,所以我没有任何东西可以做其他步骤。
  • @Trevor Sheet2 叫什么名字?
  • Sheet2 是“公式”

标签: vba excel


【解决方案1】:

您正在寻找类似以下代码的内容:

Sub Button18_Click()

    Dim Row_Source As Range
    Dim WS As Worksheet, WS2 As Worksheet
    Dim Day_Num As String
    Dim Day_Dest As Range
    Dim PRL As String
    Dim Address As String
    Dim RowNum As Long

    Dim Cell As Range
    Set Cell = ActiveCell ' just in case you'll decide to give-up on the "bad practice" of using ActiveCell
    RowNum = Cell.Row

    Set WS = ThisWorkbook.Sheets("Protocols")
    Set WS2 = ThisWorkbook.Sheets("Formulas")

    With WS
        PRL = .Range("B" & RowNum).Value

        Day_Num = InputBox("Please enter a day number to add to: " & PRL, "Add New Day")
        If Day_Num <> "" Then
            Set Row_Source = .Rows(RowNum)
            Row_Source.Copy

            Row_Source.Offset(1).Insert Shift:=xlDown
            Application.CutCopyMode = False

            .Range("D" & RowNum + 1).Value = Day_Num
        End If
    End With

    With WS2
        Set Row_Source = .Rows(RowNum)
        Row_Source.Copy

        Row_Source.Offset(1).Insert Shift:=xlDown
        Application.CutCopyMode = False
    End With

End Sub

【讨论】:

  • 谢谢你的代码,Shai。不幸的是,它不是很有效。我试图对其进行一些修改,以使操作按照它们需要发生的顺序发生,但我也无法得到它。它必须去:1)在公式上插入空白行 2)在协议上复制行 3)在协议上插​​入行 4)在公式上复制行 5)从 1 开始在空白行中粘贴行。我无法插入,因为它破坏了公式表.
  • 我已经用我刚才所做的更新更新了我的原始帖子。我不知道为什么我的格式在粘贴时不会保留。
  • 看起来真正的问题是我在我们复制和插入的协议行中有一个单元格,其中包含对公式的引用。 (例如 =Formulas!AB3)由于某种原因,它增加了 2 而不是 1,所以我希望看到 =Formulas!AB4 我看到 =Formulas!AB5,所以我没有得到正确的最终输出。 Formulas!AB# 上的所有内容看起来都很好,只是没有引用正确的单元格。你可以忽略我“修复”问题的尝试。
  • @Trevor 我住的地方是晚上,有时我们需要睡觉;)
  • 感谢您的所有帮助。如果没有您的宝贵意见,我不可能完成这项工作!
【解决方案2】:

这是执行此操作的代码。如果没有 Shai Rado,我不可能完成这一切,大部分功劳都应该归于那里。这完全符合规范:

Sub Button18_Click()

    Dim Row_Source As Range
    Dim WS As Worksheet, WS2 As Worksheet
    Dim Day_Num As String
    Dim Day_Dest As Range
    Dim PRL As String
    Dim RowNum As Long
    Dim Cell As Range

    Set Cell = ActiveCell ' just in case you'll decide to give-up on the "bad practice" of using ActiveCell
    RowNum = Cell.Row

    Set WS = ThisWorkbook.Sheets("Protocols")
    Set WS2 = ThisWorkbook.Sheets("Formulas")

    With WS
        PRL = .Range("B" & RowNum).Value

        Day_Num = InputBox("Please enter a day number to add to: " & PRL, "Add New Day")
        If Day_Num <> "" Then
            Set Row_Source = .Rows(RowNum)
        End If
    End With

    With WS2
        If Day_Num <> "" Then
            Set Row_Source = .Rows(RowNum)
            Row_Source.Offset(1).Insert Shift:=xlDown
            Application.CutCopyMode = False
        End If
    End With

    With WS
        If Day_Num <> "" Then
            Set Row_Source = .Rows(RowNum)
            Row_Source.Copy

            Row_Source.Offset(1).Insert Shift:=xlDown
            Application.CutCopyMode = False

            .Range("D" & RowNum + 1).Value = Day_Num
        End If
    End With

    With WS2
        Set Row_Source = .Rows(RowNum)
        Row_Source.Copy

        Row_Source.Offset(1).PasteSpecial

        Application.CutCopyMode = False
    End With

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多