简体   繁体   English

如何复制动态命名范围内的值?

[英]How to copy the values within a dynamic named range?

I've doing a waste calculation exercise at work, and I've got several dynamic named ranges whose cells all contain values arrived at by formulae. 我正在工作中进行浪费计算,并且有几个动态命名范围,它们的单元格都包含通过公式得出的值。 I need a module that will copy the values within a dynamic named range onto another sheet without copying the formulae themselves, just the values that the cells contain. 我需要一个模块,该模块将动态命名范围内的值复制到另一张纸上,而无需复制公式本身,而只是复制单元格包含的值。 I tried to use this: 我试图用这个:

Sub Sample()
    Dim wsI As Worksheet, wsO As Worksheet

    Set wsI = ThisWorkbook.Sheets("Sheet1")
    Set wsO = ThisWorkbook.Sheets("Sheet1")

    wsI.Range("Pct_waste").Copy wsO.Range("B4")

End Sub

But this copies the formulae, which is no use. 但这会复制公式,没有用。 And I can't just use absolute references because I need to be able to quickly add new data. 而且我不能只使用绝对引用,因为我需要能够快速添加新数据。 Ultimately, I intend to create a macro that will copy the values within the dynamic named ranges to a new sheet, then sort these values numerically and plot them on a scatter chart. 最终,我打算创建一个宏,该宏将动态命名范围内的值复制到新的工作表中,然后对这些值进行数字排序并将其绘制在散点图上。 So I need to find a way to copy all the values in my dynamic named ranges without copying the formulae themselves. 因此,我需要找到一种方法来复制动态命名范围中的所有值,而无需复制公式本身。

Also, I'm fairly novice when it comes to VBA so go easy! 另外,关于VBA,我还是新手,所以轻松一点!

Use the .Resize() method: 使用.Resize()方法:

Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet1")

With wsI.Range("Pct_waste")
    wsO.Range("B4").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With

Try pasting the value only: 尝试仅粘贴值:

Sub Sample() 

Dim wsI As Worksheet, wsO As Worksheet

Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet1")

wsI.Range("Pct_waste").Copy 
wsO.Range("B4").PasteSpecial xlPasteValues

End Sub

or something like this 或类似的东西

Sub test()

Dim r As Excel.Range
Dim i As Integer

Set r = Range("test_range")

For i = 1 To r.Cells.Count
    Range("x" & i).Value = r.Cells(i, 1).Value
Next i

End Sub

or you could do something like this as static formula in the sheet, =index(range_name,rows($A$1:$A1),1) and fill down, also trap the errors using IFERRROR, this will be static though. 或者您可以像这样在工作表中执行以下静态公式,例如= index(range_name,rows($ A $ 1:$ A1),1)并填写下来,还可以使用IFERRROR捕获错误,但这将是静态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM