Hello Experts,
I 'am novice at vba programming , however am stuck with VBA code to update the Product names from two columns.
I have achieved the first task of updating product names if it exists in first column, now am trying to update the product names after matching the column names from second column.
But am getting stuck in the below line of the code
.Range(mgprodname).AutoFilter Field:=1, Criteria1:=TextToFind
with run time error 1004 object defined error.
I have also attached a sample excel file in the thread.
Kindly help me in fixing the error.
I 'am novice at vba programming , however am stuck with VBA code to update the Product names from two columns.
I have achieved the first task of updating product names if it exists in first column, now am trying to update the product names after matching the column names from second column.
But am getting stuck in the below line of the code
.Range(mgprodname).AutoFilter Field:=1, Criteria1:=TextToFind
with run time error 1004 object defined error.
I have also attached a sample excel file in the thread.
Kindly help me in fixing the error.
Code:
Sub MM2()
Dim r As Long, ws As Worksheet
Dim prodname As Long
Dim prodytpe As Long
Dim lastRow As Long
Dim lastrow1 As Long
prodtype = WorksheetFunction.Match("Producttype", Range("1:1"), 0)
Cells(1, prodtype + 1).EntireColumn.Insert
Cells(1, prodtype + 1) = "Product Names"
prodname = WorksheetFunction.Match("Product Names", Range("1:1"), 0)
Set ws = Sheets("Updateproductnames")
lastRow = ws.Cells(Rows.Count, prodtype).End(xlUp).Row
For r = 2 To lastRow
If ws.Cells(r, prodtype).Value = "Desktops" Then
ws.Cells(r, prodname).Value = "Desktops"
ElseIf ws.Cells(r, prodtype).Value = "Laptops" Then
ws.Cells(r, prodname).Value = "Laptops"
ElseIf ws.Cells(r, prodtype).Value = "Workstations" Then
ws.Cells(r, prodname).Value = "Workstations"
ElseIf ws.Cells(r, prodtype).Value = "Printers" Then
ws.Cells(r, prodname).Value = "Printers"
End If
Next r
With ActiveSheet
TagRows "Attach", "Printers"
TagRows "Commercial Desktops", "Desktops"
TagRows "Commercial Notebooks", "Laptops"
TagRows "Consumer Desktops", "Desktops"
TagRows "Consumer Notebooks", "Laptops"
TagRows "Managed Services", "Printers"
TagRows "LJ Supplies", "Printers"
TagRows "IJ Supplies", "Printers"
TagRows "LJ Supplies", "Printers"
TagRows "Workstations", "Workstations"
TagRows "Scanners", "Printers"
TagRows "IPG Services Support", "Printers"
TagRows "Supplies", "Printers"
End With
End Sub
Sub TagRows(TextToFind As String, TagWithText As String)
Dim mgprodname As Long
Dim lastrow1 as long
With ActiveSheet
mgprodname = WorksheetFunction.Match("ManagementProductnames", Range("1:1"), 0)
'filter the column for "TextToFind"
.Range(mgprodname).AutoFilter Field:=1, Criteria1:=TextToFind
'find the last row
lastrow1 = .Range(mgprodname & Rows.Count).End(xlUp).Row
'if any visible rows, fill in the new comment "TagWithText"
If lastrow1 > 2 Then
.Range(.Range(prodname), .Range(prodname & lastRow1)).SpecialCells(xlCellTypeVisible).Value = TagWithText
End If
.Range(mgprodname).AutoFilter Field:=1 'clear the filter
End With
ActiveSheet.AutoFilterMode = False
End Sub