Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VBA中以变量形式存储和Excel命名的范围,然后传递给函数_Excel_Vba - Fatal编程技术网

在VBA中以变量形式存储和Excel命名的范围,然后传递给函数

在VBA中以变量形式存储和Excel命名的范围,然后传递给函数,excel,vba,Excel,Vba,在VBA中,我试图将命名范围存储到变量中,然后将其传递给函数,但出现错误“对象不支持此属性或方法” referestorange是名称对象的属性,而不是范围 改变 ThisWorkbook.Sheets("BOM").Range("myTable").RefersToRange.Value 到 或 除了引用命名范围之外,代码中还有更多问题 你的代码,重构了吗 'Your are not returning anything, so use a Sub

在VBA中,我试图将命名范围存储到变量中,然后将其传递给函数,但出现错误“对象不支持此属性或方法”


referestorange
名称
对象的属性,而不是
范围

改变

ThisWorkbook.Sheets("BOM").Range("myTable").RefersToRange.Value


除了引用命名范围之外,代码中还有更多问题

你的代码,重构了吗

'Your are not returning anything, so use a Sub
'Define your parameter types
Sub BOM(PathRow As Long, rng As Range)
    Dim CR_Path As String
    Application.ScreenUpdating = False
    CR_Path = ThisWorkbook.Sheets("Request").Cells(PathRow, 1).Value 'Copy that path from a cell
    Set mybook = Workbooks.Open(CR_Path)
    
' Use the range variable Values property
    mybook.Sheets("BOM").Range("A4:G28").Value = rng.Value
    mybook.Close SaveChanges:=True
    Application.ScreenUpdating = True
End Sub

Sub test()
    Dim rng As Range
    Dim PathRow As Long ' use meaningful names for variables
    Set rng = ThisWorkbook.Names("myTable").RefersToRange
    PathRow = 2

    ' use the variable you defined
    BOM PathRow, rng
End Sub

尝试此操作后得到:选择范围时出现运行时错误1004“应用程序定义或对象定义错误”。问题出在函数内部```mybook.Sheets(“BOM”).Range(“A4:G28”).Value=ThisWorkbook.Sheets(“BOM”).Range(“rng”).Value这不是我建议的。这非常有效。是否也可以复制该范围内的所有内容,而不仅仅是值?
ThisWorkbook.Sheets("BOM").Range("myTable").Value
ThisWorkbook.Names("myTable").RefersToRange.Value
'Your are not returning anything, so use a Sub
'Define your parameter types
Sub BOM(PathRow As Long, rng As Range)
    Dim CR_Path As String
    Application.ScreenUpdating = False
    CR_Path = ThisWorkbook.Sheets("Request").Cells(PathRow, 1).Value 'Copy that path from a cell
    Set mybook = Workbooks.Open(CR_Path)
    
' Use the range variable Values property
    mybook.Sheets("BOM").Range("A4:G28").Value = rng.Value
    mybook.Close SaveChanges:=True
    Application.ScreenUpdating = True
End Sub

Sub test()
    Dim rng As Range
    Dim PathRow As Long ' use meaningful names for variables
    Set rng = ThisWorkbook.Names("myTable").RefersToRange
    PathRow = 2

    ' use the variable you defined
    BOM PathRow, rng
End Sub