Sharid
Well-known Member
- Joined
- Apr 22, 2007
- Messages
- 1,066
- Office Version
- 2016
- Platform
- Windows
I have to extract a lot of staff and supply contacts (emails )from a worksheet. I have got this code from here Link but now I am trying to make some changes to it so it pulls from Sheet7. Currently I have the command button On sheet7. However I will be moving it to a userform and Sheet7 may not always be active when it is run. I could be on sheet2 when I click the button.
Problem,
1) How do I get it to work from Sheet7
2) Range to be dynamic and I don't have to input in the range
I was also looking at this, as the range is last row with data, but need to have it checking sheet7. Mr Excel
Problem,
1) How do I get it to work from Sheet7
2) Range to be dynamic and I don't have to input in the range
VBA Code:
Command Button Click ()
Dim WorkRng As Range
Dim arr As Variant
Dim CharList As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
arr = WorkRng.Value
CheckStr = "[A-Za-z0-9._-]"
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
extractStr = arr(i, j)
outStr = ""
Index = 1
Do While True
Index1 = VBA.InStr(Index, extractStr, "@")
getStr = ""
If Index1 > 0 Then
For p = Index1 - 1 To 1 Step -1
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = Mid(extractStr, p, 1) & getStr
Else
Exit For
End If
Next
getStr = getStr & "@"
For p = Index1 + 1 To Len(extractStr)
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = getStr & Mid(extractStr, p, 1)
Else
Exit For
End If
Next
Index = Index1 + 1
If outStr = "" Then
outStr = getStr
Else
outStr = outStr & Chr(10) & getStr
End If
Else
Exit Do
End If
Loop
arr(i, j) = outStr
Next
Next
WorkRng.Value = arr
End Sub
I was also looking at this, as the range is last row with data, but need to have it checking sheet7. Mr Excel
VBA Code:
Dim PosAt As Integer, PosBeg As Integer, PosEnd As Integer, AddLen As Integer
Dim i As Integer, Lrow As Long
On Error Resume Next
Lrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To Lrow
PosAt = InStr(1, Cells(i, 2), "@", vbBinaryCompare)
PosBeg = InStrRev(Cells(i, 2), " ", PosAt, vbBinaryCompare) + 1
PosEnd = InStr(PosAt, Cells(i, 2), " ", vbBinaryCompare)
If PosEnd = 0 Then
PosEnd = Len(Cells(i, 2))
Else
PosEnd = PosEnd - 1
End If
AddLen = PosEnd - PosBeg + 1
Cells(i, 2).Value = Mid(Cells(i, 2), PosBeg, AddLen)
Next i