VBA: Finding from clipboard

AirMaximus88

New Member
Joined
Mar 13, 2014
Messages
16
Hi guys,

Another problem from me, but this time it's VBA based. I'm trying to find data from my clipboard in another spreadsheet. At the moment it's a laborious process of copying, changing windows, findings, etc.

This is the code I'm trying out, but i dont understand why it's producing an error [Runtime error 91: Object vairable or With block variable not set].


Sub FindInP7()
Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText

Windows("P7 Data.xlsx").Activate


Cells.Find(What:=strClip, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End Sub




I also tried to change ActiveCell to a range of cells, but that didnt work [After:=Range("A1:D1"). If anyone could help me with that and also deciphering what error my noob-like coding has produced, I would appreciate it!

Max
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Max, If you're putting the search string into the clipboard by copying it from a cell, the resulting value of strClip will have two extra characters added to the end for Carriage Return and Line Feed (vbCrLf). In that case your code returns an error because it can't find a match to that string with the extra characters.

Try making the modifications below which will remove the vbCrLf if it is present at the end of the search string.

Code:
Sub FindInP7()
 Dim MyData As DataObject
 Dim strClip As String
 Dim rngFound As Range
 
 Set MyData = New DataObject
 MyData.GetFromClipboard
 strClip = MyData.GetText
 
 Windows("P7 Data.xlsx").Activate
 
 If Right(strClip, 2) = vbCrLf Then
   strClip = Left(strClip, Len(strClip) - 2)
 End If
 
 Set rngFound = Cells.Find(What:=strClip, LookIn:=xlFormulas, _
   LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
 If rngFound Is Nothing Then
   MsgBox strClip & " not found."
 Else
   rngFound.Select
 End If
End Sub

One other observation...your code assumes that when activating P7 Data.xlsx, the search should take place on the ActiveSheet of that workbook. If it's possible that the desired search sheet might not be active, your code should explicitly activate that sheet.
 
Last edited:
Upvote 0
Hi Max, If you're putting the search string into the clipboard by copying it from a cell, the resulting value of strClip will have two extra characters added to the end for Carriage Return and Line Feed (vbCrLf). In that case your code returns an error because it can't find a match to that string with the extra characters.

Try making the modifications below which will remove the vbCrLf if it is present at the end of the search string.

Code:
Sub FindInP7()
 Dim MyData As DataObject
 Dim strClip As String
 Dim rngFound As Range
 
 Set MyData = New DataObject
 MyData.GetFromClipboard
 strClip = MyData.GetText
 
 Windows("P7 Data.xlsx").Activate
 
 If Right(strClip, 2) = vbCrLf Then
   strClip = Left(strClip, Len(strClip) - 2)
 End If
 
 Set rngFound = Cells.Find(What:=strClip, LookIn:=xlFormulas, _
   LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
 If rngFound Is Nothing Then
   MsgBox strClip & " not found."
 Else
   rngFound.Select
 End If
End Sub

One other observation...your code assumes that when activating P7 Data.xlsx, the search should take place on the ActiveSheet of that workbook. If it's possible that the desired search sheet might not be active, your code should explicitly activate that sheet.

Thanks a lot! Worked a treat. I couldn't understand why strClip wouldn't work and couldn't find the answer anywhere.

+2 VBA points to you, sir.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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