Good day all experts,
I ran into this problem that the macro will stop when using shortcut key (I set to ctrl+shift+x) to run, but it will be smooth if I run each line one by one by pressing F8 in VBA window. After searching for all the relative posts on the forum, I still can't find (or probably not understand the solution) of what causes this problem since I am very new to VBA. The macro stops right after line" Set Wb2 = Workbooks.Open("\10.60.177.66\pm\DC\Daily shipping summary" & NM & ".xlsx")" It does open the file, but it stops there.
Maybe it is due to the file is still opening? I would really appreciate if someone can give me a solution. Here's the full code I wrote:
Sub ShippingReport()
'
' ShippingReport 巨集
'
' 快速鍵: Ctrl+Shift+X
'
Dim Wb1 As Object
Dim Wb2 As Object
Application.ScreenUpdating = False
Set Wb1 = ActiveWorkbook
Sheets("FXN TPE ---Shipping Report").Select
NM = InputBox("Insert shipping summary file name") 'ask the user for the file name
Set Wb2 = Workbooks.Open("\\10.60.177.66\pm\DC\Daily shipping summary\" & NM & ".xlsx")
Wb2.Sheets("工廠").Copy After:=Wb1.Sheets("SheetA")
Wb2.Sheets("PCBA to Hub").Copy After:=Wb1.Sheets("工廠")
Wb2.Close SaveChanges:=False
Dim dt, d1 As Variant
dt = InputBox("Enter the starting date(非2022/12/12格式不可執行)") 'ask the user for the starting date
dt = DateValue(dt)
d1 = dt + 1
d2 = d1 + 1
d3 = d2 + 1
d4 = d3 + 1
d5 = d4 + 1
d6 = d5 + 1
d7 = d6 + 1
Sheets("SheetA").Cells.Clear
Sheets("工廠").Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For x = 1 To FinalRow
' Decide if to copy based on column D
ThisValue = Cells(x, 1).Value
If ThisValue = dt Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
ElseIf ThisValue = d1 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
ElseIf ThisValue = d2 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
ElseIf ThisValue = d3 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
ElseIf ThisValue = d4 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
ElseIf ThisValue = d5 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
ElseIf ThisValue = d6 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
ElseIf ThisValue = d7 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("工廠").Select
End If
Next x
Sheets("PCBA to Hub").Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For x = 1 To FinalRow
' Decide if to copy based on column D
ThisValue = Cells(x, 1).Value
If ThisValue = dt Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
ElseIf ThisValue = d1 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
ElseIf ThisValue = d2 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
ElseIf ThisValue = d3 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
ElseIf ThisValue = d4 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
ElseIf ThisValue = d5 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
ElseIf ThisValue = d6 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
ElseIf ThisValue = d7 Then
Cells(x, 1).Resize(1, 12).Copy
Sheets("SheetA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("PCBA to Hub").Select
End If
Next x
Sheets("FXN TPE ---Shipping Report").Select
FinalRowB = Cells(Rows.Count, 1).End(xlUp).Row
Sheets("SheetA").Select
If Range("A2").Value = "" Then
Response = MsgBox("No data in selected period found", vbOKOnly, "無出貨資料")
Else
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To FinalRow
Cells(x, 1).Copy
Sheets("FXN TPE ---Shipping Report").Select
NextRowB = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRowB, 2).PasteSpecial Paste:=xlPasteValues
Cells(NextRowB, 3) = Cells(NextRowB, 2).Value + 10
Cells(NextRowB, 1) = "PO"
Sheets("SheetA").Select
Cells(x, 9).Copy
Sheets("FXN TPE ---Shipping Report").Select
Cells(NextRowB, 4).PasteSpecial Paste:=xlPasteValues
If Cells(NextRowB, 4) = "GDL" Then
Cells(NextRowB, 5) = "PCE PARAGON SOLUTIONS (MEXICO) SA DE CV"
Else: Cells(NextRowB, 5) = Cells(NextRowB, 4)
End If
Sheets("SheetA").Select
Cells(x, 2).Copy
Sheets("FXN TPE ---Shipping Report").Select
Cells(NextRowB, 6).PasteSpecial Paste:=xlPasteValues
Sheets("SheetA").Select
Cells(x, 3).Copy
Sheets("FXN TPE ---Shipping Report").Select
Cells(NextRowB, 7).PasteSpecial Paste:=xlPasteValues
If Cells(NextRowB, 7) = "1A620KP00-600-G-B+31" Then
Cells(NextRowB, 7) = "1A620KP00-600-G"
Cells(NextRowB, 8) = "Hanuman EVT Motherboard"
ElseIf Cells(NextRowB, 7) = "1A620P600-600-G-B+30" Then
Cells(NextRowB, 7) = "1A620P600-600-G"
Cells(NextRowB, 8) = "Hanuman EVT Midplane"
ElseIf Cells(NextRowB, 7) = "1A626DA00-600-G-B+30" Then
Cells(NextRowB, 7) = "1A626DA00-600-G"
Cells(NextRowB, 8) = "Hanuman EVT Riser"
ElseIf Cells(NextRowB, 7) = "1A626DE00-600-G-B+30" Then
Cells(NextRowB, 7) = "1A626DE00-600-G"
Cells(NextRowB, 8) = "Hanuman PVT Midplane"
ElseIf Cells(NextRowB, 7) = "1A626DF00-600-G-B+31" Then
Cells(NextRowB, 7) = "1A626DF00-600-G"
Cells(NextRowB, 8) = "Hanuman PVT Motherboard"
ElseIf Cells(NextRowB, 7) = "1A6288F00-600-G-B+31" Then
Cells(NextRowB, 7) = "1A6288F00-600-G"
Cells(NextRowB, 8) = "Hanuman EVT UPDB"
ElseIf Cells(NextRowB, 7) = "1A62FMM00-600-G-B+A0" Then
Cells(NextRowB, 7) = "1A62FMM00-600-G"
Cells(NextRowB, 8) = "Hanuman PVT UPDB"
ElseIf Cells(NextRowB, 7) = "1A62FMM00-600-G-B+30" Then
Cells(NextRowB, 7) = "1A62FMM00-600-G"
Cells(NextRowB, 8) = "Hanuman PVT UPDB"
ElseIf Cells(NextRowB, 7) = "1A62FMP00-600-G+30" Then
Cells(NextRowB, 7) = "1A62FMP00-600-G"
Cells(NextRowB, 8) = "Hanuman PVT UPDB"
ElseIf Cells(NextRowB, 7) = "1A62FR300-600-G-B+30" Then
Cells(NextRowB, 7) = "1A62FR300-600-G"
Cells(NextRowB, 8) = "Hanuman PVT Riser"
End If
Sheets("SheetA").Select
DAT = Cells(x, 5)
If Len(DAT) = 10 Then
Cells(x, 5).Copy
Sheets("FXN TPE ---Shipping Report").Select
Cells(NextRowB, 10).PasteSpecial Paste:=xlPasteValues
End If
Sheets("SheetA").Select
Cells(x, 4).Copy
Sheets("FXN TPE ---Shipping Report").Select
If Cells(NextRowB, 7) = "1A620KP00-600-G" Then
Cells(NextRowB, 13) = "1810.52"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A620P600-600-G" Then
Cells(NextRowB, 13) = "276.06"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A626DA00-600-G" Then
Cells(NextRowB, 13) = "27.63"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A626DE00-600-G" Then
Cells(NextRowB, 13) = "276.06"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A626DF00-600-G" Then
Cells(NextRowB, 13) = "1810.52"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A6288F00-600-G" Then
Cells(NextRowB, 13) = "180"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A62FMM00-600-G" Then
Cells(NextRowB, 13) = "180"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A62FMP00-600-G" Then
Cells(NextRowB, 13) = "180"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
ElseIf Cells(NextRowB, 7) = "1A62FR300-600-G" Then
Cells(NextRowB, 13) = "20.32"
Cells(NextRowB, 13).Interior.Color = RGB(255, 125, 125)
End If
Cells(NextRowB, 9).PasteSpecial Paste:=xlPasteValues
Cells(NextRowB, 14) = Cells(NextRowB, 9) * Cells(NextRowB, 13)
Cells(NextRowB, 16) = "Air"
Cells(NextRowB, 17) = "DB Schenker"
Sheets("SheetA").Select
Cells(x, 8).Copy
Sheets("FXN TPE ---Shipping Report").Select
Cells(NextRowB, 18).PasteSpecial Paste:=xlPasteValues
If Cells(NextRowB, 4) = "GDL" Then
Cells(NextRowB, 15) = "EXW"
ElseIf Cells(NextRowB, 4) = "DBS HUB - CVG" Then
Cells(NextRowB, 15) = "DDP"
ElseIf Cells(NextRowB, 4) = "DBS HUB - SLC" Then
Cells(NextRowB, 15) = "DDP"
ElseIf Cells(NextRowB, 4) = "DBS HUB - DUB" Then
Cells(NextRowB, 15) = "DAP"
ElseIf Cells(NextRowB, 4) = "DBS HUB - SIN" Then
Cells(NextRowB, 15) = "DAP"
End If
Cells(NextRowB, 19) = "N"
Sheets("SheetA").Select
Next x
End If
Application.DisplayAlerts = False
Wb1.Sheets("工廠").Delete
Wb1.Sheets("PCBA to Hub").Delete
Application.DisplayAlerts = True
Response2 = MsgBox("Do not forget to check Unit Price", vbOKOnly, "程式未填單價")
Sheets("FXN TPE ---Shipping Report").Select
Application.ScreenUpdating = True
End Sub
- I've tried to add a timer to delay the whole process right after the problematic line, however, the timer didn't even initiate so I guess the macro had stopped already.
- I've also tried "DoEvents" but later found that isn't the problem I aim to solve. Thank you for anyone responding and I will reply to everyone!