If statement to sendspecialkeys [field+] or [field-]

ianjwerner

New Member
Joined
Sep 20, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I need help with how to write an if statement in VBA so that if the value in a cell is positive it will Call SendSpecialKeys("[field+]") or if the value is a cell is negative it will Call SendSpecialKeys("[field-]")
 

Attachments

  • modual 1.PNG
    modual 1.PNG
    69.6 KB · Views: 3
  • modual 2.PNG
    modual 2.PNG
    76.4 KB · Views: 2

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
VBA Code:
If range("A1") > 0 And range("A1") <> 0 Then
   SendSpecialKeys("[field+]")
ElseIf range("A1") < 0 And range("A1") <> 0
   SendSpecialKeys("[field-]")
End If
>0 is +ve; <0 is negative but 0 is neither so you really have 3 conditions to worry about. What I wrote will do nothing if cell is 0 but I'm thinking it could be simpler:
VBA Code:
If range("A1") > 0 Then
   SendSpecialKeys("[field+]")
ElseIf range("A1") < 0
   SendSpecialKeys("[field-]")
End If
This will also do nothing if cell value is 0.

Perhaps you will need to worry about no value in the cell as well.
Provide your own range/cell reference instead of A1. Could also be done with a Select Case block.
BTW, you rarely need to use Call statement.
 
Upvote 0
VBA Code:
If range("A1") > 0 And range("A1") <> 0 Then
   SendSpecialKeys("[field+]")
ElseIf range("A1") < 0 And range("A1") <> 0
   SendSpecialKeys("[field-]")
End If
>0 is +ve; <0 is negative but 0 is neither so you really have 3 conditions to worry about. What I wrote will do nothing if cell is 0 but I'm thinking it could be simpler:
VBA Code:
If range("A1") > 0 Then
   SendSpecialKeys("[field+]")
ElseIf range("A1") < 0
   SendSpecialKeys("[field-]")
End If
This will also do nothing if cell value is 0.

Perhaps you will need to worry about no value in the cell as well.
Provide your own range/cell reference instead of A1. Could also be done with a Select Case block.
BTW, you rarely need to use Call statement.
hello!
when trying to input the VBA code it produces an error "Compile error: Expected: Then or GoTo
 

Attachments

  • then error.PNG
    then error.PNG
    99.8 KB · Views: 3
Upvote 0
That is not what I posted. ElseIf and Else If are not the same. "Then" is supposed to be optional with ElseIf, so first try ElseIf as I posted.
 
Upvote 0
I receive a syntax error when I paste as is.
 

Attachments

  • syntax.PNG
    syntax.PNG
    111.1 KB · Views: 2
Upvote 0
You've got you code in a standard module. As such, it stands alone and has no idea what "Range" is all about. I wrote it as if your code was on a sheet but it's not, so you have to be specific about the range. I think you'll need to qualify it as in Sheets("yourSheetNameHere").Range("A1") but then, I don't see why it didn't error on the previous If line (or did it?). Documentation about ElseIf says "Then" is optional but you could try adding Then to that line.

I have to go out for a few hours so won't be able to reply until later.
 
Upvote 0
Solution
It worked when I put the extra "Then" Thank you very much for your help! :)
 
Upvote 0
Sorry about that. I seldom use ElseIf, preferring Select Case instead. I must have mis-read the documentation before posting, because I thought it said Then was optional but I can't find that now. Glad I could help and thanks for the recognition.
 
Upvote 0
Then is not optional with ElseIf. From the help file:

1706032156782.png


You don't need a Then with Else but you do with ElseIf
 
Upvote 0

Forum statistics

Threads
1,215,127
Messages
6,123,203
Members
449,090
Latest member
bes000

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
Back
Top