Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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
Excel 如何在运行时使用字符串指定属性或方法。错误:公共成员';临时工';关于类型';范围';找不到_Excel_Vba - Fatal编程技术网

Excel 如何在运行时使用字符串指定属性或方法。错误:公共成员';临时工';关于类型';范围';找不到

Excel 如何在运行时使用字符串指定属性或方法。错误:公共成员';临时工';关于类型';范围';找不到,excel,vba,Excel,Vba,如何在运行时使用字符串指定属性或方法 我试图通过将excel属性动态分配给字符串来执行以下代码 原创的 Range("A1:A20").NumberFormat = @ 我的编辑版本 Dim Temp As String = "NumberFormat" Range("A1:A20").Temp = @ 这让我犯了一个错误 找不到类型“Range”上的公共成员“Temp” 现在我如何解决这个错误,我想使用相同的代码,动态地将多个属性(NumberFormat、WrapText、Font.S

如何在运行时使用字符串指定属性或方法

我试图通过将excel属性动态分配给字符串来执行以下代码

原创的

Range("A1:A20").NumberFormat = @
我的编辑版本

Dim Temp As String = "NumberFormat"

Range("A1:A20").Temp = @
这让我犯了一个错误

找不到类型“Range”上的公共成员“Temp”


现在我如何解决这个错误,我想使用相同的代码,动态地将多个属性(NumberFormat、WrapText、Font.Size等)分配给Temp

,下面是@BigBen的评论:

Sub tester()

    ApplyValue Selection.Cells(1), "NumberFormat", "@"
    ApplyValue Selection.Cells(1), "HorizontalAlignment", xlRight
    ApplyValue Selection.Cells(1), "Font.Bold", True
    ApplyValue Selection.Cells(1), "Font.Color", vbYellow
    ApplyValue Selection.Cells(1), "Interior.Color", vbRed

End Sub

'Set a named property of an object to a provided value
'Object to be affected may be at the end of a period-delimited "chain"
'  of "Object1.ChildObject" etc
Sub ApplyValue(obj As Object, propString, propVal)
    Dim o As Object, arr, x As Long
    arr = Split(propString, ".")
    Set o = obj
    For x = LBound(arr) To UBound(arr)
        If x <> UBound(arr) Then
            'not there yet - get the next object in the chain
            Set o = CallByName(o, arr(x), VbGet)
        Else
            'at the end of the chain, so set the property
            CallByName o, arr(x), VbLet, propVal
        End If
    Next x
End Sub
子测试仪()
应用值选择。单元格(1),“NumberFormat”,“@”
应用值选择。单元格(1),“水平对齐”,xlRight
ApplyValue Selection.Cells(1),“Font.Bold”,True
应用值选择。单元格(1),“字体.颜色”,vbYellow
ApplyValue Selection.Cells(1),“Interior.Color”,vbRed
端接头
'将对象的命名属性设置为提供的值
'受影响的对象可能位于以句点分隔的“链”结尾处'
“Object1.ChildObject”等的
子应用程序值(对象作为对象、propString、propVal)
尺寸o为对象,arr为,x为长度
arr=拆分(propString,“.”)
设置o=obj
对于x=LBound(arr)到UBound(arr)
如果x UBound(arr),则
'还没有-获取链中的下一个对象
设置o=CallByName(o,arr(x),VbGet)
其他的
'在链的末尾,设置属性
CallByName o,arr(x),VbLet,propVal
如果结束
下一个x
端接头

以下是@BigBen的评论:

Sub tester()

    ApplyValue Selection.Cells(1), "NumberFormat", "@"
    ApplyValue Selection.Cells(1), "HorizontalAlignment", xlRight
    ApplyValue Selection.Cells(1), "Font.Bold", True
    ApplyValue Selection.Cells(1), "Font.Color", vbYellow
    ApplyValue Selection.Cells(1), "Interior.Color", vbRed

End Sub

'Set a named property of an object to a provided value
'Object to be affected may be at the end of a period-delimited "chain"
'  of "Object1.ChildObject" etc
Sub ApplyValue(obj As Object, propString, propVal)
    Dim o As Object, arr, x As Long
    arr = Split(propString, ".")
    Set o = obj
    For x = LBound(arr) To UBound(arr)
        If x <> UBound(arr) Then
            'not there yet - get the next object in the chain
            Set o = CallByName(o, arr(x), VbGet)
        Else
            'at the end of the chain, so set the property
            CallByName o, arr(x), VbLet, propVal
        End If
    Next x
End Sub
子测试仪()
应用值选择。单元格(1),“NumberFormat”,“@”
应用值选择。单元格(1),“水平对齐”,xlRight
ApplyValue Selection.Cells(1),“Font.Bold”,True
应用值选择。单元格(1),“字体.颜色”,vbYellow
ApplyValue Selection.Cells(1),“Interior.Color”,vbRed
端接头
'将对象的命名属性设置为提供的值
'受影响的对象可能位于以句点分隔的“链”结尾处'
“Object1.ChildObject”等的
子应用程序值(对象作为对象、propString、propVal)
尺寸o为对象,arr为,x为长度
arr=拆分(propString,“.”)
设置o=obj
对于x=LBound(arr)到UBound(arr)
如果x UBound(arr),则
'还没有-获取链中的下一个对象
设置o=CallByName(o,arr(x),VbGet)
其他的
'在链的末尾,设置属性
CallByName o,arr(x),VbLet,propVal
如果结束
下一个x
端接头

我不认为仅仅将
NumberFormat
属性名替换为
Temp
有什么意义,即使你可以。您只是在创建一个中间人操作符。。。。你能再详细谈谈你的问题吗。我怀疑这是一个XY问题,当标签的描述显示该标签正在等待删除时。不要将此标签用于新问题,这意味着您不应使用此标签。添加标签之前,请阅读标签说明,以确保它们最适合您的问题。
Dim Temp As String=“NumberFormat”
是VB.net,而不是VBA。。。。但是您可以在VBA中使用:
CallByName范围(“A1:A20”)、Temp、VbLet、@“
。即使可以,我也不认为仅仅为
Temp
交换
NumberFormat
属性名有什么意义。您只是在创建一个中间人操作符。。。。你能再详细谈谈你的问题吗。我怀疑这是一个XY问题,当标签的描述显示该标签正在等待删除时。不要将此标签用于新问题,这意味着您不应使用此标签。添加标签之前,请阅读标签说明,以确保它们最适合您的问题。
Dim Temp As String=“NumberFormat”
是VB.net,而不是VBA。。。。但是您可以在VBA中使用:
CallByName范围(“A1:A20”)、Temp、VbLet、@“