简体   繁体   中英

VBA: set ranges in one column

I'm trying to save each instrument map as a separate text file.

JPG:多个范围 - Excel VBA

Here's my code. It prints, but everything I try saves 16 repeats of AU2, AU3, or both... per file.

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

How do I identify each instrument or list as the next range to save?

I found my own solution! =)

Instead of setting a new range, I just used IF to filter the existing range. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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