[英]VBA: set ranges in one column
I'm trying to save each instrument map as a separate text file.我正在尝试将每个仪器 map 保存为单独的文本文件。
Here's my code.这是我的代码。 It prints, but everything I try saves 16 repeats of AU2, AU3, or both... per file.它可以打印,但我尝试的所有操作都会保存 16 次重复 AU2、AU3 或两者……每个文件。
Private Sub Cubase()
Dim c As Range, r As Range, s As Range
Dim LR As Long
Dim output As String
Dim map As String
LR = Cells(Rows.Count, "B").End(xlUp).Row '(columns AQ & B are identical)
Set r = Range("AU2:AU" & LR)
For Each c In r
If c.Offset(0, -2).Value = 1 Then
output = c.Offset(0, -1).Value
map = c.Offset(0, -3).Value
Open output For Output As #1
For Each c In r
If map = c.Offset(0, -4).Value And c.Offset(0, -3).Text > 0 Then
Print #1, c.Value & Chr(10)
End If
Next
End If
Close
Next c
End Sub
Instead of setting a new range, I just used IF to filter the existing range.我没有设置新范围,而是使用 IF 过滤现有范围。 My code shows I had the right thought last night, just poorly implemented without sleep.我的代码显示我昨晚的想法是正确的,只是在没有睡眠的情况下实施得很差。 My new code:我的新代码:
Private Sub Cubase()
Dim c As Range, r As Range, s As Range
Dim LR As Long
Dim output As String
Dim map As String
'Find last used row in Column B
LR = Cells(Rows.Count, "B").End(xlUp).Row
'Set XML cells to look through, down to last row
Set r = Range("AU2:AU" & LR)
'Go through each cell, store output filename if row is a new map (2 left = 1)
For Each c In r
If c.Offset(0, -2).Value = 1 Then
Close #1
output = c.Offset(0, -1).Value
Open output For Output As #1
End If
If c.Offset(0, -3).Text > 0 Then
Print #1, c.Value & Chr(10)
End If
Next c
Close #1
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.