excel 宏循環行數據 ,Excel統計所有sheet數據行數 VBA


Sub fun1()
'統計每一個sheet有多少行數據
Set s1 = Sheets("Sheet1")
'totalok = 0
For i = 1 To Sheets.Count
s1.Cells(i, 1) = Sheets(i).Name
r = Sheets(i).Range("A65535").End(xlUp).Row

s1.Cells(i, 2) = r
If i > 4 Then
totalok = totalok + r
End If
Next
s1.Cells(1, 3) = totalok
End Sub

 
Sub Fun2()
    '一個sheet表中循環查另一個sheet中是否存在,
查地名是否存在 OK 'Set Sheet = Worksheets("Sheet251") Set dbSH = Sheets("pd_port") 'Set newSH = Sheets("(CN) CHINA") Cells(1, 12) = Now() ' 開始時間 Dim i i = 2 Do While Cells(i, 2) <> "" newCode = Trim(UCase(Cells(i, 2))) newName = Trim(UCase(Cells(i, 4))) isAirPort = "" isSeaPort = "" isRailway = "" PortType = 3 ' 0港口,1機場,2火車站 3其他 If InStr(newName, " APT") > 0 Then isAirPort = "Yes AirPort" PortType = 1 End If If InStr(newName, " PT") > 0 Then isSeaPort = "Yes SeaPort" PortType = 0 End If If InStr(newName, " RAILWAY") > 0 Then isRailway = "Yes Railway" PortType = 2 End If r1 = "code No" r2 = "name No" For j = 2 To 5192 dbCode = Trim(UCase(dbSH.Cells(j, 6))) dbName = Trim(UCase(dbSH.Cells(j, 4))) If PortType = 1 Then dbName = Replace(dbName, " Apt", " AIRPORT") newCode = Right(newCode, 3) newName = Replace(newName, " Apt", "") End If If dbCode = newCode Then r1 = "code Exist" End If If dbName = newName Then r2 = "name Exist" End If Next Cells(i, 12) = r1 Cells(i, 13) = r2 Cells(i, 14) = isAirPort Cells(i, 15) = isSeaPort Cells(i, 16) = isRailway Cells(i, 17) = PortType i = i + 1 Loop Cells(1, 13) = Now() ' 結束時間 End Sub
Sub Fun3()
    
'一個sheet表中循環查另一個sheet中是否存在,
  ’
查地名是否存在 test 'Set Sheet = Worksheets("Sheet251") Set dbSH = Sheets("pd_port") 'Set newSH = Sheets("(CN) CHINA") Cells(1, 12) = Now() ' 開始時間 Dim i i = 2 'Do While Cells(i, 2) <> "" For i = 1105 To 1110 newCode = Trim(UCase(Cells(i, 2))) newName = Trim(UCase(Cells(i, 4))) isAirPort = "" isSeaPort = "" isRailway = "" PortType = 3 ' 0港口,1機場,2火車站 3其他 If InStr(newName, " APT") > 0 Then isAirPort = "Yes AirPort" PortType = 1 End If If InStr(newName, " PT") > 0 Then isSeaPort = "Yes SeaPort" PortType = 0 End If If InStr(newName, " RAILWAY") > 0 Then isRailway = "Yes Railway" PortType = 2 End If r1 = "code No" r2 = "name No" For j = 2 To 5192 dbCode = Trim(UCase(dbSH.Cells(j, 6))) dbName = Trim(UCase(dbSH.Cells(j, 4))) If PortType = 1 Then dbName = Replace(dbName, " Apt", " AIRPORT") newCode = Right(newCode, 3) newName = Replace(newName, " Apt", "") End If If dbCode = newCode Then r1 = "code Exist" End If If dbName = newName Then r2 = "name Exist" End If Next Cells(i, 12) = r1 Cells(i, 13) = r2 Cells(i, 14) = isAirPort Cells(i, 15) = isSeaPort Cells(i, 16) = isRailway Cells(i, 17) = PortType 'i = i + 1 'Loop Next Cells(1, 13) = Now() ' 結束時間 End Sub
Sub Fun4()
    ' 查國家是否存在

     ' Set Sheet = Worksheets("Sheet251")
     Set dbSH = Sheets("pd_country")
     Set newSH = Sheets("Country Name")
    newSH.Cells(1, 3) = Now() ' 開始時間
    
    For i = 2 To 250
       ' Sheet.Rows(i).Cells (0)
       ' Print Sheet.Cells(i, 1)
      ' Print Worksheets("Sheet251").Cells(i, 1).Value
      newCode = UCase(newSH.Cells(i, 1))
      newName = UCase(newSH.Cells(i, 2))
      
      ResultCode = "code No"
      ResultName = "name No"
      For j = 1 To 255
        dbCode = UCase(dbSH.Cells(j, 5))
        dbName = UCase(dbSH.Cells(j, 3))
        
        ' MsgBox dbValue + "---" + newValue
        If dbCode = newCode Then
            ResultCode = "code Exist"
        End If
        
        If dbName = newName Then
            ResultName = "name Exist"
        End If
        
        
      Next
      
      newSH.Cells(i, 3) = ResultCode
      newSH.Cells(i, 4) = ResultName
      

      
      newSH.Cells(1, 4) = Now() ' 結束時間
    Next
