Solved

Create calculated column on multiple worksheets

Posted on 2011-03-21
6
233 Views
Last Modified: 2012-05-11
I need to add a calculated column to the right of the last column in several populated worksheets (in this case it is sheets 1 thru 5 from the same workbook).
I would like to create a column named Month_Year based on this formula  DATE(H2|I2|1)
That creates the same number of rows as in the column to the immediate left of the calculated column.  Worksheets 1-5 contain different numbers of columns and rows of data.
0
Comment
Question by:Ed_CLP
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:ragnarok89
Comment Utility
This should work.
Sub FindLastColumn()
'Finds last used column, copies and selects the next
Dim LastColumn As Integer
'Find last column with text
    If WorksheetFunction.CountA(Cells) > 0 Then
        'Search for any entry, by searching backwards by Columns.
        LastColumn = Cells.Find(What:="*", After:=[A1], _
                           SearchOrder:=xlByColumns, _
                       SearchDirection:=xlPrevious).Column
    End If
    
'se;ect first row in last column
cells(1,lastcolumn).select

-- do other stuff ---

End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:krishnakrkc
Comment Utility
Hi,

may be...


Kris
Sub kTest()
    Dim i As Long, r   As Long, c As Long, MySheets
    
    MySheets = Array("Sheet1", "Sheet2", "Sheet3") '<<=== add more sheets
    
    For i = 0 To UBound(MySheets)
        With Worksheets(CStr(MySheets(i)))
            r = .Range("h" & .Rows.Count).End(xlUp).Row
            c = .UsedRange.Find("*", .[a1], , , , 2).Column
            .Cells(2, c + 1).Resize(r - 1).Formula = "=date(h2,i2,1)"
        End With
    Next
    
End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:krishnakrkc
Comment Utility
Hi,

replace

c = .UsedRange.Find("*", .[a1], , , , 2).Column

with

c = .UsedRange.Find("*", .[a1], , , 2, 2).Column

Kris
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Ed_CLP
Comment Utility
@krishnakrkc - that is perfect!  Can the column title(Month_year) be set as part of the script?
0
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 500 total points
Comment Utility
Hi,

Try


Kris
Sub kTest()
    Dim i As Long, r   As Long, c As Long, MySheets
    
    MySheets = Array("Sheet1", "Sheet2", "Sheet3") '<<=== add more sheets
    
    For i = 0 To UBound(MySheets)
        With Worksheets(CStr(MySheets(i)))
            r = .Range("h" & .Rows.Count).End(xlUp).Row
            c = .UsedRange.Find("*", .[a1], , , 2, 2).Column
            .Cells(1, c + 1).Value = "Month_Year"
            .Cells(2, c + 1).Resize(r - 1).Formula = "=date(h2,i2,1)"
        End With
    Next
    
End Sub

Open in new window

0
 

Author Closing Comment

by:Ed_CLP
Comment Utility
Completely answered my question.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now