VBA Find column by header and apply TRIM in that column

Addictions

Board Regular
Joined
May 27, 2018
Messages
60
Office Version
  1. 365
Hello,

Please can somebody help with VBA code?.
I'm looking for VBA code to find column by header name and apply TRIM in that column. Column name is "Title".

I'm also looking for vba code for VLOOKUP. Find column by header name "SKU" and apply vlookup in that column "=VLOOKUP("Should be SKU column here",Sheet2!B:E,2,FALSE)"

Please help.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This will do part 1
Code:
Sub Addictions()
   Dim Fnd As Range, addr As String
   Set Fnd = Range("1:1").Find("title", , , xlWhole, , , False, , False)
   If Fnd Is Nothing Then Exit Sub
   With Range(Fnd, Cells(Rows.Count, Fnd.Column).End(xlUp))
      .Value = Evaluate("if({1},trim(" & .Address & "))")
   End With
End Sub
 
Upvote 0
Your welcome.
For part2 you cannot put the vlookup in the sku column, if you want it to lookup the value in that column.
 
Upvote 0
Your welcome.
For part2 you cannot put the vlookup in the sku column, if you want it to lookup the value in that column.

I see. Currently I am using:

Code:
 Sub Vlookup()
Dim Vlkp As String
Dim rngUsernameHeader As Range, rngHeaders As Range

Vlkp = "=VLOOKUP(L2,Vlookup!B:E,2,FALSE)"

Set rngHeaders = Range("1:1")
    Set rngUsernameHeader = rngHeaders.Find(what:="CustomLabel", After:=Cells(1, 1))


    rngUsernameHeader.Offset(0, 1).EntireColumn.Insert
    rngUsernameHeader.Offset(0, 1).Value = "SKU"
     
   Set Fnd = Range("1:1").Find("ASIN", , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      Fnd.Offset(1).Resize(Range("A" & Rows.Count).End(xlUp).Row - 1).Value = Vlkp
   End If
End Sub


It does the job, but its not going to work if column L is going to be moved.
Thank you for your help.

Also as a last stage of the process would you possibly know how to copy all data from Column SKU, Title, Picture paste it in new workbook and save it on desktop as name results?.

I really appreciate your great help. Thank you so much!
 
Last edited:
Upvote 0
How about
Code:
Sub Addictions()
   Dim Fnd As Range, addr As String
   Set Fnd = Range("1:1").Find("sku", , , xlWhole, , , False, , False)
   If Fnd Is Nothing Then Exit Sub
   Fnd.Offset(, 1).EntireColumn.Insert
   With Range(Fnd.Offset(1, 1), Cells(Rows.Count, Fnd.Column).End(xlUp).Offset(, 1))
      .Formula = "=VLOOKUP(" & Fnd.Offset(1).Address(0, 0) & ",Sheet2!B:E,2,FALSE)"
   End With
End Sub
 
Upvote 0
How about
Code:
Sub Addictions()
   Dim Fnd As Range, addr As String
   Set Fnd = Range("1:1").Find("sku", , , xlWhole, , , False, , False)
   If Fnd Is Nothing Then Exit Sub
   Fnd.Offset(, 1).EntireColumn.Insert
   With Range(Fnd.Offset(1, 1), Cells(Rows.Count, Fnd.Column).End(xlUp).Offset(, 1))
      .Formula = "=VLOOKUP(" & Fnd.Offset(1).Address(0, 0) & ",Sheet2!B:E,2,FALSE)"
   End With
End Sub

It is working. Thats great. Is it possible that inserted column could have a header names Results instead of blank possibly?
 
Upvote 0
Yup, just add this
Code:
Fnd.Offset(,1).value="Results"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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