Results 1 to 6 of 6

Thread: Add new column in excel using Visual Basic

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Add new column in excel using Visual Basic

    I have 10 excel files in the folder. Each of the files have five columns. How I can add another one new column name "CATATAN" at the end of the columns so that I have six column.

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Add new column in excel using Visual Basic

    excel already has more columns, do u mean to hide / unhide column?
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Add new column in excel using Visual Basic

    Actually, I have table with 5 columns "A", "B", "C", "D","E". I would like to insert new column in the table using visual basic. The new columns in the table is "CATATAN". It take time to do manually one by one.

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Add new column in excel using Visual Basic

    Try this (sh is the WorkSheet Object)
    Code:
        With sh.UsedRange
            sh.Cells(.Row, .Column + .Columns.Count) = "CATATAN"
        End With
    A complete example loading a WorkBook, making this change, saving and exiting:
    Code:
    Dim ex As Excel.Application
    Dim wb As Excel.Workbook
    Dim sh As Excel.Worksheet
    
        Set ex = New Excel.Application
        Set wb = ex.Workbooks.Open("C:\wb1.xls")
        Set sh = wb.Worksheets(1)
            
        With sh.UsedRange
            sh.Cells(.Row, .Column + .Columns.Count) = "CATATAN"
        End With
        
        wb.Save
        Set sh = Nothing
        wb.Close
        Set wb = Nothing
        ex.Application.Quit
        Set ex = Nothing

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Add new column in excel using Visual Basic

    It added in the first row. How to added it on the fourth rows within the tables

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Add new column in excel using Visual Basic

    Code:
    Dim ex As Excel.Application
    Dim wb As Excel.Workbook
    Dim sh As Excel.Worksheet, i As Long
    
        Set ex = New Excel.Application
        Set wb = ex.Workbooks.Open("C:\wb1.xls")
        Set sh = wb.Worksheets(1)
            
        With sh.UsedRange
            For i = .Row To .Row + .Rows.Count - 1
                sh.Cells(i, .Column + .Columns.Count) = "CATATAN"
            Next
        End With
        
        wb.Save
        Set sh = Nothing
        wb.Close
        Set wb = Nothing
        ex.Application.Quit
        Set ex = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width