It's kinda of complicated. I'm creating a Userform which enables my coworkers to track their work. They are required to enter the phone number and other information. What I'm trying to do it, when they've entered the phone number in txtBTN, I want the macro to take the first 6 digits (9 digits will be entered in the text box, without any spaces) and do a vlookup to an excel worksheet ("Markets")whick has the 6 digits in column A, and the markets are in colum B. And to bring back the market that's matched to a text box called txtMkt in the Userform.
I have the coding to do this from one excel sheet to another, but I can't get it to work, userform component to userform component.
This is what I have for the macro for the excel sheet to excel sheet.
Option Explicit
Sub MarketSearch()
On Error Resume Next
Dim strMarketFileName As String ' Area Code and Market Name File
Dim intLastRowPasteFile As Integer ' Last Row in Source File
Dim strPasteBookName As String ' file name to paste data in
Dim strPasteBookVlookupColumn As String * 1 ' row with phone numbers
Dim intPasteBookPasteColumn As Integer 'column to paste market data in
Dim lngAreaCodeAniToFind As Long ' Area Code and ANI to Find
Dim strMarketName As String ' Market Name in strMarketFileName
Dim I As Integer ' counter
Dim RangeObj As Object
I = 0 'init
intLastRowPasteFile = 0
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'====Source File====start===============
strPasteBookName = ActiveWorkbook.Name ' file name to paste data in
Range("A1").Select
' row for vlookup
Cells.Find(What:="Originating Number", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False).Activate
strPasteBookVlookupColumn = Chr(ActiveCell.Column + 64)
intPasteBookPasteColumn = ActiveCell.Column + 5
intLastRowPasteFile = Range("A1").End(xlDown).Row
'====Source File====end===============
'====Market File====start===============
strMarketFileName = GetFileName() ' Area Code and Market Name File
Workbooks.Open (strMarketFileName)
strMarketFileName = ActiveWorkbook.Name
'====Market File====end===============
'vlookup====start===================
For I = 1 To intLastRowPasteFile
lngAreaCodeAniToFind = 0 'reset
Err.Clear
Workbooks(strPasteBookName).Activate
lngAreaCodeAniToFind = Str(Left((Range(strPasteBookVlookupColumn & I).Value), 6))
If lngAreaCodeAniToFind = 0 Or lngAreaCodeAniToFind = Null Then GoTo NextILoop
Workbooks(strMarketFileName).Activate
Columns("A:A").Select
Set RangeObj = Selection.Find(What:=lngAreaCodeAniToFind, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False)
If RangeObj Is Nothing Then GoTo NextILoop Else RangeObj.Select
strMarketName = ActiveCell.Offset(0, 1).Value
Workbooks(strPasteBookName).Activate
Cells(I, intPasteBookPasteColumn).Value = strMarketName
NextILoop:
Next I
'vlookup====end===================
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I'm willing to try anything right now.
Thanks in Advance
I have the coding to do this from one excel sheet to another, but I can't get it to work, userform component to userform component.
This is what I have for the macro for the excel sheet to excel sheet.
Option Explicit
Sub MarketSearch()
On Error Resume Next
Dim strMarketFileName As String ' Area Code and Market Name File
Dim intLastRowPasteFile As Integer ' Last Row in Source File
Dim strPasteBookName As String ' file name to paste data in
Dim strPasteBookVlookupColumn As String * 1 ' row with phone numbers
Dim intPasteBookPasteColumn As Integer 'column to paste market data in
Dim lngAreaCodeAniToFind As Long ' Area Code and ANI to Find
Dim strMarketName As String ' Market Name in strMarketFileName
Dim I As Integer ' counter
Dim RangeObj As Object
I = 0 'init
intLastRowPasteFile = 0
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'====Source File====start===============
strPasteBookName = ActiveWorkbook.Name ' file name to paste data in
Range("A1").Select
' row for vlookup
Cells.Find(What:="Originating Number", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False).Activate
strPasteBookVlookupColumn = Chr(ActiveCell.Column + 64)
intPasteBookPasteColumn = ActiveCell.Column + 5
intLastRowPasteFile = Range("A1").End(xlDown).Row
'====Source File====end===============
'====Market File====start===============
strMarketFileName = GetFileName() ' Area Code and Market Name File
Workbooks.Open (strMarketFileName)
strMarketFileName = ActiveWorkbook.Name
'====Market File====end===============
'vlookup====start===================
For I = 1 To intLastRowPasteFile
lngAreaCodeAniToFind = 0 'reset
Err.Clear
Workbooks(strPasteBookName).Activate
lngAreaCodeAniToFind = Str(Left((Range(strPasteBookVlookupColumn & I).Value), 6))
If lngAreaCodeAniToFind = 0 Or lngAreaCodeAniToFind = Null Then GoTo NextILoop
Workbooks(strMarketFileName).Activate
Columns("A:A").Select
Set RangeObj = Selection.Find(What:=lngAreaCodeAniToFind, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False)
If RangeObj Is Nothing Then GoTo NextILoop Else RangeObj.Select
strMarketName = ActiveCell.Offset(0, 1).Value
Workbooks(strPasteBookName).Activate
Cells(I, intPasteBookPasteColumn).Value = strMarketName
NextILoop:
Next I
'vlookup====end===================
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I'm willing to try anything right now.
Thanks in Advance