简体   繁体   English

如何使用 for 循环,以便如果我索引字符串的特定值,我可以在 if 语句中使用它?

[英]How do use a for loop such that if I index a specific value of a string I can use it within an if statement?

I am currently working on a code where I loop through the the array as a string named "os".我目前正在编写一个代码,在该代码中我将数组作为名为“os”的字符串循环。

First I filter all the data in this workbook through a series of If statements.首先,我通过一系列If语句过滤此工作簿中的所有数据。
Then if the second loop reaches the first index of my string, namely "CM", I want to copy the data from another workbook into this workbook.然后,如果第二个循环到达我的字符串的第一个索引,即“CM”,我想将另一个工作簿中的数据复制到这个工作簿中。
Then the next loop begins, but this time if the second index of my string is reached, namely "PM", I want to copy other data from another workbook into this workbook.然后下一个循环开始,但是这次如果达到我的字符串的第二个索引,即“PM”,我想将另一个工作簿中的其他数据复制到这个工作簿中。

Image visualizing the copying process可视化复制过程的图像

Below you will find a section of my code corresponding to my problem.您将在下面找到与我的问题相对应的代码部分。 This code does not work for the part where I try to index my string, thus os(k) = 1 and os(k) = 2 .此代码不适用于我尝试索引字符串的部分,因此os(k) = 1os(k) = 2

Dim LastRow As Long
Dim j As Long
Dim k As Long
Dim os(1 To 2) As String

os(1) = "CM"
os(2) = "PM"

With ThisWorkbook.Worksheets("Brondata NB")
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With

For j = 2 To LastRow
    For k = 1 To 2
        If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 1).Value = "Q1" Then
            If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 2).Value = "Harmsenbrug" Then
                If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 4).Value = "0-20%" Then
                    If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 3).Value = os(k) = 1 Then
                        OpenBook.Sheets("Resultaten").Range("C19:D23").Copy
                        ThisWorkbook.Worksheets("Brondata NB").Cells(j, 5).PasteSpecial xlPasteValues
                    End If
        
                    If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 3).Value = os(k) = 2 Then
                        OpenBook.Sheets("Resultaten").Range("F19:G23").Copy
                        ThisWorkbook.Worksheets("Brondata NB").Cells(j, 5).PasteSpecial xlPasteValues
                    End If
                End If
            End If
        End If
    Next k
Next j

I managed to solve my problem.我设法解决了我的问题。 Here is the code.这是代码。

Sub GetDataFromFile()


Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim i As Integer
Dim LastRow As Long
Dim j As Long
Dim os(1 To 2) As String
Dim brug(1 To 15) As String
Dim kwt(1 To 4) As String
Dim k As Long
Dim n As Long


os(1) = "CM"
os(2) = "PM"

brug(1) = "B"
brug(2) = "N"
brug(3) = "R"
brug(4) = "C"
brug(5) = "G"
brug(6) = "Ha"
brug(7) = "H"
brug(8) = "M"
brug(9) = "b"
brug(10) = "S"
brug(11) = "Su"
brug(12) = "Br"
brug(13) = "D"
brug(14) = "V"
brug(15) = "W"

kwt(1) = "Q1"
kwt(2) = "Q2"
kwt(3) = "Q3"
kwt(4) = "Q4"

Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Blader naar uw bestand & importeer", filefilter:="Excel Files(*.xls*),*xls*", MultiSelect:=True)

     
      
            
            
If IsArray(FileToOpen) Then
    
    For i = LBound(FileToOpen) To UBound(FileToOpen)
        Set OpenBook = Application.Workbooks.Open(FileToOpen(i))
            If FileToOpen(i) Like "*Botlekbrug*" Or FileToOpen(i) Like "*Noord*" Or FileToOpen(i) Like "*Rijn*" Or _
                FileToOpen(i) Like "*Calandbrug*" Or FileToOpen(i) Like "*Giesserbrug*" Or FileToOpen(i) Like "*Harmsenbrug*" Or _
                FileToOpen(i) Like "*Haringvlietbrug*" Or FileToOpen(i) Like "*Merwedebrug*" Or FileToOpen(i) Like "*beneden*" Or _
                FileToOpen(i) Like "*Spijkenisserbrug*" Or FileToOpen(i) Like "*Suuroffbrug*" Or FileToOpen(i) Like "*Brienenoordbrug*" Or _
                FileToOpen(i) Like "*Dordrecht*" Or FileToOpen(i) Like "*Volkerakbrug*" Or FileToOpen(i) Like "*Wantijbrug*" Then
        
                
                With ThisWorkbook.Worksheets("Brondata NB")
                    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                End With

                For n = 1 To 4
                    For j = 2 To LastRow
                        For k = 1 To 15
                            If InStr(1, ThisWorkbook.Worksheets("Brondata NB").Cells(j, 1).Value, kwt(n)) And InStr(1, OpenBook.Path, kwt(n)) Then
                                If InStr(1, ThisWorkbook.Worksheets("Brondata NB").Cells(j, 2).Value, brug(k)) And InStr(1, OpenBook.Name, brug(k)) Then
                                    If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 4).Value = "0-20%" Then

                                        If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 3).Value = os(1) Then
                                            OpenBook.Sheets("Resultaten").Range("C19:D23").Copy
                                            ThisWorkbook.Worksheets("Brondata NB").Cells(j, 5).PasteSpecial xlPasteValues
                                            OpenBook.Sheets("Resultaten").Range("C25:D29").Copy
                                            ThisWorkbook.Worksheets("Brondata NB").Cells(j, 7).PasteSpecial xlPasteValues
                                        End If
            
                                        If ThisWorkbook.Worksheets("Brondata NB").Cells(j, 3).Value = os(2) Then
                                            OpenBook.Sheets("Resultaten").Range("F19:G23").Copy
                                            ThisWorkbook.Worksheets("Brondata NB").Cells(j, 5).PasteSpecial xlPasteValues
                                            OpenBook.Sheets("Resultaten").Range("F25:G29").Copy
                                            ThisWorkbook.Worksheets("Brondata NB").Cells(j, 7).PasteSpecial xlPasteValues
                                        End If
                                    End If
                                End If
                            End If
                        Next k
                    Next j
                Next n
                
            End If
            OpenBook.Close False


    Next i
    
End If


Application.ScreenUpdating = True
End Sub
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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