用于根据列中的值将行导出到txt文件的脚本

时间:2018-01-12 10:21:52

标签: excel-vba vba excel

我正在寻找一个简单的脚本来根据特定单元格中的值导出行。找到了这个脚本(也许它可以做得更简单?)但需要帮助来过滤我的txt输出,这样只导出B列中带有“x”的行。

Sub Export()
'Declaring variables
Dim sLine As String
Dim sFName As String
Dim intFNumber As Integer
Dim lCounter As Long
Dim lLastRow As Long

'Just showing where the input data are
Worksheets("mysheet").Activate
Range("A9").Select

'Find the last row that contains data
With Worksheets("mysheet")
    lLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

'Setting the name and the path of text file based on workbook path
sFName = ThisWorkbook.Path & "\txtfile.txt"

'Get an unused file number
intFNumber = FreeFile

'Create a new file (or overwrite an existing one)
Open sFName For Output As #intFNumber

    'Choose first row to export
    For lCounter = 2 To lLastRow

        'Read specific data from the worksheet
        With Worksheets("mysheet")
            sLine = .Cells(lCounter, 1)
        End With

        'Write data to file if there is an x in column B (I just need column A to be written  what should ????? be?)
        If ?????? = "x" Then
            Print #intFNumber, sLine
        End If

    'Continue looping until the last row
    Next lCounter

'Close file
Close #intFNumber           
End Sub

1 个答案:

答案 0 :(得分:0)

这个怎么样:

If Worksheets("mysheet").cells(lCounter, 2) = "x" Then 'the 2 in this line refers to the column B
    Print #intFNumber, sLine
End If

如果B列中有x,则会写入A列的内容。

相关问题