简体   繁体   English

关于 VBA(Excel 宏)中的循环限制的问题

[英]Question about Loop Limit in VBA (Excel Macro)

To preface my code could be simpler and better made without a doubt.毫无疑问,我的代码可以更简单,更好地编写序言。 Nevertheless, it does the job and works perfectly except for a hard loop limit that it encounters.尽管如此,除了遇到硬循环限制之外,它可以完美地完成工作并且工作得很好。 In short the objective is to write down dates for a given store and loop through all the dates in a store.简而言之,目标是写下给定商店的日期并循环浏览商店中的所有日期。 See Image 1 for the example input Example 1 - Input Now the output data only needs to return the dates in a column.有关示例输入,请参见图 1示例 1 - 现在输入output 数据只需返回列中的日期。 I have other parts of the code that formats and fixes things already so this portion only needs to write the dates 1 per column, See sample output in Example 2 Example 2 - Desired Output我有代码的其他部分已经格式化和修复了一些东西,所以这部分只需要每列写入日期 1,请参阅示例 2示例 2 中的示例 output - 所需的 Output

Again, the code works well, except that it runs into a problem and won't output past 373 rows and I know there are limits but I would have expected numbers well into the thousands for a simple nested loop.同样,代码运行良好,除了它遇到问题并且不会 output 超过 373 行,我知道有限制,但对于简单的嵌套循环,我希望数字能达到数千。 Now that is for the Unique Store IDs, For the other data it's checking against it stops around roughly 1469 rows.现在这是唯一商店 ID,对于它检查的其他数据,它大约停止在 1469 行左右。 A bigger number, but from what I could research online I shouldn't be hitting any limits just quite yet.一个更大的数字,但从我可以在线研究的内容来看,我应该还没有达到任何限制。 Is there something I am utilizing incorrectly to increase my limit?有什么我不正确地使用来增加我的限制吗? I apologize for any errors in formatting as I am new to coding & to the stacked overflow community.我对格式中的任何错误表示歉意,因为我是编码和堆叠溢出社区的新手。

Sub MissingDates()
'SubRoutine to check for missing Dates
Dim store As String
Dim storeunique As String
Dim missdat As String
Dim str As String
'Store = Store_ID; missdat = missing Date; Storeunique = Nonduplicate store column

'cel As Range
Dim i As Integer
Dim x As Integer
Dim j As Integer
Dim n As Integer
Dim a As Integer


x = 1
'x = 253
j = 0
'j = 150
i = 1

For i = 1 To 400


    store = Range("A" & x + 3).Value
    storeunique = Range("E" & i + 3).Value
    If storeunique = "" Then
        Exit For
    ElseIf store = storeunique Then
        For n = 1 To 31
        'n - Column of Dates to be checked against
            store = Range("A" & x + 3).Value

            If store = storeunique Then
                missdat = Range("B" & x + 3).Value
                a = Len(missdat)
                str = Left(missdat, a - 5)

                Cells(j + 4, n + 6).Value = str
            Else
                Exit For
            End If
            '
            x = x + 1
        Next n

    End If
        j = j + 1

Next i
End Sub

From reviewing your code and the details that you've given, it is most likely that you are hitting the exit condition of:通过查看您的代码和您提供的详细信息,您很可能遇到了以下退出条件:

If storeunique = "" Then
    Exit For

Have you checked that when you're assigning the 'storeunique' value, you're not running into a blank cell unexpectedly?您是否检查过,当您分配“storeunique”值时,您没有意外遇到空白单元格?

You're right that for the amount of rows (1469 from your original post) is far below the maximum integer value of 32,767 (more on data values from Microsoft's documentation here .) If this were the case, you would encounter an overflow error.您说得对,因为行数(原始帖子中的 1469 行)远低于 integer 的最大值 32,767(更多关于 Microsoft 文档中数据值)。如果是这种情况,您会遇到溢出错误。

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

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