Excel导出某列数据到txt文件

作者:追风剑情 发布于:2014-8-23 17:45 分类:VBA

测试文件版本Excel 2007

测试数据文件

Excel图.png


添加开发工具菜单

添加开发工具菜单.png


创建一个宏

创建一个宏.png


编写模块代码

编写模块代码.png


创建向导窗口

创建向导窗口.png


为窗口编写功能代码

向导窗口功能代码.png



以下为运行效果

运行效果1.png


运行效果2.png


运行效果3.png


下面贴出代码

模块代码

Public lastListIndex As Integer

Sub ExportLanguage()
    UserForm1.ComboBox1.Clear
    For i = 2 To 10 Step 1
        If Cells(1, i).value <> "" Then
           UserForm1.ComboBox1.AddItem (Cells(1, i))
        Else
            Exit For
        End If
    Next i

    UserForm1.ComboBox1.listIndex = lastListIndex
    
    UserForm1.Show
End Sub

窗口功能代码

Private Sub UserForm_Initialize()
    lastListIndex = 0
End Sub

Private Sub CommandButton1_Click()
    Dim listIndex As Integer
    listIndex = UserForm1.ComboBox1.listIndex
    lastListIndex = listIndex

    Dim path As String
    path = ThisWorkbook.path & "\" & ActiveSheet.Name & ".txt"
      
    Dim key As String
    Dim value As String
    Dim colIndex As Integer
    Dim empyCount As Integer
    Dim i As Integer
    
    colIndex = listIndex + 2
    empyCount = 0
    i = 0
    
    Open path For Output As #1
    Do While empyCount < 2
        i = i + 1
        key = Cells(i, 1).value
        
        If key <> "" Then
            value = Cells(i, colIndex)
            Print #1, key & Chr(9) & value
            empyCount = 0
        Else
            empyCount = empyCount + 1
        End If
    Loop
    Close #1
    
    MsgBox ("导出成功" & Chr(10) & path)
    UserForm1.Hide
    
End Sub

标签: VBA

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号