Macro inserts column in wrong place

DakotaV

New Member
Joined
Mar 20, 2020
Messages
6
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,

I usually code macro's in Excel but I need the process automated. I tried to record a macro but for some reason it goes horribly wrong. This is what I want it to do (and what I recorded):

- Insert a column in a table
- Do a VLOOKUP
- Do this for all remaining rows

When I recorded the macro it looked like this:

VBA Code:
Sub InsertFirstName()

Columns("B:B").Select

Range("B3").Activate

Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("B4").Select

ActiveCell.FormulaR1C1 = "First Name"

Range("B5").Select

ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Append1,3,FALSE)"

Range("B5").Select

Selection.AutoFill Destination:=Range("B5:B49"), Type:=xlFillDefault

Range("B5:B49").Select

ActiveWindow.SmallScroll Down:=-12

End Sub

When I run the macro above for some reason it inserts a column in front of my table and moves my table a couple of cells to the right. The place where I want the new column with the Vlookup is column B. So the original column B would go to the right side and change to C.

I would really appreciate some help on how I can fix this code or how I can properly record a macro?

Thanks in advance!
 
Ok, how about
VBA Code:
Sub InsertFirstName()
   Dim Ws As Worksheet
   For Each Ws In Worksheets
      If Ws.Name <> "Data" Then
         Ws.Columns("B:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
         Ws.Range("B4").Value2 = "First Name"
         Ws.Range("B5:B49").FormulaR1C1 = "=VLOOKUP(RC[-1],Append1,3,FALSE)"
      End If
   Next Ws
End Sub

This code worked like a charm. Thank you very much for this, I appreciate the help!
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

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