想要在宏中添加代码以将一行的公式粘贴到同一工作表上范围的底部。

huangapple go评论52阅读模式
英文:

VBA / Excel help: Want to add to a macro to paste formula from a row to the bottom of a range on the same sheet

问题

我有一个包含数据范围在B:C的Excel表格,从B30开始,延伸到一定数量的行。

在E1:O1中有一个公式。

我希望有一个可以点击的宏,它将复制E1:O1中的公式,并粘贴到B:C数据范围旁边(保留列D为空白)。

我一直在编辑来自这里的一些代码,它在大多数情况下都有效,但是当它从表的顶部复制公式时,它只将其粘贴到数据的顶行,而不是每一行。

以下是我编辑过的代码:

'move data data and populate formulas

'for New
Sub Copy()
    Application.ScreenUpdating = False

    Dim s1 As Excel.Worksheet
    Dim s2 As Excel.Worksheet
    Dim s3 As Excel.Worksheet
    Dim s4 As Excel.Worksheet
    
    Dim iLastCellS2 As Excel.Range
    Dim iLastCellS3 As Excel.Range
    Dim iLastCellS4 As Excel.Range
    
    Dim idataRange1 As Excel.Range
    Dim idataRange2 As Excel.Range
    Dim idataRange3 As Excel.Range
    
    Dim iLastRowS1 As Long

    Set s1 = Sheets("SET UP")
    Set s2 = Sheets("New")
    Set s3 = Sheets("Current")
    Set s4 = Sheets("Proposed")

    '获取SET UP中列C的最后一行行号
    iLastRowS1 = s1.Cells(s1.Rows.Count, "C").End(xlUp).Row

    '获取要粘贴数据的New的最后一个可用单元格
    Set iLastCellS2 = s2.Cells(s2.Rows.Count, "B").End(xlUp).Offset(1, 0)
    
    '获取要粘贴数据的Current的最后一个可用单元格
    Set iLastCellS3 = s3.Cells(s3.Rows.Count, "B").End(xlUp).Offset(1, 0)
    
    '获取要粘贴数据的Proposed的最后一个可用单元格
    Set iLastCellS4 = s4.Cells(s4.Rows.Count, "B").End(xlUp).Offset(2, 0)
    
    '获取要粘贴公式的New的最后一个可用单元格
    Set idataRange1 = s2.Cells(s2.Rows.Count, "B").End(xlUp).Offset(1, 3)
    
    '获取要粘贴公式的Current的最后一个可用单元格
    Set idataRange2 = s3.Cells(s3.Rows.Count, "B").End(xlUp).Offset(1, 3)
    
    '获取要粘贴公式的Proposed的最后一个可用单元格
    Set idataRange3 = s4.Cells(s4.Rows.Count, "B").End(xlUp).Offset(2, 3)

    '复制并粘贴到New
    s1.Range("C17", s1.Cells(iLastRowS1, "D")).Copy iLastCellS2
    
    '复制并粘贴New的公式
    s2.Range("E1:O1").Copy idataRange1
    
    '复制并粘贴到Current
    s1.Range("C17", s1.Cells(iLastRowS1, "D")).Copy iLastCellS3
        
    '复制并粘贴Current的公式
    s3.Range("E1:O1").Copy idataRange2
        
    '复制并粘贴到Proposed
    s1.Range("C17", s1.Cells(iLastRowS1, "D")).Copy iLastCellS4
        
    '复制并粘贴Proposed的公式
    s4.Range("E1:AU1").Copy idataRange3

    Application.ScreenUpdating = True
End Sub

我认为问题可能出在以下部分:s2.Range("E1:O1").Copy idataRange1s3.Range("E1:O1").Copy idataRange2,和s4.Range("E1:AU1").Copy idataRange3,这些代码只粘贴了公式到一个单元格。您可能需要修改这些行,以确保公式被粘贴到整个范围,而不仅仅是一个单元格。

英文:

I have an excel sheet with a range of data in B:C, starting at B30 that extends to a dynamic number of rows.

There is a forumla in E1:O1.

I wish to have a macro I can click that will copy the formula from E1:O1 and past it in the range next to the data in B:C (leaving colum d blank).

I have been editing some code from here and it works for the most part, however when it copys the formula from the top of the sheets it only pastes it in the top row of the data, not for every row.

Here is my edited code:

'move data data and populate formulas

'for New
Sub Copy()
    Application.ScreenUpdating = False

    Dim s1 As Excel.Worksheet
    Dim s2 As Excel.Worksheet
    Dim s3 As Excel.Worksheet
    Dim s4 As Excel.Worksheet
    
    Dim iLastCellS2 As Excel.Range
    Dim iLastCellS3 As Excel.Range
    Dim iLastCellS4 As Excel.Range
    
    Dim idataRange1 As Excel.Range
    Dim idataRange2 As Excel.Range
    Dim idataRange3 As Excel.Range
    
    Dim iLastRowS1 As Long

    Set s1 = Sheets("SET UP")
    Set s2 = Sheets("New")
    Set s3 = Sheets("Current")
    Set s4 = Sheets("Proposed")

    ' get last row number of C in SET UP
    iLastRowS1 = s1.Cells(s1.Rows.Count, "C").End(xlUp).Row

    ' get last AVAILABLE cell to past into new for data data
    Set iLastCellS2 = s2.Cells(s2.Rows.Count, "B").End(xlUp).Offset(1, 0)
    
    ' get last AVAILABLE cell to past into current for data data
    Set iLastCellS3 = s3.Cells(s3.Rows.Count, "B").End(xlUp).Offset(1, 0)
    
    ' get last AVAILABLE cell to past into proposed for data data
    Set iLastCellS4 = s4.Cells(s4.Rows.Count, "B").End(xlUp).Offset(2, 0)
    
    ' get last AVAILABLE cell to past into new for formula
    Set idataRange1 = s2.Cells(s2.Rows.Count, "B").End(xlUp).Offset(1, 3)
    
    ' get last AVAILABLE cell to past into current for formula
    Set idataRange2 = s3.Cells(s3.Rows.Count, "B").End(xlUp).Offset(1, 3)
    
    ' get last AVAILABLE cell to past into proposed for formula
    Set idataRange3 = s4.Cells(s4.Rows.Count, "B").End(xlUp).Offset(2, 3)

    'copy&paste into New
    s1.Range("C17", s1.Cells(iLastRowS1, "D")).Copy iLastCellS2
    
    'copy&paste formulas for new
    s2.Range("E1:O1").Copy idataRange1
    
    'copy&paste into Current
    s1.Range("C17", s1.Cells(iLastRowS1, "D")).Copy iLastCellS3
        
    'copy&paste formulas for Current
    s3.Range("E1:O1").Copy idataRange2
        
    'copy&paste into Proposed
    s1.Range("C17", s1.Cells(iLastRowS1, "D")).Copy iLastCellS4
        
    'copy&paste formulas for proposed
    s4.Range("E1:AU1").Copy idataRange3
    

    Application.ScreenUpdating = True
End Sub

I expect Im doing lots of this wrong - i am not experianced with Macros.

each section with the "'copy&paste formulas for " is only pasting into the top row, and not to the bottom of the range.

答案1

得分: 1

复制数据和公式

