Hello all.
I am a manufacturing engineer and we have a program that tracks our inventory levels but does not generate a report that gives us changes each day, but can dump the data into an excel file. I have limped all day online and have made a pretty rough and probably embarrassing code that does the following.
1. Opens up a worksheet from inventory program with todays date, copys it to the active worksheet
2. Opens up a worksheet from inventory program with a user specified date, copys it to the active worksheet
3.Renames those sheets as Todays Date. Specified Date, and makes a new sheet called Comparison
4. Brings in the Part numbers from Todays date into the comparison sheet column A (reason I am doing it this way is if new parts come into stock the two sheets may not match, so I thought taking todays inventory part numbers and comparing it to previous with vlookup would avoid errors)
5. Finds the size of the part number column (basically how many entries there is)
6.Runs a loop that looks at the comparison part number, finds the inventory value from both the todays date and specified date sheet, subtracts them, and adds them to the comparison sheet
7.Loop until you are done.
I understand this code is probably so inefficient it'll make your headspin, but today is the first day I am writing VBA code in my life. I used matlab before so I can limp by, but for some reason, vlookup keeps giving me 1004 error code. I tried on the sheet itself and the logic I am using works.
Any help is appreciated. I can't post the data set as it's protected by every law under the sun, with security clearance thrown in there for good measure
Thanks,
Ryan
Sub RunCompare()
Dim sh As Integer, ShName As String
Dim wbPrev As Workbook
Dim wbCur As Workbook
Dim wsPrev As Sheets
Dim wsCur As Sheets
Dim iRow As Double, iCol As Double, iRow_Max As Double, iCol_Max As Double
Dim File1_Path As String, File2_Path As String, F1_Data As String, F2_Data As String
Dim SourceLastRow As Integer
Dim i As Integer
Dim ILB As String
Dim ILA As String
'Assign the Workbook File Name along with its Path
Dim DateFrom As Double
DateFrom = Worksheets("Compare Date").Range("B2").Value
File1_Path = "C:\Users\rkubik\Desktop\TEST" & "\" & Format(Date, "ddmmyyyy")
File2_Path = "C:\Users\rkubik\Desktop\TEST" & "\" & DateFrom
Application.ScreenUpdating = False
Set wbPrev = Workbooks.Open(File2_Path)
wbPrev.Sheets("Sheet1").Copy Before:=ThisWorkbook.Sheets("Comparison")
Worksheets("Sheet1").Name = "SpecifiedDate"
wbPrev.Close SaveChanges:=False
Set wbCur = Workbooks.Open(File1_Path)
wbCur.Sheets("Sheet1").Copy Before:=ThisWorkbook.Sheets("Comparison")
wbCur.Close SaveChanges:=False
Worksheets("Sheet1").Name = "TodaysDate"
Worksheets("Todaysdate").Range("A3:A739").Copy Worksheets("Comparison").Range("A1")
With Worksheets("Comparison")
SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).row
End With
i = 1
Do While i <= SourceLastRow
On Error Resume Next
ILB = Application.WorksheetFunction.VLookup(("A" & i), Worksheets("SpecifiedDate").Range("A3: D" & SourceLastRow + 3), 4, 0)
On Error GoTo 0
ILA = Application.WorksheetFunction.VLookup(("A" & i), Worksheets("TodaysDate").Range("A3: D" & SourceLastRow + 3), 4, 0)
On Error GoTo 0
Range("B" & i).Value = ILA - ILB
Range("C" & i).Value = ILA
i = i + 1
Loop
End Sub
I am a manufacturing engineer and we have a program that tracks our inventory levels but does not generate a report that gives us changes each day, but can dump the data into an excel file. I have limped all day online and have made a pretty rough and probably embarrassing code that does the following.
1. Opens up a worksheet from inventory program with todays date, copys it to the active worksheet
2. Opens up a worksheet from inventory program with a user specified date, copys it to the active worksheet
3.Renames those sheets as Todays Date. Specified Date, and makes a new sheet called Comparison
4. Brings in the Part numbers from Todays date into the comparison sheet column A (reason I am doing it this way is if new parts come into stock the two sheets may not match, so I thought taking todays inventory part numbers and comparing it to previous with vlookup would avoid errors)
5. Finds the size of the part number column (basically how many entries there is)
6.Runs a loop that looks at the comparison part number, finds the inventory value from both the todays date and specified date sheet, subtracts them, and adds them to the comparison sheet
7.Loop until you are done.
I understand this code is probably so inefficient it'll make your headspin, but today is the first day I am writing VBA code in my life. I used matlab before so I can limp by, but for some reason, vlookup keeps giving me 1004 error code. I tried on the sheet itself and the logic I am using works.
Any help is appreciated. I can't post the data set as it's protected by every law under the sun, with security clearance thrown in there for good measure
Thanks,
Ryan
Sub RunCompare()
Dim sh As Integer, ShName As String
Dim wbPrev As Workbook
Dim wbCur As Workbook
Dim wsPrev As Sheets
Dim wsCur As Sheets
Dim iRow As Double, iCol As Double, iRow_Max As Double, iCol_Max As Double
Dim File1_Path As String, File2_Path As String, F1_Data As String, F2_Data As String
Dim SourceLastRow As Integer
Dim i As Integer
Dim ILB As String
Dim ILA As String
'Assign the Workbook File Name along with its Path
Dim DateFrom As Double
DateFrom = Worksheets("Compare Date").Range("B2").Value
File1_Path = "C:\Users\rkubik\Desktop\TEST" & "\" & Format(Date, "ddmmyyyy")
File2_Path = "C:\Users\rkubik\Desktop\TEST" & "\" & DateFrom
Application.ScreenUpdating = False
Set wbPrev = Workbooks.Open(File2_Path)
wbPrev.Sheets("Sheet1").Copy Before:=ThisWorkbook.Sheets("Comparison")
Worksheets("Sheet1").Name = "SpecifiedDate"
wbPrev.Close SaveChanges:=False
Set wbCur = Workbooks.Open(File1_Path)
wbCur.Sheets("Sheet1").Copy Before:=ThisWorkbook.Sheets("Comparison")
wbCur.Close SaveChanges:=False
Worksheets("Sheet1").Name = "TodaysDate"
Worksheets("Todaysdate").Range("A3:A739").Copy Worksheets("Comparison").Range("A1")
With Worksheets("Comparison")
SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).row
End With
i = 1
Do While i <= SourceLastRow
On Error Resume Next
ILB = Application.WorksheetFunction.VLookup(("A" & i), Worksheets("SpecifiedDate").Range("A3: D" & SourceLastRow + 3), 4, 0)
On Error GoTo 0
ILA = Application.WorksheetFunction.VLookup(("A" & i), Worksheets("TodaysDate").Range("A3: D" & SourceLastRow + 3), 4, 0)
On Error GoTo 0
Range("B" & i).Value = ILA - ILB
Range("C" & i).Value = ILA
i = i + 1
Loop
End Sub