Error Retrieving data with XLOOKUP after using VBA to copy to source cells.

Derick63

Board Regular
Joined
Apr 6, 2018
Messages
76
Office Version
  1. 365
Hello everyone. I have a doozy of a situation here:
On my Inventory sheet I have a table listing all our products, 2500+ items. Column A, Item #, has a mixture of alphanumeric and numeric assigned to each item and that column is formatted as text for the reason of not needing to differentiate between alpha, numerical, alphanumerical, values retaining leading zero's, and preventing scientific notation. When I select any cell on any row, or rows, a shape pops up on the selected row, with an assigned macro, to copy the Item # on the selected row or rows to the first blank row of the Inventory Adjustment worksheet (InvWkSht(code)). The macro adds an apostrophe ( ' ) only to numbers so it would register as text on the Adjustment Worksheet in Column D, row 7 being the first row of data. This works really great and solved the initial problem of a numerical entry. The next column, column B, Item Name, has the formula;
Excel Formula:
=IF([@[Item '#]]="","",XLOOKUP([@[Item '#]], Products[Item '#], Products[Item Name], "Item Not Found",0))
It locates the Item Name and enters it without any issues.
The problem here is, on the Adjustment worksheet if I manually enter a number by typing or with a barcode scanner without prefixing the number with an apostrophe, the formula wont find the item in the Item # column on the Inventory worksheet.

Screenshot 2024-06-15 195210.png

Inventory Table. Item # column

I'm looking for another solution to this scenario that the XLOOKUP would look for the value in Item # on the Inventory sheet whether it's alpha, numerical or alphanumerical values.
How can I do this without formatting the Item # column to the Text format?
Thanks in advance for any energy you exert into this and I totally appreciate it.

Here's the Copy Items To Adjustment WorkSheet Macro:
VBA Code:
Sub CopyItems2AdjWkSht()
    Dim targetRange As Range
    Dim visibleCells As Range
    Dim targetCell As Range
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim itemCount As Long
    Dim copiedItems As String
    Dim tbl As ListObject
    
    Set ws = Inventory
    Set targetRange = Selection
    Set tbl = InvWkSht.ListObjects("InventoryAdj")
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    On Error GoTo CleanUp
    
    copiedItems = ""
    itemCount = 0
    
    ' Check if only one cell is selected. If only one cell is selected, set visibleCells to that cell
    If targetRange.Cells.Count = 1 Then
        Set visibleCells = targetRange
    Else
        On Error Resume Next
        Set visibleCells = targetRange.SpecialCells(xlCellTypeVisible) ' Applies to a filtered table with hidden rows
        On Error GoTo 0
    End If
    
    ' Check if there are visible cells within the selection
    If Not visibleCells Is Nothing Then
        For Each targetCell In visibleCells
            If Not Intersect(targetCell, ws.Range("A5:G9999")) Is Nothing And ws.Range("A" & targetCell.Row).Value <> "" Then
                If InvWkSht.Range("D7").Value = "" Then
                    LastRow = 7
                Else
                    LastRow = InvWkSht.Cells(InvWkSht.Rows.Count, "D").End(xlUp).Row + 1
                End If
'----------------------------------------------------------------------------------------------------
' Problem Snippet
                ' Copy the cell value as text to avoid scientific notation
                ' Original  attempt: 
'       InvWkSht.Cells(LastRow, "D").Value = ws.Range("A" & targetCell.Row).Value ' 
                ' Next attempt: Added apostrophe to all items copied
                ' InvWkSht.Cells(LastRow, "D").Value = "'" & ws.Range("A" & targetCell.Row).Value
                ' Final attempt: Added If to differentiate text and numbers
               If IsNumeric(ws.Range("A" & targetCell.Row).Value) Then
                    InvWkSht.Cells(LastRow, "D").Value = "'" & ws.Range("A" & targetCell.Row).Value
                Else
                    InvWkSht.Cells(LastRow, "D").Value = ws.Range("A" & targetCell.Row).Value
                End If
'----------------------------------------------------------------------------------------------------
                
                ' Message box information
                copiedItems = copiedItems & vbNewLine & ws.Range("B" & targetCell.Row).Value
                itemCount = itemCount + 1
            End If
        Next targetCell
        ' Adjust row height
        tbl.DataBodyRange.Rows.AutoFit
        ' Display message box with copied items
        If itemCount > 0 Then
            MsgBox itemCount & " item(s) added to Adjustment WorkSheet:" & vbNewLine & copiedItems, , "Items Added For Adjustment"
        Else
            MsgBox "No valid items were found to copy."
        End If
    Else
        MsgBox "No visible cells found in the selection."
    End If
    
CleanUp:
    ActiveWindow.ScrollRow = 1 
    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub
 

Attachments

  • 1718495693708.png
    1718495693708.png
    32.5 KB · Views: 9

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
For those who would like know how to get a shape to move to the row selected as shown in the image above. Assign a Macro to the shape. Big timesaver for me.
Modify to make it work for you
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim LastRow As Long
    LastRow = .Range("A99999").End(xlUp).Row
    If Shapes("Add").Visible = msoTrue Then Shapes("Add").Visible = msoFalse ' Add is the name of the shape. Hide it.
    If Not Intersect(Target, Range("A5:G" & LastRow)) Is Nothing And Range("A" & Target.Row).Value <> Empty Then
        With Shapes("Add")
            .Left = Range("C" & Target.Row).Left - 50 'Place shape at the end of the cell in column B
            .Top = Range("C" & Target.Row).Top
            .Visible = msoTrue
            .Height = Range("C" & Target.Row).RowHeight ' Always the height of the row so it fits nicely
        End With
    End If
End Sub
 
Upvote 0
How can I do this without formatting the Item # column to the Text format?
I don't understand what is the down side of formatting the column as text when you are prefixing all the numbers with an apostrophe anyway ?

PS: Please update your account profile to show what version of Excel you are using as the answer may vary depending on the version.
 
Upvote 0
I don't understand what is the down side of formatting the column as text when you are prefixing all the numbers with an apostrophe anyway ?
Sorry Alex. It was an error on my part. Pretty embarrassing move on my part actually. This post will be removed shortly. My apologies sir.
 
Upvote 0
Apologies to all who landed here.
The error was on my part and embarrassing to say the least. I'm going to mark it as solved. Thanks for the effort and sorry for wasting your time.
 
Upvote 0

Forum statistics

Threads
1,224,798
Messages
6,181,037
Members
453,013
Latest member
Shubashish_Nandy

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