Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Create calculated column on multiple worksheets

Posted on 2011-03-21
6
236 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
ID: 35181524
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
ID: 35181715
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
ID: 35181752
Hi,

replace

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

with

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

Kris
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:Ed_CLP
ID: 35182019
@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
ID: 35182092
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
ID: 35182206
Completely answered my question.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

861 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