|
-
Apr 18th, 2012, 09:49 AM
#1
Thread Starter
Frenzied Member
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.
-
Apr 18th, 2012, 11:13 AM
#2
Re: Add new column in excel using Visual Basic
excel already has more columns, do u mean to hide / unhide column?
-
Apr 18th, 2012, 11:46 AM
#3
Thread Starter
Frenzied Member
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.
-
Apr 18th, 2012, 08:10 PM
#4
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
-
Apr 21st, 2012, 07:16 AM
#5
Thread Starter
Frenzied Member
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
-
Apr 21st, 2012, 07:27 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|