根据 IF 结果写入 TXT
Writing to TXT depending of IF outcome
我有一个 Excel 文件,其中添加了一些数据,它可以是 1 行,也可以是 1000 行。
VBA 脚本匹配将根据 C 和 E 列使用的结果:
Sub LoopRange()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
Dim N As Long
N = Cells(1, 1).End(xlDown).Row
For i = 2 To N
If Range("E" & i).Value = "South" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "South" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "West" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "West" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "North" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "North" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "East" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "East" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthWest" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthWest" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthEast" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthEast" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
End Sub
我想根据哪些 IF 语句为真来创建不同的文本文件。打印到文本文件的值是最静态的,但我想在文本中使用的 A 列和 B 列中的值。
例如:
第 1 行和第 9 行匹配 IF 语句
If Range("E" & i).Value = "South" And Range("C" & i).Value = "Yes" Then...
然后我想获取 A2 和 B2 中的值以用于文本文件中的一个结果以及一些其他静态代码。
我想在同一个文本文件的另一行中获取 A9 和 B2 中的值。
我找到了如何将数据导出到 TXT,但不知道如何根据 IF 语句选择要检索的内容。
myFile = Application.DefaultFilePath & "\South_Rename.txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next j
Next i
Close #1
有人有什么建议吗?
像下面这样的东西怎么样,请记住输出将在写入文本文件之前完全清空文本文件,因此我将 Append 方法作为替代方法添加到文本文件而不先清空其内容,看看下面代码中的注释:
Sub LoopRange()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
Dim N As Long
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
N = ws.Cells(1, 1).End(xlDown).Row 'get the last row with data on Column A
For i = 2 To N
If ws.Range("E" & i).Value = "South" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\South_Yes.txt" 'choose the text file to write to
'Open myFile For Output As #1 'Output will re-write the file
Open myFile For Append As #1 'Append will add the rows to the file
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "South" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\South_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "West" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\West_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "West" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\West_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "North" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\North_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "North" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\North_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "East" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\East_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "East" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\East_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthWest" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthWest_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthWest" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthWest_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthEast" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthEast_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthEast" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthEast_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
End If
Next i
End Sub
我有一个 Excel 文件,其中添加了一些数据,它可以是 1 行,也可以是 1000 行。
VBA 脚本匹配将根据 C 和 E 列使用的结果:
Sub LoopRange()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
Dim N As Long
N = Cells(1, 1).End(xlDown).Row
For i = 2 To N
If Range("E" & i).Value = "South" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "South" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "West" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "West" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "North" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "North" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "East" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "East" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthWest" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthWest" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthEast" And Range("C" & i).Value = "Yes" Then
'Do this
End If
Next i
For i = 2 To N
If Range("E" & i).Value = "NorthEast" And Range("C" & i).Value = "No" Then
'Do this
End If
Next i
End Sub
我想根据哪些 IF 语句为真来创建不同的文本文件。打印到文本文件的值是最静态的,但我想在文本中使用的 A 列和 B 列中的值。
例如: 第 1 行和第 9 行匹配 IF 语句
If Range("E" & i).Value = "South" And Range("C" & i).Value = "Yes" Then...
然后我想获取 A2 和 B2 中的值以用于文本文件中的一个结果以及一些其他静态代码。 我想在同一个文本文件的另一行中获取 A9 和 B2 中的值。
我找到了如何将数据导出到 TXT,但不知道如何根据 IF 语句选择要检索的内容。
myFile = Application.DefaultFilePath & "\South_Rename.txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next j
Next i
Close #1
有人有什么建议吗?
像下面这样的东西怎么样,请记住输出将在写入文本文件之前完全清空文本文件,因此我将 Append 方法作为替代方法添加到文本文件而不先清空其内容,看看下面代码中的注释:
Sub LoopRange()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
Dim N As Long
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
N = ws.Cells(1, 1).End(xlDown).Row 'get the last row with data on Column A
For i = 2 To N
If ws.Range("E" & i).Value = "South" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\South_Yes.txt" 'choose the text file to write to
'Open myFile For Output As #1 'Output will re-write the file
Open myFile For Append As #1 'Append will add the rows to the file
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "South" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\South_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "West" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\West_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "West" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\West_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "North" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\North_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "North" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\North_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "East" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\East_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "East" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\East_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthWest" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthWest_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthWest" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthWest_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthEast" And ws.Range("C" & i).Value = "Yes" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthEast_Yes.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
ElseIf ws.Range("E" & i).Value = "NorthEast" And ws.Range("C" & i).Value = "No" Then
acellValue = ws.Range("A" & i)
bcellValue = ws.Range("B" & i)
myFile = Application.DefaultFilePath & "\NorthEast_No.txt" 'choose the text file to write to
Open myFile For Append As #1
Write #1, acellValue & ", " & bcellValue & vbNewLine
Close #1
End If
Next i
End Sub