VBA Vlookup message box on error display message box and exit sub - vlookup

I have following code which on Error will resume to next.
Now I want to display a message box on error and exit SUB
Message box: "Part number" & "PN" & "not found. Please define packaging details"
Sub Vlookup()
Dim Volume As Worksheet
Dim Packaging As Worksheet
Dim PN As Long
Dim Pcs As Long
Dim x As Variant
Dim dataRNG As Range
Set Volume = ThisWorkbook.Worksheets("Volume per shipment")
Set Packaging = ThisWorkbook.Worksheets("Packaging details")
PN = Volume.Range("A" & Rows.Count).End(xlUp).Row
Pcs = Packaging.Range("A" & Rows.Count).End(xlUp).Row
Set dataRNG = Packaging.Range("A2:G" & Pcs)
For x = 2 To PN
On Error Resume Next
Volume.Range("D" & x).Value = Application.WorksheetFunction.Vlookup( _
Volume.Range("A" & x).Value, dataRNG, 7, 0)
Next x
Application.ScreenUpdating = True
End Sub

I have worked on this,and now on error it displays the MsgBox with the missing part number, but also it displays the message if no error (but without part number as it is not missing any).
I want to display a message, only when I have one or more missing part numbers.
Sub Vlookup()
Dim Volume As Worksheet
Dim Packaging As Worksheet
Dim PN As Long
Dim Pcs As Long
Dim x As Long
Dim dataRNG As Range
Dim Msg As String
Dim PartNo As Variant
Set Volume = ThisWorkbook.Worksheets("Volume per shipment")
Set Packaging = ThisWorkbook.Worksheets("Packaging details")
PN = Volume.Range("A" & Rows.Count).End(xlUp).Row
Pcs = Packaging.Range("A" & Rows.Count).End(xlUp).Row
Set dataRNG = Packaging.Range("A2:G" & Pcs)
For x = 2 To PN
PartNo = Application.Vlookup(Volume.Range("A" & x).Value, dataRNG, 7, 0)
If IsError(PartNo) Then
Msg = Msg & vbLf & Volume.Range("A" & x).Value
Else
Volume.Range("D" & x).Value = PartNo
End If
Next x
If Msg <> "" Then MsgBox Msg & vbLf & "not found. Please define packaging details"
End Sub

Related

How to stop second run of the code to prevent override data regex vba?

