簡體   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