【问题标题】:How can I set a value in a cell to be range for a formula in vba excel?如何将单元格中的值设置为 vba excel 中公式的范围?
【发布时间】:2022-12-03 13:05:29
【问题描述】:

我有代码提取最初给定值和范围的 cell.address, 但后来我希望它在新范围内寻找新值仅在 cell.address 行号中。 我在代码中添加了 cmets 以更好地解释它,但在单元格 j3 中,我得到了单元格地址,例如 $A$15,在单元格 k3 中,我可以使用左、右函数“15”获取行号,我想要第二个范围“设置单元格”函数根据这些单元格中的任何一个而改变,所以如果第一个输出是 $A$20,我希望第二个函数行更改为 20

Sub find()
Dim a As Double
Dim wks As Worksheet
Dim b As Double
Dim c As Integer
Dim cell As Range


       
         Set wks = Worksheets("comefri")
            a = wks.Range("c8").value
            b = wks.Range("D7").value
            c = wks.Range("k4").value
            
       
 With comefri
       
      
Set cell = Range("a:a").find(b, MatchCase:=Fasle, searchformat:=False)
         Range("j3").value = cell.Address
               
               
  ' I want the range row number to change depending on the value output form cell j3 or k3
 Set cell = Range("CX15:GS15").find(a, MatchCase:=Fasle, searchformat:=False)
               Range("K3").value = cell.Address
     

 
Range("k3").value = cell.Address


             
             
             
        End With
                

   
                

End Sub

Public Function ToColNum(ColN)
    ToColNum = Range(ColN & 1).Column
    End Function


Function GetValue(row As Integer, col As Integer)
    GetValue = ActiveSheet.Cells(row, col)
End Function

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    要在 VBA Excel 中将单元格的值设置为公式的范围,可以使用以下代码:

    Sub set_range()
    Dim cell As Range
    Dim value As String
    Dim formula As String
    
    Copy code
    ' Set the cell to be the cell at row 3, column 5
    Set cell = ActiveSheet.Cells(3, 5)
    
    ' Set the value to be the address of the cell (e.g. "E3")
    value = cell.Address
    
    ' Set the formula to be the SUM function using the cell as the range
    formula = "=SUM(" & value & ")"
    
    ' Set the formula of the cell to be the formula we defined
    cell.Formula = formula
    End Sub
    

    请注意,此代码以 SUM 函数为例,但您可以将其替换为任何使用范围的公式。

    【讨论】:

    • 谢谢,但这不完全是我需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多