'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? [closed]
I am currently working on a code where I loop through the the array as a string named "os".
First I filter all the data in this workbook through a series of If statements.
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.
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.
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.
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
Solution 1:[1]
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 'If files are selected then the code below is followed
For i = LBound(FileToOpen) To UBound(FileToOpen) 'The number of files selected in a specific folder will form the upper and lower point for the loop
Set OpenBook = Application.Workbooks.Open(FileToOpen(i)) 'If the filename contains a certain string text then values are copied and pasted
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
