Forum Discussion

rijuthomas's avatar
rijuthomas
Copper Contributor
Jul 22, 2019

Need help in macros

i have a excel which i have attached to this post, where in raw sheet is the raw data in which i need to insert a column in between A and B and copy the heading of the data against the date, final copy how i want i have attached in the other sheet.

 

 

3 Replies

  • Kodipady's avatar
    Kodipady
    Iron Contributor

    rijuthomas 

    I may be missing something, your attached file July DRAFT.XLS has only one sheet called "Sheet1".  Column B has a header called "DOCUMENT DATE". Is that the new column you would like to insert?  If yes, how do you derive this column value?  

    • rijuthomas's avatar
      rijuthomas
      Copper Contributor

      Kodipady​sorry i think wrong file uploaded, have uploaded correct file

       

      let me know any doubts

       

      <Admin Removed File Attachment for Privacy Reasons >>

      • HansVogelaar's avatar
        HansVogelaar
        MVP

        Here you go:

        Sub InsertColumn()
            Dim wsh As Worksheet
            Dim ref As String
            Dim r As Long
            Dim m As Long
            Dim v As Variant
            Application.ScreenUpdating = False
            Set wsh = ActiveSheet ' Worksheets("raw sheet")
            wsh.Range("B1").EntireColumn.Insert
            m = wsh.Range("C" & wsh.Rows.Count).End(xlUp).Row
            For r = 3 To m
                v = wsh.Range("C" & r).Value
                If IsNumeric(v) Or IsDate(v) Then
                    wsh.Range("B" & r).Value = ref
                Else
                    ref = v
                End If
            Next r
            wsh.UsedRange.EntireColumn.AutoFit
            Application.ScreenUpdating = True
        End Sub

Resources