将所有工作表中固定范围的值粘贴到一个工作表中

时间:2012-10-01 06:46:22

标签: excel excel-vba vba

我有大约30个工作表(每月的几天)和每张表

我想将每个工作表上的A293:AL293A296:AL296范围内的值复制到我的活动工作表(例如,从1月1日到30日的连续行中)。

这些范围分别使用MINPERCENTILE公式。但是,当我运行宏时,单元格包含!REF错误,因为公式被复制而不是实际的单元格值。

我应该如何修改宏,以便复制和粘贴值而不是公式?

我从另一个宏修改的代码,我用来将30张数据中的数据复制并粘贴到一张表中:

Sub test()
Dim curRow As Integer
Dim activeWorksheet As Worksheet
Set activeWorksheet = ActiveSheet
curRow = 1
For Each ws In ActiveWorkbook.Worksheets
    If Not ws.Name = activeWorksheet.Name Then
        ws.Range("A293:AL293").Copy Destination:=activeWorksheet.Range(CStr(curRow) & ":" & CStr(curRow + 288))
     curRow = curRow + 289   
    End If
Next ws
End Sub

我发现的另一个代码是:

Option Explicit
Sub CreateTempPSDReport()
Dim WS As Worksheet, Rept As Worksheet

Set Rept = Sheets("Temporary PSD Report")
Application.ScreenUpdating = False

'--> Loop through each worksheet except the report and
'--> Copy the set range to the report
For Each WS In ThisWorkbook.Worksheets
    If Not WS.Name = "Temporary PSD Report" Then
        WS.Range("A42", "I42").Rows.Copy
        Rept.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial _
        Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End If
Next
Application.ScreenUpdating = True
End Sub

但我不确定

是什么
        WS.Range("A42", "I42").Rows.Copy
        Rept.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial _
        Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

部分确实。

谢谢!

1 个答案:

答案 0 :(得分:1)

要将这对行复制到摘要表,您可以执行此操作。需要注意的两点

  • 使用.Value复制值而不是公式。在这种情况下,我使用了.Value2
  • 您无需复制和粘贴(除非您想复制格式),您只需将一个范围设置为等于另一个范围内的值

    Sub test()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lngCnt As Long
    
    Set ws1 = ActiveSheet
    Application.ScreenUpdating = False
    For Each ws2 In ActiveWorkbook.Worksheets
        If Not ws1.Name = ws2.Name Then
            ws1.[a1].Offset(lngCnt, 0).Resize(1, 38).Value2 = ws2.Range("A293:AL293").Value2
            ws1.[a1].Offset(lngCnt + 1, 0).Resize(1, 38).Value2 = ws2.Range("A296:AL296").Value2
            lngCnt = lngnct + 2
        End If
    Next ws2
    Application.ScreenUpdating = True
    End Sub