• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Force a Sub to complete before the following Sub runs.

15309mng

New Member
Hi all,
My data is downloaded from another program & needs reformatting/sorting to extract the relevant data. Part of the procedure is for one sub to delete non essential columns and the following applies a date format to other columns... and then continue with subsequent routines. When run in test mode, or individually, the results are as expected. However, when all macros are called, the second runs before the first completes, corrupting the results. I have tried including Do Events at various places in the routine, but without success. I could swap the order they are called as a work-around, but I would like to understand how to overcome the problem for future reference. Any ideas/explanation how to force the first to complete before the second runs?

Code as follows:

Code:
Sub MT09_DeleteEmptyColumnsWithHeader()

  Dim Col As Long, ColCnt As Long, Rng As Range

  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  DoEvents
  On Error GoTo Exits:
 
  If Selection.Columns.Count > 1 Then
  Set Rng = Selection
  Else
  Set Rng = Range(Columns(1), Columns(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column()))
  End If
  ColCnt = 0
  For Col = Rng.Columns.Count To 2 Step -1
  If Application.WorksheetFunction.CountA(Rng.Columns(Col).EntireColumn) < 2 Then
  Rng.Columns(Col).EntireColumn.Delete
  ColCnt = ColCnt + 1
  End If
  Next Col
 
Exits:
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic
  DoEvents
End Sub

followed by

Code:
Sub MT10_FormatDates()

Dim FX1 As String
Dim FX2 As String
Dim FX3 As String
Dim FX4 As String
Dim FX5 As String
Dim FX6 As String

