Generic Code To Highlight And Copy Any Selection Of Text On Any Textbox On Any Userform In An Application

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
917
Office Version
  1. 365
Platform
  1. Windows
The question is – can this be done generically with a few lines of code (few =?)? - that is, Excel has to know which textbox of which userform in an application with say, 10 Userforms containing several textboxes on each userform? It's like saying the active textbox of the active userform

I tried to be as short but as complete as possible in this description. Here it is:

Each textbox gets its text and displays it from a Listbox Rowsource property of each userform of a specific sheet the results of a FIND method copies to.
With the code I have below, I can highlight, select, copy and paste any text selected to any specific sheet but it only correctly performs the operation for one specific textbox on one specific userform at a time.

Code:
Private Sub cmdEDITVERSE_Click()
Sheets("VSEDITS").Range("A1:B1").ClearContents
Dim s As String, vs As Worksheet
s = Me.TextBox8.Value 'CHG SHT NAME. USE s VAR – >>>THIS CHANGES WITH EACH USERFORM
With BIBLETEXTWINDOW.NASB - – >>> REFERENCES SHEET NAME OF USERFORM NAME.  THIS CHANGES W EACH USERFORM
.SelStart = 0
.SelLength = Len(.Text)
.Copy
End With
cmdEDITVERSE.SetFocus
Set vs = Sheets("VSEDITS")---> just copies to a temporary "holding") sheet
vs.Cells(1, 1).Value = s '
vs.paste Destination:=vs.Range("B1")
Sheets("VSEDITS").Range("A2:B100").ClearContents
Dim X As String, c As Range, ws, rs As Worksheet
Set ws = Worksheets("Sheet2") - MAIN DATA SHEET WHERE CHANGES ARE STORED AND SAVED
With ws.Range("B:B")
Set c = .Find(What:=TextBox8.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 'CHG FIND WHAT VALUE
X = c.Offset(, 3).Value 'offset number from col B i1=KJV, 2=NIV, 3=NASB, 4=RSV - each col is a version down to 31103 rows
If Not c Is Nothing Then
c.Offset(, 3) = Sheets("VSEDITS").Range("B1:B1")
Else
'MsgBox "Can't find " & TextBox8.Value 'CHG TEXTBOX NAME
End If
End With
Set rs = Worksheets("RESULT") 'THIS CHANGES TO A DIFFERENT SHEET FOR EACH LISTBOX DATA COPIED FIND RESULTS TO
With rs.Range("B:B") - THIS COL CHANGES
Set c = .Find(What:=TextBox8.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 'CHG FIND WHAT VALUE
If Not c Is Nothing Then
c.Offset(, 3) = Sheets("VSEDITS").Range("B1:B1").Value '+ " " & D
Else
End If
End With
BIBLETEXTWINDOW.TextBox1.SelStart = 0 'BIBLETEXTWINDOW = Userform name.  THIS CHANGES TO USERFORM  NAME AND SHT NAME
End Sub
This is a lot of code to have to continually copy to different buttons on different userforms.
As it stands now, If I want to be able to do this on any and all textboxes on any and all userforms – I’d have to copy this entire code block below to all userform buttons. and make changes to code lines where indicated above. A painstaking process.

The way I see it is that the only way to do this is that the code must be generic – that is Excel must be able to “recognize” each userform and textbox independently, and the text that is being highlighted and selected - the same as the Activetextbox on the Activeuserform(if there are such things) and run the rest of the code.

Hopefully, there's a way to highlight and select any text in multiuserform multitextbox an application do this without recopying so much code.

I would really appreciate anyone's help in this

cr
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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