英文:
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 idataRange1
,s3.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 = "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 ' 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 < 1 Then
MsgBox "No data in source worksheet.", 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
' or to copy only values:
'drg.Value = srg.Value
With dws.Range(dfAddresses(n))
.Copy Intersect(drg.EntireRow, .EntireColumn)
End With
Next n
Application.ScreenUpdating = True
MsgBox "Data and formulas copied.", 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("E:O").Copy s2.Range("E:O")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论