循环使用包含数据子集的工作表子集,应用公式 - 我能否比目前更有效地执行此操作?

时间:2016-09-29 09:38:55

标签: excel-vba loops foreach vba excel

社区大家好:)

这是我的第一篇文章,我是Excel-Vba编程的新手。我参加了一个入门课程,我正在尝试将我学到的技术应用到我生成vba代码的一些请求中。我基本上都是绿色的,所以如果我问任何傻话,请温柔地对待我!

我所拥有的是一个包含大量工作表的工作簿,这些工作表构建了depot的开销视图,然后是一些包含查找数据的工作表。

我只想循环遍历 depot 工作表,然后将引用工作表中的数据的公式应用到指定GL的行上的单元格,并跳过将它们应用于不符合GL的行。 / p>

最后,我想删除公式,然后将数据复制并粘贴为值。

我已经创建了以下一些代码来实现这一目标,但感觉非常缓慢而且笨重,并且在这里阅读了很多帖子,我确信有更好的方法可以做到这一点!

Public Sub Apply_Formulae_BO()

Application.ScreenUpdating = False - Toggle for testing and running final.
'Application.ScreenUpdating = True - Toggle for testing and running final.

 Dim BusFnctn As Variant 'Does this have to be a variant to make the code work?
 Dim BusFnctns As Variant 'Does this have to be a variant to make the code work?
 Dim GL As Range
 Dim GLs As Range


          BusFnctns = Array("101 Scotland Region", _
                            "102 South Region", _
                            "103 Yorkshire Region", _
                            "104 Central South Region", _
                            "120 Central", _
                            "121 Incentives", _
                            "122 Trainees", _
                            "130 Fruit", _
                            "131 Glasshouse", _
                            "132 Plantsystems", _
                            "133 Vegetable", _
                            "134 SWS", _
                            "135 Ebbage")


    For Each BusFnctn In BusFnctns 'Cycles through the arrayed tabs in the workbook

            With Worksheets(BusFnctn)

                Sheets(BusFnctn).Activate

                Set GLs = Range("J9:J112") '"Set" needs to be used when setting a variable "as RANGE"

                    For Each GL In GLs.Cells

                       'Debug.Print BusFnctn 'Tests what tab we're in in the sheet array.
                       'Debug.Print GL 'Tests what GL is held in the variable

                        If GL = "" Then 'If GL in the set range equals nothing

                        GL = "" 'Skip it - Is this the best way to skip a cell?

                    'Otherwise apply the following formulae into cells offset from the GL in the current variable

                        Else: GL.Offset(0, -9) = "=SUMIFS('OH Data 2015'!C13,'OH Data 2015'!C5,RC10,'OH Data 2015'!C9,R4C5)"
                              GL.Offset(0, -7) = "=SUMIFS('OH Data 2016'!C13,'OH Data 2016'!C5,RC10,'OH Data 2016'!C9,R4C5)"
                              GL.Offset(0, -6) = "=SUMIFS('Annual Budget'!C8,'Annual Budget'!C6,R4C5,'Annual Budget'!C2,RC10)-SUMIFS('OH Data 2016'!C14,'OH Data 2016'!C9,R4C5,'OH Data 2016'!C5,RC10)"
                              GL.Offset(0, -5) = "=SUM(RC[-2]:RC[-1])"
                              GL.Offset(0, -3) = "=SUMIFS('Annual Budget'!C8,'Annual Budget'!C6,R4C5,'Annual Budget'!C2,RC[3])"

                                'Can the above formulae be applied to ranges of cells at once on multiple sheets rather than cell by cell?

                        End If

                    Next GL 'Go to next cell in the "GLs" variable range

            End With

    Next BusFnctn

End Sub

我在编码中添加了一些注释,以提醒自己我的问题......

这是它的关键所在:

有没有办法可以将这些公式应用到所有工作表上的正确单元格中,而无需一次一个单元格遍历数组中的每个工作表?这种方式非常缓慢且非常低效。

我还想抑制(隐藏)行中所有公式结果总计为零的任何行。这更好地写成单独的程序还是作为这个程序的延伸?

我还没有写过将公式结果写成值的代码 - 但是不要担心!

任何人都可以给予任何指导,我们将非常感激和感激。

干杯 - CJP

0 个答案:

没有答案