简体   繁体   English

Excel VBA:将公式分配给同一工作表中的倍数动态范围表

[英]Excel VBA : assign formula to multiples dynamic range table in same sheet

I am new and learning Excel VBA. 我是新手,正在学习Excel VBA。 I am now having this problem 我现在有这个问题

  1. There is more than 10 tables in a worksheet (number of tables is not consistent) 工作表中有10个以上的表(表的数量不一致)
  2. The number of columns are consistent but not the rows in each tables 列数是一致的,但每个表中的行是不一致的
  3. I would like to apply a total row to the end of every table 我想将总行应用于每个表的末尾
  4. After that, I will apply the same formula to every table and put the results on the right side of each table 之后,我将相同的公式应用于每个表并将结果放在每个表的右侧

This could be easy but the core problem is that the range is unknown. 这可能很容易,但是核心问题是范围未知。 - As it is not an actual table in Excel, so I tried to first define the range of the data by creating table for it, then again, I don't have idea on how to create the table without knowing the range. -由于它不是Excel中的实际表,因此我尝试通过为其创建表来首先定义数据范围,然后再次,我不知道如何在不知道范围的情况下创建表。

Below is something I came up with (which is not very "dynamic") 以下是我想到的内容(不是很“动态”)

Sub plsWork()

Set u = ThisWorkbook.Worksheets("Sheet2")
Set f = u.Range("A").Find(what:="Name", lookat:=xlPart)
a = f.Address

Set sht = u.Range(a)

'trying to insert this at the end of the table
Total = Sum(u.Offset(2, 1) + u.Offset(3, 1) + u.Offset(4, 1))

If Cells(i, 2) = vbNullString Then 'this is already not applicable as the top 2 row in colB has null string
u.Offset(i, 1).Value = Total

'putting the table name at F2
u.Offset(-2, 5).Value = u.Offset(-3, 0).Value
u.Offset(-2, 6).Value = Total

u.Offset(-1, 5).Value = u.Offset(2, 0).Value
u.Offset(-1, 6).Value = Sum(u.Offset(2, 1) + u.Offset(2, 2) + u.Offset(2, 3))

u.Offset(0, 5).Value = u.Offset(3, 0).Value
u.Offset(0, 6).Value = Sum(u.Offset(3, 1) + u.Offset(3, 2) + u.Offset(3, 3))

u.Offset(1, 5).Value = u.Offset(4, 0).Value
u.Offset(1, 6).Value = Sum(u.Offset(4, 1) + u.Offset(4, 2) + u.Offset(4, 3))

End Sub

Oh, and when I run above code, I got error "Sub or Function not defined" on "SUM" 哦,当我运行上述代码时,在“ SUM”上收到错误“ Sub或Function not defined”

Here is the image of the tables in a sheet 这是工作表中表格的图像
yellow highlighted is what going to be there after executing the sub. 黄色突出显示是执行子程序后将要出现的内容。

It was quite easy applying formula in Excel sheet and copy paste the formula to each tables, 在Excel工作表中应用公式并将公式复制粘贴到每个表中非常容易,
but it was tedious, so I try to come out with a vba code to help so that the macro could run based on schedule. 但是这很乏味,所以我尝试提供一个vba代码来帮助您,以便宏可以根据计划运行。

I'm scratching my head and searching to and fro for the past two days, I still haven't got a clue on how to code this. 在过去的两天里,我一直抓着头来回搜寻,但我仍然对如何编写此代码一无所知。
So can any expert tell me if this is possible? 那么,有没有专家可以告诉我这是否可行? like without knowing the range? 喜欢不知道范围?
If so, could you guys shed me with some info on how to achieve this? 如果是这样,你们能给我一些有关如何实现这一目标的信息吗?
Thank you. 谢谢。 I really want to know if this can be done or not. 我真的很想知道这是否可以完成。

Here is an image of my attempt using provided answer 这是我尝试使用提供的答案的图片

You may try something like this... 您可以尝试这样的事情...

The code below will insert a Total Row for each table which has more than one row and four columns in it. 下面的代码将为每个表插入一个总行,该表中有多行和四列。

Sub InsertTotalInEachTable()
Dim ws As Worksheet
Dim rng As Range
Dim i As Integer, r As Long, j As Long

Application.ScreenUpdating = False

Set ws = ActiveSheet

For Each rng In ws.UsedRange.SpecialCells(xlCellTypeConstants, 3).Areas
    If rng.Rows.Count > 1 And rng.Columns.Count = 4 Then
        j = 2
        r = rng.Cells(rng.Rows.Count, 1).Row + 1
        Cells(r, rng.Columns(1).Column).Value = "Total"
        For i = rng.Columns(2).Column To rng.Columns(2).Column + 2
            Cells(r, i).Formula = "=SUM(" & rng.Columns(j).Address & ")"
            j = j + 1
        Next i
    End If
Next rng
Application.ScreenUpdating = True
End Sub

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

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