I am trying to use VBA to add and delete columns in my spreadsheet. The VBA just works for adding. Deleting not working at all.
The spreadsheet template is designed with 8 samples and sample number one starts at column 6. The goal is:
If job has more than 8 samples then macro will add more columns and if less then macro will delete columns.
Here is my code:
Code: Select all
Let>work_file_link=C:\Users\aaa\Documents\QSF-Template\QSF-FI-2-EDITED.xlsx
Let>new_file=C:\Users\aaa\Documents\QSF-Template\test.xlsx
Let>samples=6
Let>column=14
Gosub>edit_columns
SRT>edit_columns
Let>link=work_file_link
Let>save_link=new_file
Let>j=samples
Let>x=column
VBStart
Dim i, start_col, end_col, number_of_samples
Dim link
Sub Insert_col(link, j, save_link, x)
number_of_samples = x - 6
Const xlToRight = -4161
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(link)
Set objSheet = objwb.Sheets("ProcessSheet")
If j > number_of_samples Then
j = j + 5
start_col = x
For i = start_col to j
objSheet.Columns(i).Insert xlToRight
Next
Else
start_col = 6 + j
end_col = number_of_samples - j
For i = 1 To end_col
objSheet.Columns(start_col).Delete xlToRight
Next
End if
objwb.SaveAs (save_link)
objWB.Close True
objExcel.Quit
End Sub
VBEND
VBRun>Insert_col,link,j,save_link,x
END>edit_columns