Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Syntax error in code

I'm using this code in an existing macro to keep only rows with "F800" in
column J. All other rows are being deleted. The spreadsheet varies in
length every day so there is no end specified. The syntax error I'm
receiving in my code is on this line:

If StrComp(Cells(RowNdx, "J"), "F800", vbTextCompare) < 0
Then



Thanks for your help.

With Selection
Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If StrComp(Cells(RowNdx, "J"), "F800", vbTextCompare) < 0
Then
Rows(RowNdx).Delete
End If
Next RowNdx
End With

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Syntax error in code

First, you use "with selection", but don't use anything that belongs to that
selection--I'm not sure what you wanted to do with that line.

Second, "Then" belongs on the same logical line as the "If" statement.

Option Explicit
Sub testme01()
Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If StrComp(Cells(RowNdx, "J"), "F800", vbTextCompare) < 0 Then
Rows(RowNdx).Delete
End If
Next RowNdx
End Sub

If the "if" portion gets too long, you can use a line continuation character to
continue that "logical" line to the next "physical" line:

Option Explicit
Sub testme01()
Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If StrComp(Cells(RowNdx, "J"), "F800", vbTextCompare) < 0 _
Then
Rows(RowNdx).Delete
End If
Next RowNdx
End Sub

The space character followed by the underscore is that continuation character.

=======
If you're coming from an environment where you used to line up the If/Then/else,
you will get used to VBA's syntax.

if a = b
then do something
do something else
else
do something2
do something3.

becomes

if a = b then
do something
do something else
else
do something2
do something3
end if

It's just a matter of time <bg.
JOUIOUI wrote:

I'm using this code in an existing macro to keep only rows with "F800" in
column J. All other rows are being deleted. The spreadsheet varies in
length every day so there is no end specified. The syntax error I'm
receiving in my code is on this line:

If StrComp(Cells(RowNdx, "J"), "F800", vbTextCompare) < 0
Then

Thanks for your help.

With Selection
Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If StrComp(Cells(RowNdx, "J"), "F800", vbTextCompare) < 0
Then
Rows(RowNdx).Delete
End If
Next RowNdx
End With


--

Dave Peterson
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
runtime error: syntax error or access violation oucsester[_2_] Excel Programming 1 May 3rd 06 05:51 PM
runtime error: syntax error or access violation oucsester Excel Programming 0 May 3rd 06 02:22 PM
What's the syntax error with this SendKeys code? Phil1982 Excel Programming 1 March 19th 06 06:52 PM
Syntax Error in VLOOKUP Code Ken Excel Programming 3 October 20th 04 05:50 PM
Syntax Error Runtime Error '424' Object Required sjenks183 Excel Programming 1 January 23rd 04 09:25 AM


All times are GMT +1. The time now is 11:26 PM.

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

About Us

"It's about Microsoft Excel"