Simple (I think) VBA question

paliman

Active Member
Joined
Jul 7, 2002
Messages
255
Hi everybody.
I recorded a macro with the macro recorder and I need to make a little change to it. And as I know almost nothing about VBA I hope that someone here can give me a clue.

At some point I have this line:

Selection.End(xlToLeft).Select

I need this line to select the first cell of the row that I'm in, but there may be blank cells in the road, so this instruccion won't work. How can I cange it to fix my needs?

Many thanks to all of you

Pali.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi paliman,

If you really want the first cell, not the first cell containing data, then the code is just:

Cells(Selection.Row,1).Select

However, if you really want the first cell containing data you need to replace it with the following:

If IsEmpty(Cells(Selection.Row,1)) Then
Cells(Selection.Row,1).End(xlToRight).Select
Else
Cells(Selection.Row,1).Select
EndIf

The key here is to do the End operation from the left end of the row to the right. The If..Then..Else is needed because of the possibility that the first cell in the row isn't empty and doing the End(xlToRight) would therefore take you to the end of the data in the row rather than the beginning.
 
Upvote 0
Thanks very much for your help. In this case, column A is always filled, so I need to select the cell A-whatever.

Thanks again.
 
Upvote 0
You can do like this. In the IF statement you can do what ever you want in the if statement.

Selection.End(xlToLeft).Select
If ActiveCell.Value = "" Then
MsgBox "First Cell is Blank"
End If
 
Upvote 0

Forum statistics

Threads
1,203,606
Messages
6,056,280
Members
444,854
Latest member
goethe168

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