Macro doesnt seem to run final part / run fully

PureBluff

Board Regular
Joined
Apr 4, 2014
Messages
155
Bit of a strange one here, when I run this macro, the loop at the end doesn't take place or when it does, b46,b47,b48 are correct - but then the next formula in b49 is corrupted

Code:
Sub Macro2()

order1 = Sheets("Customer Order").Range("c9").Value
order2 = Sheets("Customer Order").Range("c10").Value

If Sheets("customer order").Range("f7").Value = "" Then
MsgBox ("No date specified in cell F7, please fix!")
Exit Sub
End If


'#order 1 blank
If order1 = "" Then
MsgBox ("Order not speficied!")
Exit Sub
End If
'#end order 1 blank

'#2 order syntax
'# -- order lines --
If order1 <> "" And order2 <> "" Then
With ActiveWorkbook.Connections("Query from FOPS").ODBCConnection
.BackgroundQuery = True
.CommandText = Array( _
"SELECT *" & Chr(13) & "" & Chr(10) & "FROM FOPS6.ORDER_LINES ORDER_LINES" & Chr(13) & "" & Chr(10) & "WHERE (ORDER_LINES.Order_number=" & order1 & ") AND (ORDER_LINES.Boxes_despatched>0) OR (ORDER_LINES.Order_number=" & order2 & ") AND (ORDER_LINES.Boxes_despatched>0)" & Chr(13) & "" & Chr(10) & "ORDER BY ORDER_LINES.Product_Code asc")
.CommandType = xlCmdSql
.Connection = Array(Array( _
"ODBC;DSN=FOPS;ServerName=UKHUNSPVP21.1583;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;DBQ=FOPS6;OpenMode=0;ClientVersion=14.1" _
), Array( _
"1.011.000;CodePageConvert=1252;PvClientEncoding=CP1252;PvServerEncoding=CP1252;" _
))
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("Query from FOPS")
.Name = "Query from FOPS"
.Description = ""
End With
    ActiveWorkbook.Connections("Query from FOPS").Refresh



' -- end order lines


' -- order header

        With ActiveWorkbook.Connections("Query from FOPS1").ODBCConnection
.BackgroundQuery = True
.CommandText = Array( _
"SELECT *" & Chr(13) & "" & Chr(10) & "FROM FOPS6.ORDER_HEADER ORDER_HEADER" & Chr(13) & "" & Chr(10) & "WHERE (ORDER_HEADER.Order_number=" & order1 & ") OR (ORDER_HEADER.Order_number=" & order2 & ")")
.CommandType = xlCmdSql
.Connection = Array(Array( _
"ODBC;DSN=FOPS;ServerName=UKHUNSPVP21.1583;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;DBQ=FOPS6;OpenMode=0;ClientVersion=14.1" _
), Array( _
"1.011.000;CodePageConvert=1252;PvClientEncoding=CP1252;PvServerEncoding=CP1252;" _
))
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("Query from FOPS1")
.Name = "Query from FOPS1"
.Description = ""
End With
    ActiveWorkbook.Connections("Query from FOPS1").Refresh


' -- end order header
End If
'#end of 2 order syntax







'#single order syntax

' -- order lines --
If order1 <> "" And order2 = "" Then


With ActiveWorkbook.Connections("Query from FOPS").ODBCConnection
.BackgroundQuery = True
.CommandText = Array( _
"SELECT *" & Chr(13) & "" & Chr(10) & "FROM FOPS6.ORDER_LINES ORDER_LINES" & Chr(13) & "" & Chr(10) & "WHERE (ORDER_LINES.Order_number=" & order1 & ") AND (ORDER_LINES.Boxes_despatched>0)" & Chr(13) & "" & Chr(10) & "ORDER BY ORDER_LINES.Product_Code asc")
.CommandType = xlCmdSql
.Connection = Array(Array( _
"ODBC;DSN=FOPS;ServerName=UKHUNSPVP21.1583;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;DBQ=FOPS6;OpenMode=0;ClientVersion=14.1" _
), Array( _
"1.011.000;CodePageConvert=1252;PvClientEncoding=CP1252;PvServerEncoding=CP1252;" _
))
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("Query from FOPS")
.Name = "Query from FOPS"
.Description = ""
End With
ActiveWorkbook.Connections("Query from FOPS").Refresh
' -- end order lines --

' -- order header

        With ActiveWorkbook.Connections("Query from FOPS1").ODBCConnection
.BackgroundQuery = True
.CommandText = Array( _
"SELECT *" & Chr(13) & "" & Chr(10) & "FROM FOPS6.ORDER_HEADER ORDER_HEADER" & Chr(13) & "" & Chr(10) & "WHERE (ORDER_HEADER.Order_number=" & order1 & ")")
.CommandType = xlCmdSql
.Connection = Array(Array( _
"ODBC;DSN=FOPS;ServerName=UKHUNSPVP21.1583;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;DBQ=FOPS6;OpenMode=0;ClientVersion=14.1" _
), Array( _
"1.011.000;CodePageConvert=1252;PvClientEncoding=CP1252;PvServerEncoding=CP1252;" _
))
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("Query from FOPS1")
.Name = "Query from FOPS1"
.Description = ""
End With
    ActiveWorkbook.Connections("Query from FOPS1").Refresh



' -- end order header
End If

'#end of single order syntax

Sheets("customer order").Activate




Range("b46:b144").ClearContents


Dim i, j As Long
j = 2
For i = 46 To 144

ActiveSheet.Range("b" & i).Formula = _
"=IF(ISERROR(TRIM('Order Lines'!C" & j & ")),"""",(TRIM('Order Lines'!C" & j & ")))"

j = j + 1
Next I

''
'Call SaveFile

end Sub

If it's corrupted B49, the formula in there should be:
Code:
=IF(ISERROR(TRIM('Order Lines'!C5)),"",(TRIM('Order Lines'!C5)))

however it's always
Code:
=IF(ISERROR(TRIM('Order Lines'!C49)),"",(TRIM('Order Lines'!C49)))

Can anyone shed some light why this corruption may be happening?

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,845
Office Version
  1. 2010
Platform
  1. Windows
try replacing these lines of code:
VBA Code:
Dim i, j As Long
j = 2
For i = 46 To 144

ActiveSheet.Range("b" & i).Formula = _
"=IF(ISERROR(TRIM('Order Lines'!C" & j & ")),"""",(TRIM('Order Lines'!C" & j & ")))"

j = j + 1
Next i

with these:
VBA Code:
Dim i, j As Long
Dim outarr(1 To 99, 1 To 1)
j = 2
For i = 1 To 99
outarr(i, 1) = "=IF(ISERROR(TRIM('Order Lines'!C" & j & ")),"""",(TRIM('Order Lines'!C" & j & ")))"
'ActiveSheet.Range("b" & i).Formula = _
'"=IF(ISERROR(TRIM('Order Lines'!C" & j & ")),"""",(TRIM('Order Lines'!C" & j & ")))"

j = j + 1
Next i
ActiveSheet.Range("b46:b144").Formula = outarr
at the very least it will be faster!!
 

Watch MrExcel Video

Forum statistics

Threads
1,132,803
Messages
5,655,400
Members
418,196
Latest member
kendocon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top