Sub CopyDataAndFormulas()

    Const SRC_NAME As String = "SET UP"
    Const SRC_FIRST_CELL As String = "C17"
    Const SRC_COPY_COLUMNS As String = "C:D"
    Const DST_COLUMN As String = "B"

    Dim dwsNames(): dwsNames = VBA.Array("New", "Current", "Proposed")
    Dim dfAddresses(): dfAddresses = VBA.Array("E1:O1", "E1:O1", "E1:AU1")

    Dim wb As Workbook: Set wb = ThisWorkbook '包含此代码的工作簿

    Dim sws As Worksheet: Set sws = wb.Sheets(SRC_NAME)

    Dim srg As Range, srCount As Long, scCount As Long

    With sws.Range(SRC_FIRST_CELL)
        srCount = sws.Cells(sws.Rows.Count, .Column).End(xlUp).Row - .Row + 1
        If srCount < 1 Then
            MsgBox "源工作表中没有数据。", vbCritical
            Exit Sub
        End If
        Set srg = .Resize(srCount).EntireRow.Columns(SRC_COPY_COLUMNS)
        scCount = srg.Columns.Count
    End With

    Application.ScreenUpdating = False

    Dim dws As Worksheet, drg As Range, dfCell As Range, n As Long

    For n = 0 To UBound(dwsNames)
        Set dws = wb.Sheets(dwsNames(n))
        Set dfCell = dws.Cells(dws.Rows.Count, DST_COLUMN).End(xlUp).Offset(1)
        Set drg = dfCell.Resize(srCount, scCount)
        srg.Copy drg
        ' 或者只复制数值:
        ' drg.Value = srg.Value
        With dws.Range(dfAddresses(n))
            .Copy Intersect(drg.EntireRow, .EntireColumn)
        End With
    Next n

    Application.ScreenUpdating = True

    MsgBox "数据和公式已复制。", vbInformation

End Sub

想要在宏中添加代码以将一行的公式粘贴到同一工作表上范围的底部。

英文:

Copy Data and Formulas

想要在宏中添加代码以将一行的公式粘贴到同一工作表上范围的底部。

<!-- language: lang-vb -->

Sub CopyDataAndFormulas()
    
    Const SRC_NAME As String = &quot;SET UP&quot;
    Const SRC_FIRST_CELL As String = &quot;C17&quot;
    Const SRC_COPY_COLUMNS As String = &quot;C:D&quot;
    Const DST_COLUMN As String = &quot;B&quot;
 
    Dim dwsNames(): dwsNames = VBA.Array(&quot;New&quot;, &quot;Current&quot;, &quot;Proposed&quot;)
    Dim dfAddresses(): dfAddresses = VBA.Array(&quot;E1:O1&quot;, &quot;E1:O1&quot;, &quot;E1:AU1&quot;)
    
    Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code

    Dim sws As Worksheet: Set sws = wb.Sheets(SRC_NAME)
    
    Dim srg As Range, srCount As Long, scCount As Long
    
    With sws.Range(SRC_FIRST_CELL)
        srCount = sws.Cells(sws.Rows.Count, .Column).End(xlUp).Row - .Row + 1
        If srCount &lt; 1 Then
            MsgBox &quot;No data in source worksheet.&quot;, vbCritical
            Exit Sub
        End If
        Set srg = .Resize(srCount).EntireRow.Columns(SRC_COPY_COLUMNS)
        scCount = srg.Columns.Count
    End With
    
    Application.ScreenUpdating = False

    Dim dws As Worksheet, drg As Range, dfCell As Range, n As Long
    
    For n = 0 To UBound(dwsNames)
        Set dws = wb.Sheets(dwsNames(n))
        Set dfCell = dws.Cells(dws.Rows.Count, DST_COLUMN).End(xlUp).Offset(1)
        Set drg = dfCell.Resize(srCount, scCount)
        srg.Copy drg
        &#39; or to copy only values:
        &#39;drg.Value = srg.Value
        With dws.Range(dfAddresses(n))
            .Copy Intersect(drg.EntireRow, .EntireColumn)
        End With
    Next n
    
    Application.ScreenUpdating = True
    
    MsgBox &quot;Data and formulas copied.&quot;, vbInformation

End Sub

答案2

得分: -1

s1.Range("E:O").Copy s2.Range("E:O")

英文:

Assuming you want to copy from s1 to s2:

s1.Range(&quot;E:O&quot;).Copy s2.Range(&quot;E:O&quot;)

huangapple
  • 本文由 发表于 2023年7月10日 23:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定