VBA inserting in wrong sheet

PierreAB

New Member
Joined
Sep 6, 2023
Messages
4
Office Version
  1. 365
Platform
  1. MacOS
Hey guys!
I have been working on a project for a while to automate a task for my work. Final steps now, but for some reason my code is not working as it should, even if I try different versions.

Sub InsertColumnsAndLabel()
' Select the "YT" sheet
Sheets("YT").Select

' Insert 4 columns after column B
Range("C1:F1").EntireColumn.Insert

' Label the columns
Range("C1").Value = "imps"
Range("D1").Value = "Spend"
Range("E1").Value = "Views"
Range("F1").Value = "Conv."
End Sub


This is one of the ones I have tried.
My issue is that even if I select or activate the sheet labeled as "YT", the next lines all end up being applied to another sheet in the workbook called "AutoBUILD".

Thanks for your help!
Pierre
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I have
It inserted the columns in the right sheet and then went labeled the columns in the other sheet
 
Upvote 0
have you tried

Sheets("YT").Range("C1:F1").EntireColumn.Insert
Got it to work with this:

Sub InsertColumnsAndLabel3()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("YT")

' Check if the sheet exists
If Not ws Is Nothing Then
' Insert 4 columns after column B
ws.Columns("C:F").Insert Shift:=xlToRight

' Label the columns
ws.Cells(1, 3).Value = "imps"
ws.Cells(1, 4).Value = "Spend"
ws.Cells(1, 5).Value = "Views"
ws.Cells(1, 6).Value = "Conv."

' Find the last used row in column B
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

' Apply the VLOOKUP formula to the "imps" column
ws.Range("C2:C" & lastRow).Formula = "=VLOOKUP(B2, PR!A:C, 2, FALSE)"

' Apply the VLOOKUP formula to the "Spend" column
ws.Range("D2:D" & lastRow).Formula = "=VLOOKUP(B2, PR!A:C, 3, FALSE)"

' Apply the VLOOKUP formula to the "Views" column
ws.Range("E2:E" & lastRow).Formula = "=VLOOKUP(B2, PR!A:J, 4, FALSE)"

' Apply the VLOOKUP formula to the "Conv." column
ws.Range("F2:F" & lastRow).Formula = "=VLOOKUP(B2, PR!A:J, 5, FALSE)"
Else
MsgBox "The sheet 'YT' does not exist."
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,734
Messages
6,180,632
Members
452,991
Latest member
JM_000888

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