简体   繁体   中英

Why does excel rowcount return max row, after a change on the sheet?

The code works "beautifully"(it needs to be cleaned up etc.) but if I delete rows or modify rows in Worksheets("Material For Quotation") and rerun the code it defaults to the max rowcount (1,048,576). I've searched around and read the Microsoft stuff and I cant figure it out. What I am stumped on is that the code should run independent of what's in the worksheet, but the minute I delete or modify the worksheet rowcount doesn't work.

Does anyone have insight?

Sub QuoteToMaterial()

Dim wb As Workbook
Dim ws, wscheck, ws1, ws2 As Worksheet

Dim i, j, k, l As Integer
Dim dict As New Dictionary
Dim Str As String

Dim key As Variant

Set wb = ThisWorkbook
Set ws1 = wb.Worksheets("Material For Quotation")
i = ws1.Cells(Rows.Count, 2).End(xlUp).row
j = 1
'init dictionary
For j = 2 To i
    If dict.Exists(Trim(UCase(ws1.Cells(j, 3).value))) = True Then
        dict(Trim(UCase(ws1.Cells(j, 3).value))) = dict(Trim(UCase(ws1.Cells(j, 3).value))) + ws1.Cells(j, 4).value
    Else
        dict.Add ws1.Cells(j, 3).value, ws1.Cells(j, 4).value
    End If
    
Next j

i = wb.Worksheets.Count
j = 1

For k = 1 To i
    Set wscheck = wb.Worksheets(k)
    Select Case True
        Case wscheck.Name = "Summary"
            GoTo Loopiter 'loop iterate
        Case wscheck.Name = "Material For Quotation"
            GoTo Loopiter
        Case wscheck.Name = "Pricing Summary"
            GoTo Loopiter
        Case wscheck.Name = "Installation 1"
            GoTo Loopiter
        Case wscheck.Name = "Installation 2"
            GoTo Loopiter
        Case wscheck.Name = "Inst Worksheet 1"
            GoTo Loopiter
        Case wscheck.Name = "Inst Worksheet 2"
            GoTo Loopiter
        Case Else
           
        Set ws = wb.Worksheets(k)
        'inserts $ per unit into the right place
        For l = 4 To 101
            
            If dict.Exists(ws.Cells(l, 9).value) = True Then
                ws.Cells(l, 11).value = dict(ws.Cells(l, 9).value)
            End If
            
        Next l

        j = j + 1
     End Select
Loopiter:
Next k

    

End Sub

Sorry Guys the answer to this problem is that we ending up putting table formatting on columns 1-4 down to row 1,048,576. So using ws.cells(rows.count,2) goes to the bottom because they are all taken up by a table.

to correct either remove the table formatting and just cell fill to get the look or use the listobject and get the number of filled rows there.

Error in finding last used cell in Excel with VBA

Sorry all I didn't realize the problem until everyone focused on the messy code lol

Thx.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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