I have a worksheet using Excel 2002 that will automatically insert a text string from the clipboard that works but the problem is that I need it to insert the text on the line that I have highlighted and it will only do that on the first line and then it wants to re-write the line above it instead of inserting the text on the line where I want it (may be the second or third line down with a blank line in-between). Here is the Activate & Insert code so maybe someone can tell me what is wrong?
Thanks, Gene
Here is the type of string that will be on the clipboard -- A17; Bill Smith; Mary Smith; Phoenix; AZ; Bigfoot; 21FW; 2003; 8 ; 15; 19; justme@mysite.com ; Joey
VB:
Private Declare Function IsClipboardFormatAvailable _
Lib "User32.dll" _
(ByVal wFormat As Integer) As Long
Private Declare Function OpenClipboard _
Lib "User32.dll" _
(ByVal hwnd As Long) As Long
Private Declare Function GetClipboardData _
Lib "User32.dll" _
(ByVal wFormat As Integer) As Long
Private Declare Function CloseClipboard _
Lib "User32.dll" () As Long
Private Declare Function lstrlen _
Lib "kernel32.dll" _
Alias "lstrlenA" _
(ByVal lpString As String) As Long
Private Declare Function GlobalSize _
Lib "kernel32.dll" _
(ByVal hMem As Long) As Long
Private Declare Function GlobalLock _
Lib "kernel32.dll" _
(ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock _
Lib "kernel32" _
(ByVal hMem As Long) As Long
Private Declare Sub MoveMemory _
Lib "kernel32" Alias "RtlMoveMemory" _
(ByVal strDest As Any, _
ByVal lpSource As Any, _
ByVal Length As Long)
Function GetClipboardText() As String
Dim DataSize As Long
Dim hClip As Long
Dim hData As Long
Dim pData As Long
Dim Ret As Long
Dim strText As String
Const CF_TEXT As Long = 1&
Ret = OpenClipboard(0&)
If Ret = 0 Then
MsgBox "Clipboard is in use."
Exit Function
End If
Ret = IsClipboardFormatAvailable(CF_TEXT)
If Ret Then
hData = GetClipboardData(CF_TEXT)
If hData Then
DataSize = GlobalSize(hData)
strText = Space$(DataSize)
pData = GlobalLock(hData)
Call MoveMemory(strText, pData, DataSize)
strText = Left$(strText, lstrlen(strText))
Call GlobalUnlock(hData)
End If
Else
MsgBox "There is No Text on the Clipboard."
End If
Ret = CloseClipboard
GetClipboardText = strText
End Function
VB:
Sub InsertText()
Dim Rng As Range
Dim RngEnd As Range
Dim Text As String
Set Rng = Range("A2:M2")
Set RngEnd = Cells(Rows.Count, Rng.Row).End(xlUp)
Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Range(Rng, RngEnd).Offset(1, 0))
Text = GetClipboardText
If Text <> "" Then Rng.Rows(1).Cells.Value = Split(Text, ";")
End Sub
Sub ActivateInsertKey()
Application.OnKey "{INSERT}", "InsertText"
End Sub
Bookmarks