如何减少/压缩此重复的VBA代码?

时间:2019-07-06 18:49:28

标签: excel vba

我正在尝试学习如何减少代码的重复性。我以为我可以暂时摆脱它,以后再学习,但是我收到了“过程太大”错误。我知道我可以划分为多个单独的子程序,然后编写一个调用每个子程序的子程序,但是我宁愿解决问题的根源,我知道这是不必要的重复。

我了解循环的基本知识,但不了解如何将其应用于代码。这可能是因为我不了解高级逻辑,或者是因为我很难找到代码中的模式以帮助我弄清楚如何编写循环。

这是非常长的代码中的两个示例。

示例1:使用工作簿(数据文件17)中的COUNTIFS将值填充到另一个工作簿/工作表(Raw17)中的单元格中。

Dim datafile17 As Workbook
Set datafile17 = Workbooks("WorkbookName")

'First Set
 Dim EU3 As Range, EU4 As Range, EU5 As Range, EU6 As Range, EU7 As Range, EU8 As Range
    Set EU3 = Raw17.Range("F2")
    Set EU4 = Raw17.Range("F3")
    Set EU5 = Raw17.Range("F4")
    Set EU6 = Raw17.Range("F5")
    Set EU7 = Raw17.Range("F6")
    Set EU8 = Raw17.Range("F7")

    datafile17.Activate
       EU3 = Application.WorksheetFunction.CountIfs(Range("X:X"), "U", Range("C:C"), school, Range("J:J"), 3)
       EU4 = Application.WorksheetFunction.CountIfs(Range("X:X"), "U", Range("C:C"), school, Range("J:J"), 4)
       EU5 = Application.WorksheetFunction.CountIfs(Range("X:X"), "U", Range("C:C"), school, Range("J:J"), 5)
       EU6 = Application.WorksheetFunction.CountIfs(Range("X:X"), "U", Range("C:C"), school, Range("J:J"), 6)
       EU7 = Application.WorksheetFunction.CountIfs(Range("X:X"), "U", Range("C:C"), school, Range("J:J"), 7)
       EU8 = Application.WorksheetFunction.CountIfs(Range("X:X"), "U", Range("C:C"), school, Range("J:J"), 8)

'Second Set
 Dim EAB3 As Range, EAB4 As Range, EAB5 As Range, EAB6 As Range, EAB7 As Range, EAB8 As Range
    Set EAB3 = Raw17.Range("E2")
    Set EAB4 = Raw17.Range("E3")
    Set EAB5 = Raw17.Range("E4")
    Set EAB6 = Raw17.Range("E5")
    Set EAB7 = Raw17.Range("E6")
    Set EAB8 = Raw17.Range("E7")

     datafile17.Activate
       EAB3 = Application.WorksheetFunction.CountIfs(Range("X:X"), "AB", Range("C:C"), school, Range("J:J"), 3)
       EAB4 = Application.WorksheetFunction.CountIfs(Range("X:X"), "AB", Range("C:C"), school, Range("J:J"), 4)
       EAB5 = Application.WorksheetFunction.CountIfs(Range("X:X"), "AB", Range("C:C"), school, Range("J:J"), 5)
       EAB6 = Application.WorksheetFunction.CountIfs(Range("X:X"), "AB", Range("C:C"), school, Range("J:J"), 6)
       EAB7 = Application.WorksheetFunction.CountIfs(Range("X:X"), "AB", Range("C:C"), school, Range("J:J"), 7)
       EAB8 = Application.WorksheetFunction.CountIfs(Range("X:X"), "AB", Range("C:C"), school, Range("J:J"), 8)

'I have 18 more of these sets. Then I repeat the whole thing twice more with datafile18/Raw18 and datafile19/Raw19 respectively. 

示例2 :每个块在另一个工作簿(data17)上的选项卡之间循环,将值粘贴到不同工作簿/工作表(概述)中的某个单元格上。

data17.Activate
'For Column D(4)
Overview.Range("B3") = Application.VLookup(fullschcode, G3.Range("A1:AA150"), 4, False)
Overview.Range("B7") = Application.VLookup(fullschcode, G4.Range("A1:AA150"), 4, False)
Overview.Range("B11") = Application.VLookup(fullschcode, G5.Range("A1:AA150"), 4, False)
Overview.Range("B15") = Application.VLookup(fullschcode, G6.Range("A1:AA150"), 4, False)
Overview.Range("B19") = Application.VLookup(fullschcode, G7.Range("A1:AA150"), 4, False)
Overview.Range("B23") = Application.VLookup(fullschcode, G8.Range("A1:AA150"), 4, False)


'For column J (10)
Overview.Range("E3") = Application.VLookup(fullschcode, G3.Range("A1:AA150"), 10, False)
Overview.Range("E7") = Application.VLookup(fullschcode, G4.Range("A1:AA150"), 10, False)
Overview.Range("E11") = Application.VLookup(fullschcode, G5.Range("A1:AA150"), 10, False)
Overview.Range("E15") = Application.VLookup(fullschcode, G6.Range("A1:AA150"), 10, False)
Overview.Range("E19") = Application.VLookup(fullschcode, G7.Range("A1:AA150"), 10, False)
Overview.Range("E23") = Application.VLookup(fullschcode, G8.Range("A1:AA150"), 10, False)

'This one has way less repetition than my first example, but I still have 8 total sets of this type.

我找到了其他一些答案,包括this helpful post的最佳答案,但是我仍然坚持如何将其应用于自己的情况。再说一次,可能是因为我不了解循环的高级逻辑,但是我正在尝试。

其他说明:

  1. “ fullschcode”和“ school”是我之前在中分配的变量 使用输入框输入代码,具体取决于我需要数据的学校。
  2. 我通过论坛跟踪注意到,许多人可能认为使用COUNTIFS和VLOOKUP是完成某些任务的糟糕方法 任务。虽然我对改善这些功能的建议持开放态度, 我目前主要关心的是重复。我选择了COUNTIFS, VLOOKUP函数,因为我了解它们。以前有人 建议我通过Access运行所有这些数据,并且 我很欣赏这个建议并想学习,但我必须权衡一下 当我需要可用数据时的学习曲线。 我的部门以前是用印刷品手工做的 枢纽分析表并手动输入数字。 尽力使这些过程更有效。

1 个答案:

答案 0 :(得分:3)

这是第一套游戏的一种方式:

给出以下声明:

new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
   Intent startNewActivity = new Intent(CurrentActivity.this, MainActivity.class);
   startActivity(startNewActivity);

   }
}, 5000);

您可以将第一个设置简化为循环。

Dim datafile17 As Workbook, datafile17WS As Worksheet
Dim myWB As Workbook, myWS As Worksheet
Dim I As Long

Set datafile17 = Workbooks("workbookName")
Set datafile17WS = datafile17.Worksheets("worksheetname")
Set myWB = ThisWorkbook
Set myWS = myWB.Worksheets("Raw17")

请注意,正如多次提到的那样,不需要'First Set With datafile17WS For I = 2 To 7 myWS.Cells(I, 6) = Application.WorksheetFunction.CountIfs(.Range("X:X"), "U", .Range("C:C"), school, .Range("J:J"), I + 1) Next I End With 方法。