The below code will split 1 cell into 3 or 4 column based on a pattern of 6chr,5chr,4chr,5+chr. The below also needs to be available on all open workbooks and work from the user selection.
How to fix a bug that after the first splitting of the cell is done and by mistake you run it again will override the data?
Class Module
Option Explicit
'Rename this Class Module cFabric
Private pStyle As String
Private pFabric As String
Private pColour As String
Private pSize As String
Public Property Get Style() As String
Style = pStyle
End Property
Public Property Let Style(Value As String)
pStyle = Value
End Property
Public Property Get Fabric() As String
Fabric = pFabric
End Property
Public Property Let Fabric(Value As String)
pFabric = UCase(Value)
End Property
Public Property Get Colour() As String
Colour = pColour
End Property
Public Property Let Colour(Value As String)
pColour = Value
End Property
Public Property Get Size() As String
Size = pSize
End Property
Public Property Let Size(Value As String)
pSize = Value
End Property
Regular Module
Option Explicit
Sub Fabrics()
Dim wsSrc As Workbook, wsRes As Workbook
Dim vSrc As Variant, vRes As Variant, rRes As Range
Dim RE As Object, MC As Object
Const sPat As String = "^(.{6})\s*(.{5})\s*(.{4})(?:.*1/(\S+))?"
'Group 1 = style
'Group 2 = fabric
'Group 3 = colour
'Group 4 = size
Dim colF As Collection, cF As cFabric
Dim I As Long
Dim S As String
Dim V As Variant
'Set source and results worksheets and ranges
Set wsSrc = ActiveWorkbook
Set wsRes = ActiveWorkbook
Set rRes = wsRes.Application.Selection
'Read source data into array
vSrc = Application.Selection
'Initialize the Collection object
Set colF = New Collection
'Initialize the Regex Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = sPat
'Test for single cell
If Not IsArray(vSrc) Then
V = vSrc
ReDim vSrc(1 To 1, 1 To 1)
vSrc(1, 1) = V
End If
'iterate through the list
For I = 1 To UBound(vSrc, 1)
S = vSrc(I, 1)
Set cF = New cFabric
If .test(S) = True Then
Set MC = .Execute(S)
With MC(0)
cF.Style = .submatches(0)
cF.Fabric = .submatches(1)
cF.Colour = .submatches(2)
cF.Size = .submatches(3)
End With
Else
cF.Style = S
End If
colF.Add cF
Next I
End With
'create results array
'Exit if no results
If colF.Count = 0 Then Exit Sub
ReDim vRes(1 To colF.Count, 1 To 4)
'Populate the rest
I = 0
For Each V In colF
I = I + 1
With V
vRes(I, 1) = .Style
vRes(I, 2) = .Fabric
vRes(I, 3) = .Colour
vRes(I, 4) = .Size
End With
Next V
'Write the results
Set rRes = rRes.Resize(UBound(vRes, 1), UBound(vRes, 2))
rRes.Value = vRes
End Sub
Credits for the above goes to #Ron Rosenfeld for the project!
One way to tell if the entry has been previously split is as follows
If the regex.test fails, then
If the results line passes, then the item has been previously split
if not, then it is a blank, or a malformed entry
Note that a lot of this could be avoided if you were not overwriting your original data. I would recommend against overwriting your data both for audit and debugging purposes, but the below should help in case you cannot change that.
You just need to make some small changes in the logic where we checked for the malformed entry originally. As well as reading in the "possible" results array into vSrc so that we have the potentially split data to compare:
Option Explicit
Sub Fabrics()
'assume data is in column A
Dim wsSrc As Worksheet, wsRes As Worksheet
Dim vSrc As Variant, vRes As Variant, rRes As Range
Dim RE As Object, MC As Object
Const sPat As String = "^(.{6})\s*(.{5})\s*(.{4})(?:.*1/(\S+))?"
'Group 1 = style
'Group 2 = fabric
'Group 3 = colour
'Group 4 = size
Dim colF As Collection, cF As cFabric
Dim I As Long
Dim S As String
Dim V As Variant
'Set source and results worksheets and ranges
Set wsSrc = ActiveSheet
Set wsRes = ActiveSheet
Set rRes = Selection
'Read source data into array
vSrc = Selection.Resize(columnsize:=4)
'Initialize the Collection object
Set colF = New Collection
'Initialize the Regex Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = sPat
'iterate through the list
'Test for single cell
If Not IsArray(vSrc) Then
V = vSrc
ReDim vSrc(1 To 1, 1 To 1)
vSrc(1, 1) = V
End If
For I = 1 To UBound(vSrc, 1)
S = vSrc(I, 1)
Set cF = New cFabric
If .test(S) = True Then
Set MC = .Execute(S)
With MC(0)
cF.Style = .submatches(0)
cF.Fabric = .submatches(1)
cF.Colour = .submatches(2)
cF.Size = .submatches(3)
End With
ElseIf .test(vSrc(I, 1) & vSrc(I, 2) & vSrc(I, 3)) = False Then
cF.Style = S
Else
cF.Style = vSrc(I, 1)
cF.Fabric = vSrc(I, 2)
cF.Colour = vSrc(I, 3)
cF.Size = vSrc(I, 4)
End If
colF.Add cF
Next I
End With
'create results array
'Exit if not results
If colF.Count = 0 Then Exit Sub
ReDim vRes(1 To colF.Count, 1 To 4)
'Populate
I = 0
For Each V In colF
I = I + 1
With V
vRes(I, 1) = .Style
vRes(I, 2) = .Fabric
vRes(I, 3) = .Colour
vRes(I, 4) = .Size
End With
Next V
'Write the results
Set rRes = rRes.Resize(UBound(vRes, 1), UBound(vRes, 2))
With rRes
.Clear
.NumberFormat = "#"
.Value = vRes
.EntireColumn.AutoFit
End With
End Sub
Disregarding the previous regex/class method,
Option Explicit
Sub Fabrics_part_Deux()
Dim a As Long, b As Long
With Worksheets("Sheet1")
If .AutoFilterMode Then .AutoFilterMode = False
With .Range(.Cells(1, "A"), .Cells(.Rows.Count, "B").End(xlUp).Offset(0, 3))
With .Columns("B")
.Offset(1, 0).Replace what:=Chr(32), replacement:=vbNullString, lookat:=xlPart
End With
.AutoFilter field:=2, Criteria1:="<>"
.AutoFilter field:=3, Criteria1:=""
With .Resize(.Rows.Count - 1, 1).Offset(1, 1)
If CBool(Application.Subtotal(103, .Cells)) Then
With .SpecialCells(xlCellTypeVisible)
For a = 1 To .Areas.Count
With .Areas(a).Cells
.TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(6, 1), Array(11, 1), Array(15, 2))
For b = 1 To .Rows.Count
.Cells(b, 2) = UCase$(.Cells(b, 2).Value2)
If CBool(InStr(1, .Cells(b, 4).Value2, Chr(47), vbBinaryCompare)) Then
.Cells(b, 4) = Trim(Split(.Cells(b, 4), Chr(47))(1))
End If
Next b
End With
Next a
End With
End If
End With
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With
End Sub
In your code to output to the spreadsheet, you need to check for empty strings
I = 0
For Each V In colF
I = I + 1
With V
vRes(I, 1) = .Style
If len(.Fabric) > 0 then
vRes(I, 2) = .Fabric
vRes(I, 3) = .Colour
vRes(I, 4) = .Size
End If
End With
Next V

