缩短 case select [公式填充] 代码,因为过程太大

Shorten case select [formula populating] code because procedure is too large

我制作了一个 sub,由于我的 select case,它变得太大了,它是从 1 到 16。

对于select case 1

对于select case 2

对于select case 3

等等

我可以通过以不同的方式编写公式来缩短我的代码,还是可以将我的 select case 的一部分放在不同的 sub 中? (完整代码(案例 1-3)粘贴在下面以防我不够清楚)

公式的:

Set rnga = Range("a2")
Set rngh = Range("h2")
Set rngb = Range("b2")
Set rngi = Range("I2")
Set rngd = Range("d2")
Set rnge = Range("e2")
Set rngc = Range("c2")

Set rnga3 = Sheets("Artikelen_in_stuklijsten").Range("a2")
Set rngh3 = Sheets("Artikelen_in_stuklijsten").Range("h2")
Set rngb3 = Sheets("Artikelen_in_stuklijsten").Range("b2")
Set rngi3 = Sheets("Artikelen_in_stuklijsten").Range("I2")
Set rnge3 = Sheets("Artikelen_in_stuklijsten").Range("e2")
Set rngc3 = Sheets("Artikelen_in_stuklijsten").Range("c2")

Set rnga4 = Sheets("Stuklijsten_aanmaken").Range("a2")
Set rngb4 = Sheets("Stuklijsten_aanmaken").Range("b2")
Set rngcd4 = Sheets("Stuklijsten_aanmaken").Range("c2:d2")
Set rngeg4 = Sheets("Stuklijsten_aanmaken").Range("e2:g2")

                  'the 500 becomes 500 + 1     [or 500 + samenstelling_aantal -1]  
                  With Sheets("Artikelen_aanmaken")
                        rnga.Formula = ("=N0&""_POS ""&referentie!C1&K2")
                        rngh.Formula = (rnga.Formula)
                        rngb.Formula = ("=R0&""_POS ""&referentie!C1&K2")
                        rngi.Formula = (rngb.Formula)
                        rngd.Formula = ("=N0&P0")
                        rnge.Formula = (rngd.Formula)
                        rngc.Value = ("6")
                    End With

                    Sheets("Artikelen_in_stuklijsten").Activate
                        rnga3.Formula = ("=Artikelen_aanmaken!Q0& Artikelen_aanmaken!N0")
                        rngh3.Formula = ("0")
                        rngb3.Formula = ("=Artikelen_aanmaken!N0&""_POS ""&referentie!C1")
                        rngi3.Formula = ("1")
                        rnge3.Formula = ("=referentie!D1")                                              
                        rngc3.Value = ("=Stuklijsten_aanmaken!D&""_POS ""&referentie!C1")  'becomes D2+1

Plutian的帮助代码:

Public Sub aantal_samenstellingen()

Dim samenstelling_aantal As String
Dim loopinstance As Integer

    Sheets("Artikelen_aanmaken").Activate

    samenstelling_aantal = InputBox("Hoeveel nieuwe samenstellingen zijn er? (MAX. 16 invoeren)")

       Range(2 + samenstelling_aantal * 30 & ":498").EntireRow.Hidden = True 'hides from row 3 + 30 times your number. So 33 for case 1, 63 for case 2 etc.


        For loopinstance = 500 To 500 + samenstelling_aantal - 1 'the -1 is since this loop needs to start from 0 when the instance is 1.


                With Sheets("Artikelen_aanmaken")
                    .Range("A" & 2 + loopinstance * 30).Formula = ("=N$" & loopinstance & "&""_POS ""&referentie!C1&K2") 'copies from range A and 2 + 30 times the instance. Either 30 times 0 = 0 or 30 times 1 is 30 etc.
                    .Range("H" & 2 + loopinstance * 30).Formula = (Range("A" & 2 + loopinstance * 30).Formula)
                    .Range("B" & 2 + loopinstance * 30).Formula = ("=R$" & loopinstance & "&""_POS ""&referentie!C1&K2") 'R&50" & loopinstance takes the loop instance number and pastes it to the end (so 500 for instance 1, 501 for the second etc.)
                    .Range("I" & 2 + loopinstance * 30).Formula = (Range("B" & 2 + loopinstance * 30).Formula)
                    .Range("D" & 2 + loopinstance * 30).Formula = ("=N$" & loopinstance & "&P$" & loopinstance)
                    .Range("E" & 2 + loopinstance * 30).Formula = (Range("D" & 2 + 30 * loopinstance).Formula)
                    .Range("C" & 2 + loopinstance * 30).Value = ("6")
                End With

                With Sheets("Artikelen_in_stuklijsten")
                    .Range("A" & 2 + loopinstance * 30).Formula = ("=Artikelen_aanmaken!Q$" & loopinstance & "& Artikelen_aanmaken!N$" & loopinstance)
                    .Range("H" & 2 + loopinstance * 30).Formula = ("0")
                    .Range("B" & 2 + loopinstance * 30).Formula = ("=Artikelen_aanmaken!N$" & loopinstance & "&""_POS ""&referentie!C1")
                    .Range("I" & 2 + loopinstance * 30).Formula = ("1")
                    .Range("E" & 2 + loopinstance * 30).Formula = ("=referentie!D1")
                   'I'm changing something in this code line
                   'new .Range("C" & 2 + loopinstance * 30).Value = (Sheets("Stuklijsten_aanmaken").Range("=D$" & loopinstance & "&""_POS ""&referentie!C1"))
                   'old .Range("C" & 2 + loopinstance * 30).Value = ("=Stuklijsten_aanmaken!D&""_POS ""&referentie!C1")

                    .Range("D" & 2 + loopinstance * 30).Value = ("1")
                End With


        Next loopinstance

    If samenstelling_aantal = "1" Then
                Exit Sub
            Else:
        With Sheets("Stuklijsten_aanmaken") 'this is outside the loop as it only needs to be called once. This takes the Samenstelling_aantal as the resize option.
            .Range("A2").AutoFill Destination:=.Range("A2").Resize(samenstelling_aantal), Type:=xlFillDefault
            .Range("B2").AutoFill Destination:=.Range("B2").Resize(samenstelling_aantal), Type:=xlFillCopy
            .Range("C2:D2").AutoFill Destination:=.Range("C2:D2").Resize(samenstelling_aantal), Type:=xlFillDefault
            .Range("E2:G2").AutoFill Destination:=.Range("E2:G2").Resize(samenstelling_aantal), Type:=xlFillCopy
        End With

    End If

