简体   繁体   English

vba 中具有预定义范围的特定单元格中的范围总和

[英]Sum of range in a specific cell with predefined range in vba

I have a selected range that I defined as my range.我有一个选定的范围,我定义为我的范围。 I want to get the Sum of this selection in a specific cell.我想在特定单元格中获取此选择的总和。 The makro shall find "x", select the cell below and put in "Sum" + the range I defined in "myrange" makro 将在下面的单元格中找到“x”、select 并放入“Sum”+我在“myrange”中定义的范围

Sub more_twelve_months()
Dim myrange As Range

Set myrange = Range(Range("F5"), Range("F5").End(xlToRight))
       
Set more_twelve_months = Range("A1:ZZ10000").Find("x")
    more_twelve_months.Select
    FormularCell = ActiveCell.Offset(1, 0).Select
    Selection.Resize(Selection.Rows.Count, _
    Selection.Columns.Count).Select


   ActiveCell.Sum (myrange)

I tried several ways to get the sum, ActiveCell.Sum (myrange) is just the last thing I tried.我尝试了几种方法来获得总和, ActiveCell.Sum (myrange)只是我尝试的最后一件事。

Any ideas how I can solve this?有什么想法可以解决这个问题吗?

Is this what you want?这是你想要的吗?

You should avoid using Select as far as possible.您应该尽可能避免使用 Select

Sub more_twelve_months()

Dim myrange As Range
Dim more_twelve_months As Range 'declare your variables

Set myrange = Range(Range("F5"), Range("F5").End(xlToRight))
    
Set more_twelve_months = Range("A1:ZZ10000").Find("x")

If Not more_twelve_months Is Nothing Then 'check you've found something to avoid errors
    more_twelve_months.Offset(1).Value = Application.Sum(myrange)
End If

End Sub

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

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