Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

problem insert the column with formatting

0

hi

I  searched  many  ways  to  insert  column  with  formatting    so far  I  don't  find  any  solution  for this  problem   .. the  problemis  just  in the  first  column .

Sub CreateNewSheet()
Dim dDate As Long
Dim oNum As String
Dim lst As Long

sheet1.Copy After:=Sheets(Sheets.Count)

With ActiveSheet
    dDate = .Range("B3").Value
    oNum = .Range("D3").Value
    lst = .Range("A" & Rows.Count).End(xlUp).Row
    .Columns(3).Insert
    .Columns("1").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    .Range("A4").Value = "date"
    .Range("D4").Value = "order"
    .Range("A5:A" & lst).Value = Format(dDate, "dd/mm/yyyy")
    .Range("D5:D" & lst).Value = oNum
    .Rows("1:3").Clear
End With
End Sub
Answer
Discuss

Answers

0
Selected Answer

Maklil

I think there are 2 problems with column 1 in your code

  1. You used  .Columns("1").,Insert.... but it should read .Columns(1).,Insert....
  2. You use Format(dDate, "dd/mm/yyyy") but that just makes a text  version of the dDate

Also you get sheet names like Sheet1(2), from the Copy command.

From the file you attached latterly, in the revised Ive corrected those points (changes in bold). For the dates in column 1, note that I have two lines to first apply the date numeric value then (Range)..NumberFormat to set it as a date (not General format). Prior to that you'll see that the CopyOrgin is chnaged on inserting clumn 1 (to adopt fromatting for headers and borders).

Sub CreateNewSheet1()
Dim dDate As Long
Dim oNum As String
Dim lst As Long

sheet1.Copy After:=Sheets(Sheets.Count)

On Error Resume Next

With ActiveSheet
    dDate = .Range("B3").Value
    oNum = .Range("D3").Value
    lst = .Range("A" & Rows.Count).End(xlUp).Row
    .Name = "Stock " & Format(dDate, "ddmmmyyyy") ' name sheet with date
    .Columns(3).Insert
    .Columns(1).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightOrBelow
    .Range("A4").Value = "Date"
    .Range("D4").Value = "Order"
    .Range("A5:A" & lst).Value = dDate ' add the value
    .Range("A5:A" & lst).NumberFormat = "dd/mm/yyyy" ' set the cell format
    .Range("D5:D" & lst).Value = oNum
    .Rows("1:3").Clear
End With
End Sub
Note that I've given the new sheet a name including the date but if you add another sheet onthe same date, you'll still get a sheet name like Sheet1(2) (without the macro crashing, thanks to the On Error ...line), You could add logic to avoid that but I'm assuming in real life you won't run the macro more than once a day).,

Hope this works for you.

Discuss

Discussion

John thanks . but unfortunately it doesn't work  . may  you see my file,please?  may be finding the problem.
MAKLIL (rep: 34) Jul 13, '21 at 7:44 am
Maklil

Please see my revised answer. Note that it nearly always helps us if you include your file with the original question (or we are forced to guess some things).
John_Ru (rep: 6142) Jul 13, '21 at 8:29 am
John
sorry about   I   don't  attach  to  see  my  data  closely .
your  updating  works  perfectly . thanks  for  your assistance . 
MAKLIL (rep: 34) Jul 13, '21 at 8:42 am
No problem Maklil. Glad it worked. 
John_Ru (rep: 6142) Jul 13, '21 at 9:04 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login