How do I create a list in cells choosing from a combobox and selecting a button using VBA?

I have a combobox filled with values. I want to select a value in the combo box and click the "Add" button to place this value into the some cells below. I can add one item to my list using the following code, but I want to be able to add multiple items. I feel that I am very close, I just need a few tweaks!
Private Sub CommandButtonAddItem_Click()
Dim ws As Worksheet
Dim box As ComboBox
Dim food As String
Dim num As Integer
num = 19
Set ws = Worksheets("sheet1")
Set box = ws.OLEObjects("ComboBox1").Object
food = box.Value
Worksheets("sheet1").Cells(num, 1) = food
If Worksheets("sheet1").Cells(num, 1) = " " Then
Worksheets("sheet1").Cells(num, 1) = food
num = num + 1
End If
End Sub
Try THIS!
If the "default" cell is already occupied, it'll keep going down untill it finds one that's not empty, to then put the value in that cell.
Private Sub CommandButtonAddItem_Click()
Dim ws As Worksheet
Dim box As ComboBox
Dim food As String
Dim num As Integer
num = 19
Set ws = Worksheets("sheet1")
Set box = ws.OLEObjects("ComboBox1").Object
food = box.Value
While Worksheets("sheet1").Cells(num, 1) <> ""
num = num + 1
Wend
Worksheets("sheet1").Cells(num, 1) = food
End If
End Sub

VBA: How to list items on rows separated by headers?

I want to list rows from Sheet1 to Sheet2 based on a criteria, move on to next criteria once there is no more rows to copy according to the first criteria and also separate the copied rows by headers.
Sheet1 contains an unsorted list of projects, where I want to be able to add and remove projects whenever. I also want to categorize projects into different types. Sheet1 would look like this:
ProjectID ProjectName Type Cost
1 ProjectA Development -120
2 ProjectB Development -250
3 ProjectC Maintenance -30
I would then like to copy the data via VBA to Sheet2 in the following format:
Maintenance Projects
ProjectID ProjectName Type Cost
3 ProjectC Maintenance -30
Development Projects
ProjectID ProjectName Type Cost
1 ProjectA Development -120
2 ProjectB Development -250
I've been trying to look for a solution but haven't found one that would suit my need and I am not a very experienced VBA user. Any tips or hints on what method to use here?
This will copy your data from sheet1 to sheet2 assuming sheet2 is blank in the format that you requested.
Sub SplitData_Click()
Dim dicType As Object
Set dicType = CreateObject("scripting.dictionary")
Dim i As Integer
Dim lstRow As Long
Dim val As String
lstRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 4).End(xlUp).Row
Dim projects() As Variant
ReDim projects(0 To lstRow - 2, 0 To 3) ' I like 0 based arrays
' Populate the dictionary with the unique types
For i = 2 To lstRow
projects(i - 2, 0) = Range("A" & i) ' ProjectID
projects(i - 2, 1) = Range("B" & i) ' ProjectName
projects(i - 2, 2) = Range("C" & i) ' Type
projects(i - 2, 3) = Range("D" & i) ' Cost
val = Range("C" & i)
If dicType.Exists(val) Then
dicType.Item(val) = dicType.Item(val) + 1
Else
dicType.Add val, 1
End If
Next i
Dim header() As Variant
ReDim header(0 To 3)
header(0) = "ProjectId"
header(1) = "ProjectName"
header(2) = "Type"
header(3) = "Cost"
Sheets("Sheet2").Select
' loop through each type and build its structure on sheet 2
Dim key As Variant
For Each key In dicType
If Range("A1") = "" Then
Range("A1").Value = key & " Projects"
Else
lstRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row + 2
Range("A" & lstRow).Value = key & " Projects"
End If
lstRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row + 1
Range("A" & lstRow).Value = header(0)
Range("B" & lstRow).Value = header(1)
Range("C" & lstRow).Value = header(2)
Range("D" & lstRow).Value = header(3)
For i = 0 To UBound(projects)
lstRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row + 1
If projects(i, 2) = key Then
Range("A" & lstRow).Value = projects(i, 0)
Range("B" & lstRow).Value = projects(i, 1)
Range("C" & lstRow).Value = projects(i, 2)
Range("D" & lstRow).Value = projects(i, 3)
End If
Next i
Debug.Print key
Next key
End Sub

