Hello all,
I am currently trying to write a VBA-Code, which should use a Vlookup to get specific data from another workbook. (The column from which it should take the data should be flexible based on the month, which is why I implemented "ZielSpalte")
By now, I have the following code:
Public Function LResult(ByVal str As String) As String
LResult = Mid(str, 11, 7)
End Function
Sub CopyfromInputs()
Dim Month As String
Dim Year As String
Dim ii
Dim ws As Worksheet: Set ws = ActiveSheet
SourceBlatt = "MBR and CF data 2022"
wbkSource = "04MBR-02AA_Ressources_Plants_2022_BaPAA_20220503_1500.xlsx"
wbkUpload = "NWC_Ressources_WebForm_work.v2.xlsm"
UploadRessources = "Ressources_WebForm_BaP"
UploadNWC = "NWC_WebForm_BaP"
Workbooks(wbkUpload).Activate
If Sheets("Identification").Range("C6") < 10 Then Month = "0" & Sheets("Identification").Range("C6") Else Month = Sheets("Identification").Range("C6")
Year = Sheets("Identification").Range("C5")
SourceFile = "Link to Sharepoint"
'Open Source File
Workbooks.Open (SourceFile)
Sheets(SourceBlatt).Activate
'Define from which column it takes the figures, according to the "Identification" Sheet
Workbooks(wbkUpload).Activate
Sheets("Identification").Activate
IdentificationMonth = Range("C6").Value
ZielSpalte = IdentificationMonth + 3
'Use Vlookup
Sheets("Ressources_WebForm_BaP").Activate
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
For i = 7 To lr
ii = LResult(Range("A" & i).Text)
Cells(i, 4) = Application.WorksheetFunction.VLookup(ii, Workbooks("MBR-02AA_Ressources_Plants_2022_BaPAA_20220503_1500.xlsx").Worksheets("MBR and CF data 2022").Range("A8:T120"), ZielSpalte, False)
Next i
ActiveSheet.Calculate
End Sub
Everything works as it should be (Opening the source file and get lr/ii) until the red line at the end, here VBA is showing the error "Application-defined or object-defined error".
In the original code, I have defined the range of the Vlookup with variables (SourceBlatt/SourceFile), as this was not working I now even used the actual file names, but there still seems to be a problem.
Any hints from your side would be highly appreciated.
Thank you very much in advance!
BR
Bookmarks