PC Review


Reply
Thread Tools Rate Thread

How do I trapped the error and optimize the code?

 
 
Jay
Guest
Posts: n/a
 
      15th Jan 2009
How do I trapped this error in my code?
kindly try using the procedure just paste it.

-Input/ click on the activecell.column which is not indicated in those sets
of array?
-how do I optimized this code?

Thank you in advance,
Jay


Sub Batchdist()

Dim pMonths, dMonths As Long
Dim DomesticBlkTotcell, ForecastValue, clBInventory, clProduction,
clEInventory As Variant
Dim clBonna As Long
Dim clProdx, clBInvx, clFcstx, clEInvx As Variant
Dim blStatus As Boolean

clprdnBonna = 17
clfcstBonna = 18
cleiBonna = 19

'//List of all Production column number transferred to an array
clProduction = Array(7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51,
55, 59, 63)
clBInventory = Array(5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49,
53, 57, 61)
clForecast = Array(8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52,
56, 60, 64)
clEInventory = Array(9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53,
57, 61, 65)

With ActiveCell
rw = .Row
cl = .Column
End With

'//Checks if Activecell is on Production Column
clProdx = clProduction
clBInvx = clBInventory
clFcstx = clForecast
clEInvx = clEInventory

For clProduction = 0 To 15
For clBInventory = 0 To 15
For clForecast = 0 To 15
For clEInventory = 0 To 15

If ActiveCell.Column = clProdx(clProduction) Then
MsgBox "Production Column"
Exit Sub

Else
On Error GoTo nxtLine:

If ActiveCell.Column = clBInvx(clBInventory) Then
MsgBox "Beginning Inventory Column"
Exit Sub

Else
On Error GoTo nxtLine1:

If ActiveCell.Column = clFcstx(clForecast) Then
MsgBox "Forecast Column"
Exit Sub

Else
On Error GoTo nxtLine2:

If ActiveCell.Column = clEInvx(clEInventory) Then
MsgBox "Ending Inventory Column"
Exit Sub

Else
On Error GoTo nxtLine3:

End If
End If
End If
End If

nxtLine3:
Next clEInventory
nxtLine2:
Next clForecast
nxtLine1:
Next clBInventory
nxtLine:
Next clProduction

End Sub
 
Reply With Quote
 
 
 
 
JE McGimpsey
Guest
Posts: n/a
 
      15th Jan 2009
Perhaps:

With ActiveCell
If .Column >= 5 And .Column <= 64 Then _
MsgBox Choose(.Column Mod 4 + 1, "Forecast", _
"Beginning Inventory", "Ending Inventory", _
"Production") & " Column"
End With

I'm assuming you didn't mean to duplicate the column numbers for
clBInventory and clEInventory...


In article <8C174C90-9DC0-4D71-BBA5-(E-Mail Removed)>,
Jay <(E-Mail Removed)> wrote:

> How do I trapped this error in my code?
> kindly try using the procedure just paste it.
>
> -Input/ click on the activecell.column which is not indicated in those sets
> of array?
> -how do I optimized this code?
>
> Thank you in advance,
> Jay
>
>
> Sub Batchdist()
>
> Dim pMonths, dMonths As Long
> Dim DomesticBlkTotcell, ForecastValue, clBInventory, clProduction,
> clEInventory As Variant
> Dim clBonna As Long
> Dim clProdx, clBInvx, clFcstx, clEInvx As Variant
> Dim blStatus As Boolean
>
> clprdnBonna = 17
> clfcstBonna = 18
> cleiBonna = 19
>
> '//List of all Production column number transferred to an array
> clProduction = Array(7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51,
> 55, 59, 63)
> clBInventory = Array(5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49,
> 53, 57, 61)
> clForecast = Array(8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52,
> 56, 60, 64)
> clEInventory = Array(9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53,
> 57, 61, 65)
>
> With ActiveCell
> rw = .Row
> cl = .Column
> End With
>
> '//Checks if Activecell is on Production Column
> clProdx = clProduction
> clBInvx = clBInventory
> clFcstx = clForecast
> clEInvx = clEInventory
>
> For clProduction = 0 To 15
> For clBInventory = 0 To 15
> For clForecast = 0 To 15
> For clEInventory = 0 To 15
>
> If ActiveCell.Column = clProdx(clProduction) Then
> MsgBox "Production Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine:
>
> If ActiveCell.Column = clBInvx(clBInventory) Then
> MsgBox "Beginning Inventory Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine1:
>
> If ActiveCell.Column = clFcstx(clForecast) Then
> MsgBox "Forecast Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine2:
>
> If ActiveCell.Column = clEInvx(clEInventory) Then
> MsgBox "Ending Inventory Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine3:
>
> End If
> End If
> End If
> End If
>
> nxtLine3:
> Next clEInventory
> nxtLine2:
> Next clForecast
> nxtLine1:
> Next clBInventory
> nxtLine:
> Next clProduction
>
> End Sub

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      15th Jan 2009
More than likely, JE has given you what you want; however, it would be
better when you are posting long code to tell us what the code is attempting
to do so we don't have to try and figure it out (I decided not to try,
apparently JE did try). I did want to point out one 'minor' problem with one
part of your code. This line...

