• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

insert a column if not pre-existing and save the file(while consolidation)

aprvvsh

New Member
While consolidating multiple sheets I have to insert a column along with the header value if it doesn't exist already and save the file too.
I tried following but its not changing the column value.
Code:
If Workbooks(filename).Sheets("ABC").Cells("F1").Value <> "Header" Then
  Columns("F:F").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
  Workbooks(filename).Sheets("ABC").Cells("F1").Value = "Header"
  End If
 
Hi aprvvsh,

Try to change the cell reference used in code to below-

If Workbooks(Filename).Sheets("ABC").Cells(1, 6).Value <> "Header" Then

Above worked fine for me.

Cheers
AJ
 
Hi aprvvsh,

See code below


If Workbooks(Filename).Worksheets("sheet1").Cells(1, 6).Value <> "Header" Then
Columns("F:F").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Workbooks(Filename).Sheets("sheet1").Cells(1, 6).Value = "Header"
Workbooks(Filename).Save
'''OR add below to save and close file
''' Workbooks(Filename).Close SaveChanges:=True
end if


Cheers
 
Back
Top