I have a strange issue. I have a set of 12 subs to prepare an external Excel file. When I group them together in one main sub, and execute, they somehow crash and the Excel file has wrong data in the end. But when I go to the VBA view and execute one by one, all is correct. Attached are the screens of correct (manual) execution and the corrupt (automatic) execution outcome. Below is the code of the subs:
Sub A_PZ_ZST_INB_MVT()
Workbooks.Open ("K:\WAW\Warehouse\ZSMOPL\KomunikatyOS,XML\ZST_INB_MVT.XLSX")
End Sub
Sub B_PZ_konwertujmaterial()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
[C:C].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End Sub
Sub C_PZ_konwertujilosc()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
[F:F].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End Sub
Sub D_PZ_kolumny()
Workbooks("ZST_INB_MVT.XLSX").Worksheets("Sheet1").Range("A:A").EntireColumn.Insert
Workbooks("ZST_INB_MVT.XLSX").Worksheets("Sheet1").Range("J:J").EntireColumn.Insert
[J:J].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End Sub
Sub E_PZ_prawdafalsz()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
Dim i As Integer
NumRows = Range("D1", Range("D1").End(xlDown)).Rows.Count
Range("A2").FormulaR1C1 = "=RC[2]=R[-1]C[2]"
Range("A2").Select
Selection.Copy
For i = 3 To NumRows
Range(Cells(3, 1), Cells(i, 1)).Select
ActiveSheet.Paste
Next i
End Sub
Sub F_PZ_kopiujinvoice()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
Application.ScreenUpdating = False
Dim LastRow As Long
Dim myRow As Long
Application.ScreenUpdating = False
' Find last row in column C with an entry
LastRow = Cells(Rows.Count, "C").End(xlUp).Row
' Loop through all rows in column C
For myRow = 1 To LastRow
' Check to see if current row is blank and row below is populated
If Cells(myRow, "C") = "" And Cells(myRow + 1, "C") <> "" Then
Cells(myRow, "C") = Cells(myRow + 1, "C")
End If
Next myRow
Application.ScreenUpdating = True
End Sub
Sub G_PZ_konwertujinvoice()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
[C:C].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End Sub
Sub H_PZ_usunduplikaty()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
Dim i As Long
For i = Cells(Rows.Count, "e").End(xlUp).Row To 1 Step -1
If Cells(i, "e") = "" Then Cells(i, "e").EntireRow.Delete xlUp
Next i
End Sub
Sub I_PZ_prawdafalsz2()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
Worksheets("Sheet1").Columns(1).ClearContents
Dim i As Integer
NumRows = Range("b1", Range("b1").End(xlDown)).Rows.Count
Range("A2").FormulaR1C1 = "=RC[2]=R[-1]C[2]"
Range("A2").Select
Selection.Copy
For i = 3 To NumRows
Range(Cells(3, 1), Cells(i, 1)).Select
ActiveSheet.Paste
Next i
End Sub
Sub J_PZ_puste_wiersze()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
Dim i As Long
Dim xLast As Long
Dim xRng As Range
Dim xTxt As String
NumRows = Range(("D2"), Range("D2").End(xlDown)).Rows.Count
On Error Resume Next
xTxt = Application.ActiveWindow.RangeSelection.Address
Set xRng = Application.Range("$A$2:$A$100")
xLast = xRng.Rows.Count
For i = xLast To 1 Step -1
If InStr(1, xRng.Cells(i, 1).Value, False) > 0 Then
Rows(xRng.Cells(i, 1).Row).Insert Shift:=xlDown
End If
Next
End Sub
Sub K_PZ_kopiujinvoice()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
Application.ScreenUpdating = False
Dim lr As Long
With ActiveSheet
lr = .Columns("C").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
On Error Resume Next
With .Range("C2:C100" & lr)
.SpecialCells(xlCellTypeBlanks).Formula = "=R[1]C"
.Value = .Value
End With
On Error GoTo 0
End With
Application.ScreenUpdating = True
End Sub
Sub L_PZ_kopiujvendor()
Application.Goto Workbooks("ZST_INB_MVT.XLSX").Sheets("Sheet1").Range("a2")
Application.ScreenUpdating = False
Dim lr As Long
With ActiveSheet
lr = .Columns("B").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
On Error Resume Next
With .Range("B2:B100" & lr)
.SpecialCells(xlCellTypeBlanks).Formula = "=R[1]C"
.Value = .Value
End With
On Error GoTo 0
End With
Application.ScreenUpdating = True
End Sub
And the main sub which groups them all:
Sub przyjecie()
A_PZ_ZST_INB_MVT
B_PZ_konwertujmaterial
C_PZ_konwertujilosc
D_PZ_kolumny
E_PZ_prawdafalsz
F_PZ_kopiujinvoice
G_PZ_konwertujinvoice
H_PZ_usunduplikaty
I_PZ_prawdafalsz2
J_PZ_puste_wiersze
K_PZ_kopiujinvoice
L_PZ_kopiujvendor
End Sub