Dim pMonths, dMonths As Long

is probably not doing what you intended it to do. Only dMonths is declared
as a Long; pMonths is declared as a Variant. Unlike lots of other languages,
in VB, every variable must be declared as to Type individually; otherwise,
if it isn't, that variable is then declared as the default data Type which,
in VB, is a Variant. The above line needs to be written like this...

Dim pMonths As Long, dMonths As Long

or like this...

Dim pMonths As Long
Dim dMonths As Long

--
Rick (MVP - Excel)


"Jay" <(E-Mail Removed)> wrote in message
news:8C174C90-9DC0-4D71-BBA5-(E-Mail Removed)...
> How do I trapped this error in my code?
> kindly try using the procedure just paste it.
>
> -Input/ click on the activecell.column which is not indicated in those
> sets
> of array?
> -how do I optimized this code?
>
> Thank you in advance,
> Jay
>
>
> Sub Batchdist()
>
> Dim pMonths, dMonths As Long
> Dim DomesticBlkTotcell, ForecastValue, clBInventory, clProduction,
> clEInventory As Variant
> Dim clBonna As Long
> Dim clProdx, clBInvx, clFcstx, clEInvx As Variant
> Dim blStatus As Boolean
>
> clprdnBonna = 17
> clfcstBonna = 18
> cleiBonna = 19
>
> '//List of all Production column number transferred to an array
> clProduction = Array(7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51,
> 55, 59, 63)
> clBInventory = Array(5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49,
> 53, 57, 61)
> clForecast = Array(8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52,
> 56, 60, 64)
> clEInventory = Array(9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53,
> 57, 61, 65)
>
> With ActiveCell
> rw = .Row
> cl = .Column
> End With
>
> '//Checks if Activecell is on Production Column
> clProdx = clProduction
> clBInvx = clBInventory
> clFcstx = clForecast
> clEInvx = clEInventory
>
> For clProduction = 0 To 15
> For clBInventory = 0 To 15
> For clForecast = 0 To 15
> For clEInventory = 0 To 15
>
> If ActiveCell.Column = clProdx(clProduction) Then
> MsgBox "Production Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine:
>
> If ActiveCell.Column = clBInvx(clBInventory) Then
> MsgBox "Beginning Inventory Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine1:
>
> If ActiveCell.Column = clFcstx(clForecast) Then
> MsgBox "Forecast Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine2:
>
> If ActiveCell.Column = clEInvx(clEInventory) Then
> MsgBox "Ending Inventory Column"
> Exit Sub
>
> Else
> On Error GoTo nxtLine3:
>
> End If
> End If
> End If
> End If
>
> nxtLine3:
> Next clEInventory
> nxtLine2:
> Next clForecast
> nxtLine1:
> Next clBInventory
> nxtLine:
> Next clProduction
>
> End Sub


 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      15th Jan 2009
And to get completely esoteric, the exception to Rick's excellent
explanation is that you *can* have Def<type> statements in the module. I
rarely see this, but you can put this at the top of your code module:

DefLng L-N
DefStr S

Then

Public Sub try2()
Dim sString
Dim nLong
MsgBox TypeName(sString) & vbNewLine & TypeName(nLong)
End Sub






In article <#(E-Mail Removed)>,
"Rick Rothstein" <(E-Mail Removed)> wrote:

> Unlike lots of other languages, in VB, every variable must be
> declared as to Type individually; otherwise, if it isn't, that
> variable is then declared as the default data Type which, in VB, is a
> Variant.

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      15th Jan 2009
I had completely forgotten that VB contained DefType statements... I
probably haven't used them since VB3 (the compiled version of VB from around
the early to mid 1990s); and I only used them then because I had some
programs in Fortran IV (which had a similar variable declaration statements)
from our DEC mini-computer which I needed to implement on our IBM personal
computers when we switched over to them. Now that I have been reminded of
their existence, and have given careful thought on them, I will not consider
ever using them in my programs and I will promptly try to forget about their
existence once again.<g>

--
Rick (MVP - Excel)