FX1 = "=SUBSTITUTE((K2),""."",""/"")"
FX2 = "=SUBSTITUTE((L2),""."",""/"")"
FX3 = "=SUBSTITUTE((M2),""."",""/"")"
FX4 = "=IFERROR(IF(ISBLANK(P2),"""",DATEVALUE(P2)),"""")"
FX5 = "=DATEVALUE(Q2)"
FX6 = "=DATEVALUE(R2)"

  With Sheets("Delivery On Time")
  Application.ScreenUpdating = False
  
  .Range("K1:M1").Copy
  .Range("S1:U1").PasteSpecial

  
  .Range("P2:P" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = FX1
  .Range("Q2:Q" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = FX2
  .Range("R2:R" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = FX3
  .Range("S2:S" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = FX4
  .Range("T2:T" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = FX5
  .Range("U2:U" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = FX6
  
  Columns("S:U").Copy
  Columns("K:M").PasteSpecial xlPasteValues
  .Range("A:U").Sort Key1:=Range("J2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
  Columns("K:M").NumberFormat = "m/d/yyyy"
  
  Columns("P:U").Delete Shift:=xlToLeft
  
  End With
End Sub
 
Hi ,

From the code you have posted , it is not clear how you are calling all the subroutines. Do you have any Worksheet_Change event procedures in your affected sheets ?

Secondly , from your statements it is not clear :

1. how you know that the second runs before the first completes

2. what you mean by corrupting the results

Narayan
 
Hi ,

Hi Narayan

The code is called via a Private Sub routine in another workbook which opens the report, Saves As then on it goes as follows:
Code:
Private Sub CommandButton1_Click()


Dim wbkS As Workbook

Dim SourceFile As String

Dim SourcePath As String


SourcePath = Range("C6").Value

SourceFile = Range("C9").Value

Workbooks.Open SourcePath & "\" & SourceFile & ".xls"

Call MT001_RunMaterialTrackingReport

End Sub

Code:
Option Explicit

Sub MT001_RunMaterialTrackingReport()

  Call MT01_OpenWorkbookIfClosed

  Call MT02_SaveAsFromCellReference

  Call MT03_DeleteBeyondUsedRange

  Call MT04_CreateWorkSheets

  Call MT05_Copy

  Call MT06_ConfirmPosition

  Call MT07_Clear

  Call MT08_Concatenate

  Call MT09_DeleteEmptyColumnsWithHeader

  Call MT10_FormatDates

End Sub

Sub Test()

  Call MT11_WarehouseReturns

  Call MT12_DeletePM03s

  Call MT13_LateDelivery

  'Call MT14_SortLateDelivery

  Call MT15_RemoveEmptySheets

  Call MT16_Compress

  Call MT17_AssignWeekNumber

  Call MT18_AutoFitColumns

  Call MT19_ColourFillTopRow

  'Call MT20_InsertRowsAtValuesChange

  Call ActiveWorkBookSave

  Call MT21_SelectWOs

End Sub

the code trips on line
Code:
.Range("A:U").Sort Key1:=Range("J2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
with a "1004" runtime error.

Checking the spreadsheet shows the (deleted) columns in place and date format therefore applied to non relevant (but specified) columns.

Thanks for your interest

Regards Mark
 
Hi Mark ,

Thanks for the details. Let me understand this :

1. The starting point is the following procedure : CommandButton1_Click()

2. This calls the following procedure : MT001_RunMaterialTrackingReport()

3. This calls 10 other procedures , whose last two procedures are :

a. MT09_DeleteEmptyColumnsWithHeader()

b. MT10_FormatDates()

These are the 2 procedures you posted in your first post.

You say the problem appears in the Sort statement , which refers to .Range("A:U") i.e. the range("A:U") on the tab Delivery On Time ; the earlier procedure refers to Selection.

Within the last procedure , in some places you refer to .Range , in other places , you refer to Columns.

Without the workbook in front of me , it is difficult to visualize everything. Is it possible to upload your file ?

Narayan
 
Hi Narayan,

Sorry for the delay, I had a visitor, which was a nice reason to come away from the computer.

The Material Tracking file was too large as an .xls file so I have converted to an .xlsx file, which is smaller.
Preferred Results includes a copy of the sheet from the 'corrupted' version.

I have split the call for troubleshooting, and will be run as one when bugs are ironed out.

I see what you mean about Columns v .Range.
Any/all observations welcome.

Thanks & regards

Mark
 

Attachments

  • Material_Tracking.xlsx
    325.2 KB · Views: 2
  • Run MT Report.xlsm
    45.8 KB · Views: 1
  • Preferred Results_Materials Tracking Report 09-02-2014 17.54.31.xlsx
    627.2 KB · Views: 2
Hi Mark ,

Thanks for uploading your files.

When I ran the report , I get an error at this statement :

Worksheets("Material_Tracking").UsedRange.Copy

in the MT05_Copy procedure. Any idea how I can work around this ?

Narayan
 
Hi Narayan

I wonder why that would occur?

It simply copies the info from Sheet1 to Sheet2 before the slicing & dicing begins, (Material_Tracking > Delivery On Time). If all of the sheets have been generated as per the previous subs it should be possible to copy & paste manually.
I have run it just now and go past that point & trip up as before, on the ".Range("A:U").Sort Key1:=Range("J2"),..." (Line 240, Col 9) as the columns are still in place and the date format applied to inappropriate data.

I could work around the problem by scheduling the tasks at different stages, but understanding why it behaves this way to avoid/overcome trouble in the future is the 'end game'.

Oh, and yes to all your queries thus far, except that when the bugs are ironed out the Command Button will run the other ten or so procedures currently under Sub Test.

Thanks & regards Mark
 
Hi ,

I changed the statement to :

Worksheets("Sheet1").UsedRange.Copy

and the execution progressed to the Sort where it halted.

Prior to the Sort , when I put a breakpoint on the statement :

With Sheets("Delivery On Time")

the situation is as follows :

1. The activesheet is the one labelled Not Ordered.

2. The statement to be executed next is :

.Range("K1:M1").Copy

This will copy the range K1 through M1 on the sheet Delivery On Time ; I do not know why the 3 cells are copied and pasted to the cells S1 through U1.

You need to go through this section of code and see exactly what is to be done.

I am attaching the report workbook at this stage of the code. See how the code lines will affect the worksheet ; the statements which are converting the date formats are referring to the wrong columns.

Narayan
 

Attachments

  • Materials Tracking Report 10-02-2014 17.30.46.xlsx
    431.3 KB · Views: 4
Hi Narayan

Your changes have done the trick!! :awesome:
Activating the sheet is what was required.
The code now runs as I had hoped it would, without the hang time of before.
Thanks for your help... I appreciate your time & interest very much.

Kind regards
Mark
 
Back
Top