繁体   English   中英

VBA:在一列中设置范围

[英]VBA: set ranges in one column

我正在尝试将每个仪器 map 保存为单独的文本文件。

JPG:多个范围 - Excel VBA

这是我的代码。 它可以打印,但我尝试的所有操作都会保存 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

如何将每个乐器或列表识别为下一个要保存的范围?

我找到了自己的解决方案! =)

我没有设置新范围,而是使用 IF 过滤现有范围。 我的代码显示我昨晚的想法是正确的,只是在没有睡眠的情况下实施得很差。 我的新代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM