Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Problem with new programme

Hi All,
I'm new to VBA and am having trouble trying to get excel 2007 to work
out final headings for a boat that hits a cross current, but can't get
the VBA to work.

I input 2 pieces of data, the Ship heading and the Current heading.
From the Ship data one of 4 quadrants is chosen. This works ok.
Then, for each quadrant and depending on the size of the current
heading relative to the ship heading, one of 4 different actions has
to be taken.
eg 1) Ship=current then final = (180-ship)+current
2) Current ship and current <=180 then final = (180-current)
+ship
3) Current 180 and Current <= (180+ship) then final = ship-
(current-180)
4) Current (180+ship) and Current <=360 then final = (current
-180)-ship

Unfortunately, the relative parameters change for each of the 4
quadrants so each will have to be written separately.

But I can't get this second bit to work: I've tried 'if' and 'case' to
no avail. I only get 1) to work or have to cycle through 1), 2) etc
and choose the correct answer.
The answer from this is then fed into the final equation to work out
the course correction. This bit works fine as a normal Excel equation.

So, any help and suggestions you can offer would be gratefully
received.

Tony

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Problem with new programme

What have you tried so far? Please post your code and we can update that.
--
HTH,
Barb Reinhardt




" wrote:

Hi All,
I'm new to VBA and am having trouble trying to get excel 2007 to work
out final headings for a boat that hits a cross current, but can't get
the VBA to work.

I input 2 pieces of data, the Ship heading and the Current heading.
From the Ship data one of 4 quadrants is chosen. This works ok.
Then, for each quadrant and depending on the size of the current
heading relative to the ship heading, one of 4 different actions has
to be taken.
eg 1) Ship=current then final = (180-ship)+current
2) Current ship and current <=180 then final = (180-current)
+ship
3) Current 180 and Current <= (180+ship) then final = ship-
(current-180)
4) Current (180+ship) and Current <=360 then final = (current
-180)-ship

Unfortunately, the relative parameters change for each of the 4
quadrants so each will have to be written separately.

But I can't get this second bit to work: I've tried 'if' and 'case' to
no avail. I only get 1) to work or have to cycle through 1), 2) etc
and choose the correct answer.
The answer from this is then fed into the final equation to work out
the course correction. This bit works fine as a normal Excel equation.

So, any help and suggestions you can offer would be gratefully
received.

Tony


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Problem with new programme

On 10 Sep, 13:01, Barb Reinhardt
wrote:
What have you tried so far? *Please post your code and we can update that.
--
HTH,
Barb Reinhardt

" wrote:
Hi All,
I'm new to VBA and am having trouble trying to get excel 2007 to work
out final headings for a boat that hits a cross current, but can't get
the VBA to work.


I input 2 pieces of data, the Ship heading and the Current heading.
From the Ship data one of 4 quadrants is chosen. This works ok.
Then, for each quadrant and depending on the size of the current
heading relative to the ship heading, one of 4 different actions has
to be taken.
eg *1) Ship=current then final = (180-ship)+current
* * *2) Current ship and current <=180 then final = (180-current)
+ship
* * *3) Current 180 and Current <= (180+ship) then final = ship-
(current-180)
* * *4) Current (180+ship) and Current <=360 *then final = (current
-180)-ship


Unfortunately, the relative parameters change for each of the 4
quadrants so each will have to be written separately.


But I can't get this second bit to work: I've tried 'if' and 'case' to
no avail. I only get 1) to work or have to cycle through 1), 2) etc
and choose the correct answer.
The answer from this is then fed into the final equation to work out
the course correction. This bit works fine as a normal Excel equation.


So, any help and suggestions you can offer *would be gratefully
received.


Tony


Here's what I've got for the first quadrant. It's probably very
inelegant; I did say I'm new to VBA.

For this to run the same value of Ship has to be used all the time and
less than 89. I've left out the Select Case for Quadrants 2,3,4 as
they do nothing.

Sub datacalc()

Dim Ship, Current, output, Number

Ship = InputBox("Enter Ship Heading: ")
Current = InputBox("Enter Current Heading: ")

Select Case Ship

Case 0 To 89: GoTo Quadrant1:

End Select


Quadrant1:

If Ship = Current Then
GoTo abc:

ElseIf Ship <= Current And Current <= 180 Then
GoTo def:

ElseIf Current 180 And Current <= (Ship + 180) Then
GoTo ghi:

ElseIf Current (Ship + 180) And current <= 360 Then
GoTo jkl:
End If


abc: output1 = (180 - Ship) + Current
MsgBox "output1= " & output1

def: output2 = (180 - Current) + Ship
MsgBox "output2= " & output2

ghi: output3 = Ship - (Current - 180)
MsgBox "output3= " & output3

jkl: output4 = (Current - 180) - Ship
MsgBox "output4= " & output4

End

End Sub

When I run it I get 4 different answers no matter what value of
current I put in for the same value of Ship.

Thanks for your help.

Tony
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Problem with new programme

You're making it harder than it needs to be. Try this

Sub datacalc()

Dim Ship, Current, output, Number

Ship = InputBox("Enter Ship Heading: ")
Current = InputBox("Enter Current Heading: ")

If Ship = Current Then
output = (180 - Ship) + Current
ElseIf Ship <= Current And Current <= 180 Then
output = (180 - Current) + Ship
ElseIf Current 180 And Current <= (Ship + 180) Then
output = Ship - (Current - 180)
ElseIf Current (Ship + 180) And Current <= 360 Then
output = (Current - 180) - Ship
End If

MsgBox ("Output = " & output)

End Sub
--
HTH,
Barb Reinhardt




" wrote:

On 10 Sep, 13:01, Barb Reinhardt
wrote:
What have you tried so far? Please post your code and we can update that.
--
HTH,
Barb Reinhardt

" wrote:
Hi All,
I'm new to VBA and am having trouble trying to get excel 2007 to work
out final headings for a boat that hits a cross current, but can't get
the VBA to work.


I input 2 pieces of data, the Ship heading and the Current heading.
From the Ship data one of 4 quadrants is chosen. This works ok.
Then, for each quadrant and depending on the size of the current
heading relative to the ship heading, one of 4 different actions has
to be taken.
eg 1) Ship=current then final = (180-ship)+current
2) Current ship and current <=180 then final = (180-current)
+ship
3) Current 180 and Current <= (180+ship) then final = ship-
(current-180)
4) Current (180+ship) and Current <=360 then final = (current
-180)-ship


Unfortunately, the relative parameters change for each of the 4
quadrants so each will have to be written separately.


But I can't get this second bit to work: I've tried 'if' and 'case' to
no avail. I only get 1) to work or have to cycle through 1), 2) etc
and choose the correct answer.
The answer from this is then fed into the final equation to work out
the course correction. This bit works fine as a normal Excel equation.


So, any help and suggestions you can offer would be gratefully
received.


Tony


Here's what I've got for the first quadrant. It's probably very
inelegant; I did say I'm new to VBA.

For this to run the same value of Ship has to be used all the time and
less than 89. I've left out the Select Case for Quadrants 2,3,4 as
they do nothing.

Sub datacalc()

Dim Ship, Current, output, Number

Ship = InputBox("Enter Ship Heading: ")
Current = InputBox("Enter Current Heading: ")

Select Case Ship

Case 0 To 89: GoTo Quadrant1:

End Select


Quadrant1:

If Ship = Current Then
GoTo abc:

ElseIf Ship <= Current And Current <= 180 Then
GoTo def:

ElseIf Current 180 And Current <= (Ship + 180) Then
GoTo ghi:

ElseIf Current (Ship + 180) And current <= 360 Then
GoTo jkl:
End If


abc: output1 = (180 - Ship) + Current
MsgBox "output1= " & output1

def: output2 = (180 - Current) + Ship
MsgBox "output2= " & output2

ghi: output3 = Ship - (Current - 180)
MsgBox "output3= " & output3

jkl: output4 = (Current - 180) - Ship
MsgBox "output4= " & output4

End

End Sub

When I run it I get 4 different answers no matter what value of
current I put in for the same value of Ship.

Thanks for your help.

Tony

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Problem with new programme

On 10 Sep, 19:40, Barb Reinhardt
wrote:
You're making it harder than it needs to be. *Try this

Sub datacalc()

Dim Ship, Current, output, Number

Ship = InputBox("Enter Ship Heading: ")
Current = InputBox("Enter Current Heading: ")

If Ship = Current Then
* * output = (180 - Ship) + Current
ElseIf Ship <= Current And Current <= 180 Then
* * output = (180 - Current) + Ship
ElseIf Current 180 And Current <= (Ship + 180) Then
* * output = Ship - (Current - 180)
ElseIf Current (Ship + 180) And Current <= 360 Then
* * output = (Current - 180) - Ship
End If

MsgBox ("Output = " & output)

End Sub
--
HTH,
Barb Reinhardt

" wrote:
On 10 Sep, 13:01, Barb Reinhardt
wrote:
What have you tried so far? *Please post your code and we can update that.
--
HTH,
Barb Reinhardt


" wrote:
Hi All,
I'm new to VBA and am having trouble trying to get excel 2007 to work
out final headings for a boat that hits a cross current, but can't get
the VBA to work.


I input 2 pieces of data, the Ship heading and the Current heading.
From the Ship data one of 4 quadrants is chosen. This works ok.
Then, for each quadrant and depending on the size of the current
heading relative to the ship heading, one of 4 different actions has
to be taken.
eg *1) Ship=current then final = (180-ship)+current
* * *2) Current ship and current <=180 then final = (180-current)
+ship
* * *3) Current 180 and Current <= (180+ship) then final = ship-
(current-180)
* * *4) Current (180+ship) and Current <=360 *then final = (current
-180)-ship


Unfortunately, the relative parameters change for each of the 4
quadrants so each will have to be written separately.


But I can't get this second bit to work: I've tried 'if' and 'case' to
no avail. I only get 1) to work or have to cycle through 1), 2) etc
and choose the correct answer.
The answer from this is then fed into the final equation to work out
the course correction. This bit works fine as a normal Excel equation.


So, any help and suggestions you can offer *would be gratefully
received.


Tony


Here's what I've got for the first quadrant. It's probably very
inelegant; I did say I'm new to VBA.


For this to run the same value of Ship has to be used all the time and
less than 89. I've left out the Select Case for Quadrants 2,3,4 as
they do nothing.


*Sub datacalc()


Dim Ship, Current, output, Number


Ship = InputBox("Enter Ship Heading: ")
Current = InputBox("Enter Current Heading: ")


* * * * Select Case Ship


* * * * Case 0 To 89: GoTo Quadrant1:


* * * * End Select


Quadrant1:


* * * * If Ship = Current Then
* * * * *GoTo abc:


* * * * *ElseIf Ship <= Current And Current <= 180 Then
* * * * *GoTo def:


* * * * * ElseIf Current 180 And Current <= (Ship + 180) Then
* * * * *GoTo ghi:


* * * * *ElseIf Current (Ship + 180) And current <= 360 Then
* * * * *GoTo jkl:
End If


abc: *output1 = (180 - Ship) + Current
* * * * * * * * MsgBox "output1= " & output1


def: output2 = (180 - Current) + Ship
* * * * * * * * MsgBox "output2= " & output2


ghi: output3 = Ship - (Current - 180)
* * * * * * * * MsgBox "output3= " & output3


jkl: output4 = (Current - 180) - Ship
* * * * * * * *MsgBox "output4= " & output4


End


End Sub


*When I run it I get 4 different answers no matter what value of
current I put in for the same value of Ship.


Thanks for your help.


Tony


Hi Barb,

Thanks for your suggestion. I've tried what you suggest and now I only
get the first part to work. The only answer is to output = (180 -
Ship) + Current no matter what
value of current I put in. It's as though the VBA is ignoring the
ElseIf commands. Could my Excel VBA be b-----rd?

Regards

Tony
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
programme run help paul[_17_] Excel Programming 2 September 11th 07 11:50 AM
running a programme paul[_17_] Excel Programming 9 September 2nd 07 05:20 PM
PROGRAMME HELP paul[_17_] Excel Programming 2 July 30th 07 11:46 AM
Does excel VBA programme has a size limit? Or some other problems caused this problem? [email protected] Excel Programming 3 June 20th 06 05:06 PM
excel vba programme sarasa[_6_] Excel Programming 3 June 14th 04 07:27 AM


All times are GMT +1. The time now is 03:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"