End Sub

如果我没理解错的话,每个案例 select 都是前一个案例 select 操作的重复,再加上这个案例的操作。这可以通过循环轻松完成,已经将每个案例缩减为一个代码块。

其次,当它被替换为循环时,每个循环都是相同的,唯一改变的是它被调用的次数。因此,与其重复它,不如通过将案例 select 编号作为实例编号传递来调用循环。这正是我在下面所做的:

Sub caseselectreplacement()
Dim samenstelling_aantal As Integer

    Range(3 + samenstelling_aantal * 30 & ":498").EntireRow.Hidden = True 'hides from row 3 + 30 times your number. So 33 for case 1, 63 for case 2 etc.

samenstelling_aantal = 2 'Fake number for testing purposes

Dim loopinstance As Integer

For loopinstance = 0 To samenstelling_aantal - 1 'the -1 is since this loop needs to start from 0 when the instance is 1.
    With Sheets("Artikelen_aanmaken")
        .Range("A" & 2 + loopinstance * 30).Formula = ("=N" & loopinstance & "&""_POS ""&referentie!C1&K2") 'copies from range A and 2 + 30 times the instance. Either 30 times 0 = 0 or 30 times 1 is 30 etc.
        .Range("H" & 2 + loopinstance * 30).Formula = (Range("A2").Formula)
        .Range("B" & 2 + loopinstance * 30).Formula = ("=R" & loopinstance & "&""_POS ""&referentie!C1&K2") 'R&50" & loopinstance takes the loop instance number and pastes it to the end (so 500 for instance 1, 501 for the second etc.)
        .Range("I" & 2 + loopinstance * 30).Formula = (Range("B2").Formula)
        .Range("D" & 2 + loopinstance * 30).Formula = ("=N" & loopinstance & "&P" & loopinstance)
        .Range("E" & 2 + loopinstance * 30).Formula = (Range("D" & 2 + 30 * loopinstance).Formula)
        .Range("C" & 2 + loopinstance * 30).Value = ("6")
    End With

    With Sheets("Artikelen_in_stuklijsten")
        .Range("A" & 2 + loopinstance * 30).Formula = ("=Artikelen_aanmaken!Q" & loopinstance & "& Artikelen_aanmaken!N" & loopinstance)
        .Range("H" & 2 + loopinstance * 30).Formula = ("0")
        .Range("B" & 2 + loopinstance * 30).Formula = ("=Artikelen_aanmaken!N" & loopinstance & "&""_POS ""&referentie!C1")
        .Range("I" & 2 + loopinstance * 30).Formula = ("1")
        .Range("E" & 2 + loopinstance * 30).Formula = ("=referentie!D1")
        .Range("C" & 2 + loopinstance * 30).Value = ("=Stuklijsten_aanmaken!D&""_POS ""&referentie!C1")
    End With
Next loopinstance

With Sheets("Stuklijsten_aanmaken") 'this is outside the loop as it only needs to be called once. This takes the Samenstelling_aantal as the resize option.
    .Range("A2").AutoFill Destination:=.Range("A2").Resize(samenstelling_aantal), Type:=xlFillDefault
    .Range("B2").AutoFill Destination:=.Range("B2").Resize(samenstelling_aantal), Type:=xlFillDefault
    .Range("C2:D2").AutoFill Destination:=.Range("C2:D2").Resize(samenstelling_aantal), Type:=xlFillDefault
    .Range("E2:G2").AutoFill Destination:=.Range("E2:G2").Resize(samenstelling_aantal), Type:=xlFillCopy
End With
End Sub

请注意,它对我来说运行完美,但我无权访问您的数据,因此我无法检查是否正确提取了所有内容。请在测试前先备份您的数据,因为它可能会出现一些意外的结果。