Getting child properties in WPF Using VisualTreeHelper not returning values

Once I use visualTreeHelper.getchild to find a child object, how would I get the name of that object, or even other properties of the object like width or height?
i.e.
This doesnt work:
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(Can1) - 1
Dim ChildVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual)
Dim ChildName As DependencyProperty = childVisual.GetValue(Name)
It says value of type "Name" cannot be converted to a system.windows.dependencyProperty
Nor does this work (But at least it compiles):
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(Can1) - 1
Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual)
Dim GT1 As GeneralTransform = childVisual.TransformToAncestor(Can1)
Dim currentpoint As Point = GT1.Transform(New Point(0, 0))
x = currentpoint.X
y = currentpoint.Y
If I hover over childvisual, I can look at it's properties and see that name has been set to a name of an image I have on the canvas(Can1).
But, X and Y are always 0.
I found this finally on the net, and it seems to work great.
Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual)
Dim ChildName As String = ChildVisual.GetValue(Control.NameProperty)

Problem with Win32_PhysicalMedia SerialNumber property

I wrote the following code to get the physical media serial number but in one of my computers it returns null instead.
Does anybody know what the problem is?
Thanks.
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach( ManagementObject mo in searcher.Get() )
{
Console.WriteLine("Serial: {0}", mo["SerialNumber"]);
}
The Serial Number is optional, defined by the manufacturer, and for your device it is either blank or unsupported by the driver.
Virtually all hard drives have a serial number, but most USB-style Flash memory sticks do not (generally a cost issue). I would imagine most unbranded CD/DVD/BD discs would also be non-serialized.
Here is the code I used, the serial number somehow is returned raw with each pair of chars reversed (strange) and using Win32_PhysicalMedia gave different results if I ran the code as a user or an Administrator(more strange) - Windows 7 Ultimate, VS 2008 using VB only:
Function GetHDSerial() As String
Dim strHDSerial As String = String.Empty
Dim index As Integer = 0
Dim Data As String
Dim Data2 As String
Dim ndx As Integer
Dim query As New SelectQuery("Win32_DiskDrive")
Dim search As New ManagementObjectSearcher(query)
Dim info As ManagementObject
Try
For Each info In search.Get()
Data = info("SerialNumber")
Data2 = ""
For ndx = 1 To Data.Length - 1 Step 2
Data2 = Data2 & Chr(Val("&H" & Mid(Data, ndx, 2)))
Next ndx
Data = String.Empty
For ndx = 1 To Data2.Length - 1 Step 2
Data = Data & Mid(Data2, ndx + 1, 1) & Mid(Data2, ndx, 1)
Next
Data2 = Data
If Len(Data) < 8 Then Data2 = "00000000" 'some drives have no s/n
Data2 = Replace(Data2, " ", "") ' some drives pad spaces in the s/n
'forget removeable drives
If InStr(info("MediaType").ToString, "Fixed", CompareMethod.Text) > 0 Then
strHDSerial = strHDSerial & "Drive " & index.ToString & " SN: " & Data2 & vbCrLf
index += 1
End If
Next
Catch ex As Exception
strHDSerial = "Error retrieving SN for Drive "
msgbox(index.ToString)
End Try
Return strHDSerial
End Function