End Sub

 

Sub stoCheckTable()

    msg = "一.請確保【申通賬單】表格與【HELKA導出寄件數據】表格已准備;"
    msg = msg + vbCrLf + "二.請確保第1個表格為【申通賬單]表格】;"
    msg = msg + vbCrLf + "三.請確保第2個表格為【HELKA導出寄件數據】表格;"
    msg = msg + vbCrLf + "四.點[是]開始核對數據!"
    msgResult = MsgBox(msg, vbYesNo, "申通快遞數據核對")
    
    
    If msgResult = 6 Then
        Set stoTable = Sheets(1) '申通賬單表格
        Set helkaTable = Sheets(2) 'HELKA導出的表格
        
        stoIndex = 3 ' 申通的表格 第幾行開始循環
        NotFoundCount = 0 '幾條沒有找到數量統計
        Do While stoTable.Cells(stoIndex, 2) <> ""
            stoNo = Str(Trim(stoTable.Cells(stoIndex, 2)))
                helkaIndex = 2 'HELKA導出的表格 第幾行開始循環
                okIndex = 0
                Do While helkaTable.Cells(helkaIndex, 1) <> ""
                    TempNo = Trim(helkaTable.Cells(helkaIndex, 3))
                    If TempNo <> "" Then
                        helkaNo = Str(Trim(helkaTable.Cells(helkaIndex, 3)))
                        
                        If (stoNo = helkaNo) Then
                            'MsgBox "找到了" + stoNo + "=" + helkaNo + "__ helkaIndex:" + Str(helkaIndex)
                            okIndex = helkaIndex
                            Exit Do
                        End If
                        
                    End If
                    helkaIndex = helkaIndex + 1
                Loop
                If okIndex > 0 Then
                    stoTable.Cells(stoIndex, 12) = "核對成功"
                    stoTable.Cells(stoIndex, 13) = helkaTable.Cells(okIndex, 4)
                    stoTable.Cells(stoIndex, 14) = helkaTable.Cells(okIndex, 5)
                    stoTable.Cells(stoIndex, 15) = helkaTable.Cells(okIndex, 6)
                    
                Else
                    NotFoundCount = NotFoundCount + 1
                    stoTable.Cells(stoIndex, 12) = "不存在"
                    stoTable.Cells(stoIndex, 12).Interior.ColorIndex = 6
                End If
                
            stoIndex = stoIndex + 1
        Loop
        
        
        If NotFoundCount > 0 Then
            stoTable.Cells(stoIndex, 12) = "核對結果"
            stoTable.Cells(stoIndex, 13) = "狀態"
            stoTable.Cells(stoIndex, 14) = "物品名稱"
            stoTable.Cells(stoIndex, 15) = "備注"
            MsgBox "共發現" + Str(NotFoundCount) + " 條不存在的數據!"
        Else
            MsgBox "核對完成,所有單號都找到!"
        End If
        
    End If
    
End Sub

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM