ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Else without if? (https://www.excelbanter.com/excel-programming/333041-else-without-if.html)

davegb

Else without if?
 
More code, this time getting an "Else without if" error at the else
line!

Range("a1").Select
Set TtlRng = Range(Selection, Selection.End(xlToRight))
For Each rFoundHd In TtlRng.Cells
If Right(rFoundHd.Value, 2) = "ID" Then
ActiveSheet.Range(rFoundHd,
rFoundHd.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlRight
.NumberFormat = "@"
Else
Next rFoundHd
End If

Sure looks like an "If" to me, 6 lines above the else!
rFoundHd is declared as a range, as is TtlRng.
The code is supposed to look in row 1 for any header ending in "ID".
When it finds one, it should format that column to text and right align
it. Then move to the next cell in row 1.
Any suggestions?
Thanks in advance.


Charlie

Else without if?
 
Need an End With statement after .NumberFormat line

"davegb" wrote:

More code, this time getting an "Else without if" error at the else
line!

Range("a1").Select
Set TtlRng = Range(Selection, Selection.End(xlToRight))
For Each rFoundHd In TtlRng.Cells
If Right(rFoundHd.Value, 2) = "ID" Then
ActiveSheet.Range(rFoundHd,
rFoundHd.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlRight
.NumberFormat = "@"
Else
Next rFoundHd
End If

Sure looks like an "If" to me, 6 lines above the else!
rFoundHd is declared as a range, as is TtlRng.
The code is supposed to look in row 1 for any header ending in "ID".
When it finds one, it should format that column to text and right align
it. Then move to the next cell in row 1.
Any suggestions?
Thanks in advance.



davegb

Else without if?
 


Charlie wrote:
Need an End With statement after .NumberFormat line

Thanks, Charlie, but I'm still getting the same error.


Charlie

Else without if?
 
It also looks like you have a For-Next structure error. Try:

For Each rFoundHd In TtlRng.Cells
If Right(rFoundHd.Value, 2) = "ID" Then
ActiveSheet.Range(rFoundHd, rFoundHd.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlRight
.NumberFormat = "@"
End With
Exit For
End If
Next rFoundHd


"davegb" wrote:

More code, this time getting an "Else without if" error at the else
line!

Range("a1").Select
Set TtlRng = Range(Selection, Selection.End(xlToRight))
For Each rFoundHd In TtlRng.Cells
If Right(rFoundHd.Value, 2) = "ID" Then
ActiveSheet.Range(rFoundHd,
rFoundHd.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlRight
.NumberFormat = "@"
Else
Next rFoundHd
End If

Sure looks like an "If" to me, 6 lines above the else!
rFoundHd is declared as a range, as is TtlRng.
The code is supposed to look in row 1 for any header ending in "ID".
When it finds one, it should format that column to text and right align
it. Then move to the next cell in row 1.
Any suggestions?
Thanks in advance.



davegb

Else without if?
 
My mistake, Charlie, I put in an "end" rather than an "end with". It's
working now!

Charlie wrote:
Need an End With statement after .NumberFormat line

"davegb" wrote:

More code, this time getting an "Else without if" error at the else
line!

Range("a1").Select
Set TtlRng = Range(Selection, Selection.End(xlToRight))
For Each rFoundHd In TtlRng.Cells
If Right(rFoundHd.Value, 2) = "ID" Then
ActiveSheet.Range(rFoundHd,
rFoundHd.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlRight
.NumberFormat = "@"
Else
Next rFoundHd
End If

Sure looks like an "If" to me, 6 lines above the else!
rFoundHd is declared as a range, as is TtlRng.
The code is supposed to look in row 1 for any header ending in "ID".
When it finds one, it should format that column to text and right align
it. Then move to the next cell in row 1.
Any suggestions?
Thanks in advance.





All times are GMT +1. The time now is 12:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com