Need correction in code, little correction..

VBABEGINER

Well-known Member
Joined
Jun 15, 2011
Messages
1,284
Office Version
  1. 365
Platform
  1. Windows
Need correction in code, little correction..
Error - Object doesn't support this property or method..
Code:
With Worksheets("Sheet1")
    For i = 3 To RowCnt1
    If .HorizontalAlignment = xlLeft Then[COLOR=#ff0000]............Getting an error on this[/COLOR]
    .Cells(i, 6).Interior.Color = vbRed
    End If
    Next i
End With
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I think you are missing the Range reference on this line:
Code:
If .[COLOR=#ff0000]HorizontalAlignment[/COLOR] = xlLeft Then
What range are you trying to check the Horizontal Alignment on?

Note how you correctly have a range reference in the line below:
Code:
   [COLOR=#ff0000] .Cells(i, 6).[/COLOR]Interior.Color = vbRed
 
Last edited:
Upvote 0
Apologies. It's my bad. Forgot to use, cell reference. Thank you for this..
My code run's, but cell is not highlight in red color...can you tell me why is so?

I think you are missing the Range reference on this line:
Code:
If .[COLOR=#ff0000]HorizontalAlignment[/COLOR] = xlLeft Then
What range are you trying to check the Horizontal Alignment on?

Note how you correctly have a range reference in the line below:
Code:
   [COLOR=#ff0000] .Cells(i, 6).[/COLOR]Interior.Color = vbRed
 
Upvote 0
It works fine for me.
Note that the HorizontalAlignment is checking the formatting setting (not really looking at the data).
If you enter text into an unformatted cell, the text is left-justified by default (numbers are right-justified by default), but that does not mean the horizontal setting is set to "Left". By default, it is set to "General".
If you want to check if it is either "Left" or "General", the code would look like this:
Code:
    For i = 3 To RowCnt1
        With Worksheets("Sheet1").Cells(i, 6)
            If (.HorizontalAlignment = xlLeft) Or (.HorizontalAlignment = xlGeneral) Then
                .Interior.Color = vbRed
            End If
        End With
    Next i
If that doesn't do what you want, you are going to explain to us exactly what it is that you want to happen.
 
Upvote 0
Hi Joe, Really thanks for this explanation..!! Im really not so clever to understand this "pointers". The code makes all the dates in "red".

Actually what happens is, in my one of the date column, one date is coming in wrong format..
Date is -
Code:
20/01/13

I have just cross check with Year formula and I got surprised answer that 2020 in answer. Which is in-correct. Year should come 2013.

Therefore, Im trying code like,
Code:
For i = 3 To RowCnt1
If Len(Right(Cells(i, 6), 4)) < 4 Then
Cells(i, 6).Interior.Color = vbRed
Else
End If
Next i
...but not working...I really cant catch the logic..

It works fine for me.
Note that the HorizontalAlignment is checking the formatting setting (not really looking at the data).
If you enter text into an unformatted cell, the text is left-justified by default (numbers are right-justified by default), but that does not mean the horizontal setting is set to "Left". By default, it is set to "General".
If you want to check if it is either "Left" or "General", the code would look like this:
Code:
    For i = 3 To RowCnt1
        With Worksheets("Sheet1").Cells(i, 6)
            If (.HorizontalAlignment = xlLeft) Or (.HorizontalAlignment = xlGeneral) Then
                .Interior.Color = vbRed
            End If
        End With
    Next i
If that doesn't do what you want, you are going to explain to us exactly what it is that you want to happen.
 
Upvote 0
The main thing is,
this date is wrong coming in my answer. And im trying to highlight this cell in red. That this wrong capture..so user can understand
 
Upvote 0
Do you need VBA code?
Because this can be done pretty easily with Conditional Formatting.
It sounds to me like you are expecting date values, and some are coming over as text, because they are not in a valid date format (hence, the left-justification because they are text).
Better to just use the ISTEXT function.

So in Conditional Formatting, if checking cell F3, the Conditional Formatting formula would just look like:
Code:
=ISTEXT(F6)
and then choose your red coloring.

If you need this in VBA code, you can turn on the Macro Recorder and record yourself applying the Conditional Formatting.
 
Upvote 0
Hi, can you please give me this in vba code..Coz my entire other are in vba code..

Do you need VBA code?
Because this can be done pretty easily with Conditional Formatting.
It sounds to me like you are expecting date values, and some are coming over as text, because they are not in a valid date format (hence, the left-justification because they are text).
Better to just use the ISTEXT function.

So in Conditional Formatting, if checking cell F3, the Conditional Formatting formula would just look like:
Code:
=ISTEXT(F6)
and then choose your red coloring.

If you need this in VBA code, you can turn on the Macro Recorder and record yourself applying the Conditional Formatting.
 
Upvote 0
Hi, can you please give me this in vba code..Coz my entire other are in vba code..
Did you try what I recommended here, regarding VBA code?
If you need this in VBA code, you can turn on the Macro Recorder and record yourself applying the Conditional Formatting.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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