thedm
New Member
- Joined
- Oct 13, 2022
- Messages
- 11
- Office Version
- 365
- 2021
- 2016
- 2010
- Platform
- Windows
- Mobile
- Web
Hi ExcelExperts,
The below vba codes is working fine. But my boss wants the lookup_value, table_array, col_index_num should put in a inputbox as they added column, meaning dynamic range. match=false as default
She wants the other excel/workbook opened to be vlookup coz we're using another excel file. I really need your help.
The below vba codes is working fine. But my boss wants the lookup_value, table_array, col_index_num should put in a inputbox as they added column, meaning dynamic range. match=false as default
She wants the other excel/workbook opened to be vlookup coz we're using another excel file. I really need your help.
VBA Code:
Sub GetLastMoQOH_Click()
Dim goalsWbk As Workbook, dataWbk As Workbook
Dim goalsWs As Worksheet, dataWs As Worksheet
Dim goalsLastRow As Long, dataLastRow As Long, x As Long
Dim DataRng As Range
Dim FormatRuleInput As String
Set goalsWbk = ThisWorkbook
Set dataWbk = Workbooks.Open("C:\Users\Tesla\Documents\Inventory\LastMonthReport.xlsm")
Set goalsWs = ThisWorkbook.Worksheets("COUNT SHEET")
Set dataWs = dataWbk.Worksheets("Inventory Detail")
goalsLastRow = goalsWs.Range("J" & Rows.Count).End(xlUp).Row
'dataLastRow = dataWs.Range("B" & Rows.Count).End(xlUp).Row
'Set DataRng = dataWs.Range("B5:I" & dataLastRow)
'Get A Cell Address From The User to Get Number Format From
On Error Resume Next
Set DataRng = Application.InputBox( _
Title:="Highlight Cells from Previous/Last Month ", _
Prompt:="Select a cell range to highlight from the header ITEM to the bottom of QOH. Columns should be 8 to get the accurate results.", _
Type:=8)
For x = 2 To goalsLastRow
On Error Resume Next
goalsWs.Range("R" & x).Value = Application.WorksheetFunction.VLookup( _
goalsWs.Range("J" & x).Value, DataRng, 8, False)
Next x
On Error GoTo 0
'Test to ensure User Did not cancel
If DataRng Is Nothing Then Exit Sub
'Set Variable to first cell in user's input (ensuring only 1 cell)
Set DataRng = DataRng.Cells(1, 1)
'Store Number Format Rule
FormatRuleInput = DataRng.NumberFormat
'Apply NumberFormat To User Selection
If TypeName(Selection) = "Range" Then
Selection.NumberFormat = FormatRuleInput
Else
MsgBox "Please select a range of cells before running this macro!"
End If
MsgBox "Done!"
goalsWbk.Activate
goalsWbk.Save
'goalsWbk.Close
'Resume Next
End Sub