Setting the cursor position in cell text using VBA

gallan

Board Regular
Joined
May 27, 2007
Messages
79
I would like to be able to set the position of the cursor in the text in a particular cell using VBA.

The situation is that I am creating a lab report writer. Using then worksheet_selection_event I can determine where on the page the user clicked (by making the columns quite narrow), and bring the text for that row (which resides in the 1st cell of the row) to the formula bar for editing. I would like to be able to move the cursor along in the formula bar to the correct place in the text (or close to it) based on where on the screen they clicked in order to edit the line.

Can anyone tell me the code for doing this?

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Something like this :-
Code:
'===================================================================================
'- MOVE CURSOR IN THE FORMULA BAR AS REQUIRED
'- Brian Baulsom September 2007
'===================================================================================
Sub test()
    Dim c As Integer
    c = 3
    '------------------------------------------------------------------------------
    Range("A1").Select      ' select cell
    SendKeys "{F2}", True   ' activate formula bar. cursor is to right of contents.
    DoEvents
    '------------------------------------------------------------------------------
    '- move cursor 3 characters to the left in the formula bar
    '- or in the cell if Tools/Options/Edit .. "Edit directly in cell" is checked
    SendKeys "{LEFT " & CStr(c) & "}", True         ' ="{LEFT 3}"
    DoEvents
End Sub
'-----------------------------------------------------------------------------------
 
Upvote 0
I am using Excel 2016. BrianB's code doesn't work for me until I merge all the keystroke commands into single "DoEvents" session.

Modified Code:
'===================================================================================
'- MOVE CURSOR IN THE FORMULA BAR AS REQUIRED
'===================================================================================
Sub test()
Dim c As Integer
c = 3
'------------------------------------------------------------------------------
Range("A1").Select ' select cell
SendKeys "{F2}", True ' activate formula bar. cursor is to right of contents.
SendKeys "{LEFT " & CStr(c) & "}", True ' ="{LEFT 3}"
DoEvents

End Sub
'-----------------------------------------------------------------------------------
 
Upvote 0

Forum statistics

Threads
1,221,905
Messages
6,162,772
Members
451,786
Latest member
CALEB23

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