zoukankan      html  css  js  c++  java
  • 20170624xlVBA生成通讯录文件

    Public Sub QqYunContactTransferCsvFile()
    '应用程序设置
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Application.Calculation = xlCalculationManual
    
        '错误处理
        'On Error GoTo ErrHandler
    
        '计时器
        Dim StartTime, UsedTime As Variant
        StartTime = VBA.Timer
    
        '变量声明
        Dim Wb As Workbook
        Dim Sht As Worksheet
        Dim Rng As Range
        Dim Arr As Variant
        Dim EndRow As Long
        Const SplitCount As Long = 100
        Dim RecordIndex As Long
        Dim FileCount As Long
        Dim EachLine As String
        Dim WholeLine As String
        Dim i As Long, j As Long
        Dim HeadLine As String
    
        '实例化对象
        Set Wb = Application.ThisWorkbook
    
        Set Sht = Wb.Worksheets("通讯录")
    
        With Sht
            EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
            Set Rng = .Range("A1:Y" & EndRow)
            Arr = Rng.Value
            RecordIndex = 0
            FileCount = 0
            HeadLine = ""
            For j = LBound(Arr, 2) To UBound(Arr, 2)
                HeadLine = HeadLine & """" & Arr(1, j) & ""","
            Next j
            WholeLine = HeadLine
    
            For i = LBound(Arr) + 1 To UBound(Arr)
                RecordIndex = RecordIndex + 1
                EachLine = ""
                For j = LBound(Arr, 2) To UBound(Arr, 2)
                    EachLine = EachLine & """" & Arr(i, j) & """," '有双引号
                    'EachLine = EachLine & Arr(i, j) & ","'无双引号
                Next j
                WholeLine = WholeLine & EachLine & vbCrLf
    
                If RecordIndex Mod SplitCount = (SplitCount - 1) Or i = UBound(Arr) Then '生成文件的条件
                    FileCount = FileCount + 1
                    Open Wb.Path & "" & FileCount & ".csv" For Output As #1     '生成CSV文件
                    Print #1, WholeLine   '写入CSV的内容
                    Close #1    '关闭文件句柄
                    WholeLine = HeadLine
                End If
    
            Next i
        End With
    
    
    
        '运行耗时
        UsedTime = VBA.Timer - StartTime
       
    
    ErrorExit:        '错误处理结束,开始环境清理
        Set Wb = Nothing
        Set Sht = Nothing
        Set Rng = Nothing
    
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        Application.Calculation = xlCalculationAutomatic
        Exit Sub
    ErrHandler:
        If Err.Number <> 0 Then
            MsgBox Err.Description & "!", vbCritical, "错误提示!"
            'Debug.Print Err.Description
            Err.Clear
            Resume ErrorExit
        End If
    End Sub
    

      

  • 相关阅读:
    路由器、交换机学习之IP地址、使用网络掩码划分子网
    PCB线宽与电流计算器--在线计算
    数组的访问形式
    STM32开发环境--使用MDK建立一个工程
    电源模块PCB设计
    STM32--TIM定时器时钟分割(疑难)
    STM32——输入捕获实验原理及配置步骤
    STM32——PWM基本知识及配置过程
    STM32——通用定时器基本定时功能
    STM32——NVIV:嵌套中断向量控制器
  • 原文地址:https://www.cnblogs.com/nextseven/p/7129187.html
Copyright © 2011-2022 走看看