Good Day,
I need to do a lookup but I would like the format of the source to be the same as the destination. The formats are either number, currency or percentage.
I found an example here: How to copy source formatting of the lookup cell when using Vlookup in Excel?
I followed the instructions on this site but the moment I use the function as described excel seems to hang.
the first part of the code is:
The second part of the code inserting a module is:
The next step is: Click Tools > References. Then check the Microsoft Script Runtime box in the References – VBAProject dialog box
Once I insert the function
Excel just hangs and does not do anything.
I am not a VBA expert and suspect there may be a better way to this.
Any assistance will greatly be appreciated.
I need to do a lookup but I would like the format of the source to be the same as the destination. The formats are either number, currency or percentage.
I found an example here: How to copy source formatting of the lookup cell when using Vlookup in Excel?
I followed the instructions on this site but the moment I use the function as described excel seems to hang.
the first part of the code is:
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice 20180706
Dim I As Long
Dim xKeys As Long
Dim xDicStr As String
On Error Resume Next
Application.ScreenUpdating = False
Application.CutCopyMode = False
xKeys = UBound(xDic.Keys)
If xKeys >= 0 Then
For I = 0 To UBound(xDic.Keys)
xDicStr = xDic.Items(I)
If xDicStr <> "" Then
Range(xDic.Items(I)).Copy
Range(xDic.Keys(I)).PasteSpecial xlPasteFormats
Else
Range(xDic.Keys(I)).Interior.Color = xlNone
End If
Next
Set xDic = Nothing
End If
Application.ScreenUpdating = True
Application.CutCopyMode = True
End Sub
The second part of the code inserting a module is:
Code:
Public xDic As New Dictionary
'Update by Extendoffice 20180706
Function LookupKeepFormat(ByRef FndValue, ByRef LookupRng As Range, ByRef xCol As Long)
Dim xFindCell As Range
On Error Resume Next
Application.ScreenUpdating = False
Set xFindCell = LookupRng.Find(FndValue, , xlValues, xlWhole)
If xFindCell Is Nothing Then
LookupKeepFormat = " "
xDic.Add Application.Caller.Address, " "
Else
LookupKeepFormat = xFindCell.Offset(0, xCol - 1).Value
xDic.Add Application.Caller.Address, xFindCell.Offset(0, xCol - 1).Address
End If
Application.ScreenUpdating = True
End Function
The next step is: Click Tools > References. Then check the Microsoft Script Runtime box in the References – VBAProject dialog box
Once I insert the function
Code:
=LookupKeepFormat(E2,$A$1:$C$8,3)
I am not a VBA expert and suspect there may be a better way to this.
Any assistance will greatly be appreciated.