I am writing a macro to work in an excel sheet that is saved within a template then exported to .xls or .xlsx from a separate application. This macro is to copy two columns 'x' number of times depending on a quantity that is entered into a cell.
Sub Matrix_Quantity()
Dim x As Integer
x = ActiveWorkbook.Sheets("Inspection Sampling Matrix").Cells(11, 4)
Dim n As Integer
n = x - 1
For numtimes = 1 To n
'Loop by using x as the index number to make x number copies.
Sheets("Inspection Report").Columns("F:G").Select
Selection.Copy
Selection.Insert Shift:=x1 + nToRight
Next
End Sub
The problem I am having is that when the macro is run withing the template (.xlt) it runs fine. As soon as the template is converted to .xls or .xlsx it spots working and gives a runtime error. When debugging the macro it highlights
Sheets("Inspection Report").Columns("F:G").Select
My feeling is that it is looking to select the columns in the .xlt workbook but when converted to .xls or .xlsx it is still trying to look for the .xlt workbook and I'm not sure how or why its doing this.
Inspection Report
isn't active at the moment macro runs. Read how to avoid using Select/Active statements, please. You could replace your code withSheets("Inspection Report").Columns("F:G").Copy
andSheets("Inspection Report").Columns("F:G").Insert Shift:=x1 + nToRight
– Dmitry PavlivShift:=x1 + nToRight
? should it beShift:=xlToRight
? – Dmitry Pavliv