"JE McGimpsey" <(E-Mail Removed)> wrote in message
news:jemcgimpsey-(E-Mail Removed)...
> And to get completely esoteric, the exception to Rick's excellent
> explanation is that you *can* have Def<type> statements in the module. I
> rarely see this, but you can put this at the top of your code module:
>
> DefLng L-N
> DefStr S
>
> Then
>
> Public Sub try2()
> Dim sString
> Dim nLong
> MsgBox TypeName(sString) & vbNewLine & TypeName(nLong)
> End Sub
>
>
>
>
>
>
> In article <#(E-Mail Removed)>,
> "Rick Rothstein" <(E-Mail Removed)> wrote:
>
>> Unlike lots of other languages, in VB, every variable must be
>> declared as to Type individually; otherwise, if it isn't, that
>> variable is then declared as the default data Type which, in VB, is a
>> Variant.


 
Reply With Quote
 
Jay
Guest
Posts: n/a
 
      15th Jan 2009
Thanks JE McGimpsey.

Regards,
Jay

"JE McGimpsey" wrote:

> Perhaps:
>
> With ActiveCell
> If .Column >= 5 And .Column <= 64 Then _
> MsgBox Choose(.Column Mod 4 + 1, "Forecast", _
> "Beginning Inventory", "Ending Inventory", _
> "Production") & " Column"
> End With
>
> I'm assuming you didn't mean to duplicate the column numbers for
> clBInventory and clEInventory...
>
>
> In article <8C174C90-9DC0-4D71-BBA5-(E-Mail Removed)>,
> Jay <(E-Mail Removed)> wrote:
>
> > How do I trapped this error in my code?
> > kindly try using the procedure just paste it.
> >
> > -Input/ click on the activecell.column which is not indicated in those sets
> > of array?
> > -how do I optimized this code?
> >
> > Thank you in advance,
> > Jay
> >
> >
> > Sub Batchdist()
> >
> > Dim pMonths, dMonths As Long
> > Dim DomesticBlkTotcell, ForecastValue, clBInventory, clProduction,
> > clEInventory As Variant
> > Dim clBonna As Long
> > Dim clProdx, clBInvx, clFcstx, clEInvx As Variant
> > Dim blStatus As Boolean
> >
> > clprdnBonna = 17
> > clfcstBonna = 18
> > cleiBonna = 19
> >
> > '//List of all Production column number transferred to an array
> > clProduction = Array(7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51,
> > 55, 59, 63)
> > clBInventory = Array(5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49,
> > 53, 57, 61)
> > clForecast = Array(8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52,
> > 56, 60, 64)
> > clEInventory = Array(9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53,
> > 57, 61, 65)
> >
> > With ActiveCell
> > rw = .Row
> > cl = .Column
> > End With
> >
> > '//Checks if Activecell is on Production Column
> > clProdx = clProduction
> > clBInvx = clBInventory
> > clFcstx = clForecast
> > clEInvx = clEInventory
> >
> > For clProduction = 0 To 15
> > For clBInventory = 0 To 15
> > For clForecast = 0 To 15
> > For clEInventory = 0 To 15
> >
> > If ActiveCell.Column = clProdx(clProduction) Then
> > MsgBox "Production Column"
> > Exit Sub
> >
> > Else
> > On Error GoTo nxtLine:
> >
> > If ActiveCell.Column = clBInvx(clBInventory) Then
> > MsgBox "Beginning Inventory Column"
> > Exit Sub
> >
> > Else
> > On Error GoTo nxtLine1:
> >
> > If ActiveCell.Column = clFcstx(clForecast) Then
> > MsgBox "Forecast Column"
> > Exit Sub
> >
> > Else
> > On Error GoTo nxtLine2:
> >
> > If ActiveCell.Column = clEInvx(clEInventory) Then
> > MsgBox "Ending Inventory Column"
> > Exit Sub
> >
> > Else
> > On Error GoTo nxtLine3:
> >
> > End If
> > End If
> > End If
> > End If
> >
> > nxtLine3:
> > Next clEInventory
> > nxtLine2:
> > Next clForecast
> > nxtLine1:
> > Next clBInventory
> > nxtLine:
> > Next clProduction
> >
> > End Sub

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to optimize Code Gaffar Microsoft C# .NET 3 17th Aug 2005 02:03 PM
Error trapped only while stepping through the code - Not triggered when run Jeff Microsoft Excel Misc 7 7th Mar 2005 06:29 PM
Error trapped only while stepping through the code - Not triggered when run Jeff Microsoft Excel Misc 0 28th Feb 2005 06:26 PM
optimize code Jusan Microsoft VB .NET 2 20th Jul 2004 08:42 AM
Error not trapped - please help!!! Bura Tino Microsoft Excel Programming 8 18th Apr 2004 09:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 PM.