#1   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 10
Default macro ?

Tab one is 2006, tab two is 2007.. Tab 3 is difference.
Tab 1 (2006) will have text then $$ in columns C to O (12 columns and
total ), Tab 2 will have text then $$ in columns C to O ( 12 columns and
total).
Tab 3 will have Tab 1 less Tab 2 in columns C to O.
Pretty much a standard worksheet BUT there will be abut 200 worksheets and
it seems to me to be easier with a macro that will fill in the rows
depending on what is in column C on tab 1 or 2.
The worksheets will be different length, but the last line will read
"Total non-salary" ..
Any help would be appreciated as to getting this done without 5 minutes a
worksheet or about 16 hours..


  #3   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 3,365
Default macro ?

I agree with Don Guillett - not clear. You mention only 3 sheets, how do we
get from those to the other 197?
Or are you talking about workBOOKs -
Workbook: Excel file with 1 or more worksheets in it
Worksheet: a single tab within a workbook.

Plus you say "...fill in the rows depending on what is in column C on tab 1
or 2."
fill in what rows on what tab? What type of data is in column C on tabs 1
and 2 (from earlier statement, sounds like it should be dollar values).

What I *think* I hear you asking for is a macro that will open each of the
200 workbooks, calculate or place formulas in Tab 3 that will have the
differences between tabs 1 and 2 in that sheet, close the workbook and move
on to the next?


"jlmccabes" wrote:

Tab one is 2006, tab two is 2007.. Tab 3 is difference.
Tab 1 (2006) will have text then $$ in columns C to O (12 columns and
total ), Tab 2 will have text then $$ in columns C to O ( 12 columns and
total).
Tab 3 will have Tab 1 less Tab 2 in columns C to O.
Pretty much a standard worksheet BUT there will be abut 200 worksheets and
it seems to me to be easier with a macro that will fill in the rows
depending on what is in column C on tab 1 or 2.
The worksheets will be different length, but the last line will read
"Total non-salary" ..
Any help would be appreciated as to getting this done without 5 minutes a
worksheet or about 16 hours..



  #4   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 3,365
Default macro ?

If I guessed right in my previous post about your needs, then you should be
able to adapt this code to your needs. To use it, open a brand new workbook
in Excel, use:
[Alt]+[F11] to get into the VB Editor, then from its menu bar, use Insert |
Module to create a new code module. Copy the code below and paste it into
the module.

Save that workbook into the same folder with all of the 200 workbooks to be
modified and then run the macro.

This code assumes that the workbook with the code and all of the other
workbooks are in the same folder (shouldn't be any other .xls files in that
folder - they could get corrupted or code could fail). It also assumes that
the names of the 3 worksheets in those other workbooks are all the same:
2006, 2007 and some other name you didn't specify ... you can alter the code
to provide that 3rd sheet's name.

Sub RecordDifferences()
'to use this macro
'Create a folder that only contains this workbook
'and the 200 other workbooks that you have to work with
'Open this workbook and run this macro
'ASSUMPTIONS:
' the 3 worksheets involved have the same name
' in all workbooks to be used
' that you want to copy text from col C to
' the 3rd tab sheet along with putting
' values into cells in columns D-O there.
'
'change these constants as required
'
'names of sheets involved
'change names here as required
Const tab1Name = "2006"
Const tab2Name = "2007"
Const tab3Name = "Differences"
'column where phrase 'Total non-salary' occurs
Const phraseCol = "C"
'first column with numeric data to be used
Const firstDataCol = "D"
'column with last numeric entry
Const lastCol = "O"
'row with 1st set of numbers to work with
Const firstDataRow = 1 ' assumes no labels in row 1
'end of user change area

Dim basicPath As String
Dim anyFile As String
Dim lastDataRow As Long
Dim WS1 As Range ' will be tab1 C1
Dim WS2 As Range ' will be tab2 C1
Dim WS3 As Range ' will be tab3 C1
Dim offsetToLastColumn As Integer
Dim cOffset As Integer
Dim rOffset As Long

offsetToLastColumn = Range(lastCol & "1").Column - _
Range(phraseCol & "1").Column

'find the path to the folder that this workbook
'and all the others is/are stored in.
basicPath = Left(ThisWorkbook.FullName, _
InStrRev(ThisWorkbook.FullName, "\"))
' get first filename
anyFile = Dir$(basicPath & "*.xls")
' speed things up
Application.ScreenUpdating = False
'begin the real work
Do While anyFile < ""
If anyFile < ThisWorkbook.Name And _
UCase(Right(anyFile, 4)) = ".XLS" Then
'open the other workbook, it becomes active
Workbooks.Open basicPath & anyFile
Set WS1 = Workbooks(anyFile).Worksheets(tab1Name). _
Range(phraseCol & firstDataRow)
Set WS2 = Workbooks(anyFile).Worksheets(tab2Name). _
Range(phraseCol & firstDataRow)
Set WS3 = Workbooks(anyFile).Worksheets(tab3Name). _
Range(phraseCol & firstDataRow)
'find row with "Total non-salary" in it
lastDataRow = _
Workbooks(anyFile).Worksheets(tab1Name). _
Range(phraseCol & Rows.Count).End(xlUp).Row - 1
cOffset = 0 ' initialize/clear
rOffset = 0 ' initialize/clear
For cOffset = 0 To offsetToLastColumn
For rOffset = 0 To lastDataRow
If cOffset 0 Then
'handles numeric values

'if you want hard values use this
'(remove ' in front of WS3.... and
'either comment or remove the formula
'creating statement below).
'WS3.Offset(rOffset, cOffset).Value = _
WS1.Offset(rOffset, cOffset).Value - _
WS2.Offset(rOffset, cOffset).Value

'if you want formulas, use this
'(which will override hard values above unless
'you comment/delete this next statement
'I believe this will be better for you, since
'you're working with 2007 values, and 2007 is
'not over with just yet!

WS3.Offset(rOffset, cOffset).Formula = _
"='" & tab1Name & "'!" & _
WS1.Offset(rOffset, cOffset).Address & _
" - '" & tab2Name & "'!" & _
WS2.Offset(rOffset, cOffset).Address
Else
'just copies from column C in WS1 to C in WS3
WS3.Offset(rOffset, cOffset).Value = _
WS1.Offset(rOffset, cOffset).Value
End If
Next ' rOffset loop end
Next ' cOffset loop end
ActiveWorkbook.Close True ' save with changes
End If ' filename test
anyFile = Dir$ ' get next file name
Loop ' file retrieval loop end
Set WS1 = Nothing
Set WS2 = Nothing
Set WS3 = Nothing
Application.ScreenUpdating = True
End Sub


"JLatham" wrote:

I agree with Don Guillett - not clear. You mention only 3 sheets, how do we
get from those to the other 197?
Or are you talking about workBOOKs -
Workbook: Excel file with 1 or more worksheets in it
Worksheet: a single tab within a workbook.

Plus you say "...fill in the rows depending on what is in column C on tab 1
or 2."
fill in what rows on what tab? What type of data is in column C on tabs 1
and 2 (from earlier statement, sounds like it should be dollar values).

What I *think* I hear you asking for is a macro that will open each of the
200 workbooks, calculate or place formulas in Tab 3 that will have the
differences between tabs 1 and 2 in that sheet, close the workbook and move
on to the next?


"jlmccabes" wrote:

Tab one is 2006, tab two is 2007.. Tab 3 is difference.
Tab 1 (2006) will have text then $$ in columns C to O (12 columns and
total ), Tab 2 will have text then $$ in columns C to O ( 12 columns and
total).
Tab 3 will have Tab 1 less Tab 2 in columns C to O.
Pretty much a standard worksheet BUT there will be abut 200 worksheets and
it seems to me to be easier with a macro that will fill in the rows
depending on what is in column C on tab 1 or 2.
The worksheets will be different length, but the last line will read
"Total non-salary" ..
Any help would be appreciated as to getting this done without 5 minutes a
worksheet or about 16 hours..



  #5   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 10,124
Default macro ?

You might like this idea better
sub doworkbooks
for each wb in workbooks
wb.open
domath
next wb
end sub

sub eachwb
fNames = Dir("*.xls")

Do While FNames < ""
Set mybook = Workbooks.Open(FNames)


do math

mybook.Close true
FNames = Dir()
Loop


Sub domath()
With ActiveWorkbook
lr1 = Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lr2 = Sheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Row
lr = Application.Max(lr1, lr2)
Set frng = Sheets("sheet3").Range("a2:a" & lr)
With frng
.Formula = "=Sheet1!A2-Sheet2!A2"
.Formula = .Value
End With
End With
End Sub

--
Don Guillett
SalesAid Software

"JLatham" <HelpFrom @ Jlathamsite.com.(removethis) wrote in message
...
If I guessed right in my previous post about your needs, then you should
be
able to adapt this code to your needs. To use it, open a brand new
workbook
in Excel, use:
[Alt]+[F11] to get into the VB Editor, then from its menu bar, use Insert
|
Module to create a new code module. Copy the code below and paste it into
the module.

Save that workbook into the same folder with all of the 200 workbooks to
be
modified and then run the macro.

This code assumes that the workbook with the code and all of the other
workbooks are in the same folder (shouldn't be any other .xls files in
that
folder - they could get corrupted or code could fail). It also assumes
that
the names of the 3 worksheets in those other workbooks are all the same:
2006, 2007 and some other name you didn't specify ... you can alter the
code
to provide that 3rd sheet's name.

Sub RecordDifferences()
'to use this macro
'Create a folder that only contains this workbook
'and the 200 other workbooks that you have to work with
'Open this workbook and run this macro
'ASSUMPTIONS:
' the 3 worksheets involved have the same name
' in all workbooks to be used
' that you want to copy text from col C to
' the 3rd tab sheet along with putting
' values into cells in columns D-O there.
'
'change these constants as required
'
'names of sheets involved
'change names here as required
Const tab1Name = "2006"
Const tab2Name = "2007"
Const tab3Name = "Differences"
'column where phrase 'Total non-salary' occurs
Const phraseCol = "C"
'first column with numeric data to be used
Const firstDataCol = "D"
'column with last numeric entry
Const lastCol = "O"
'row with 1st set of numbers to work with
Const firstDataRow = 1 ' assumes no labels in row 1
'end of user change area

Dim basicPath As String
Dim anyFile As String
Dim lastDataRow As Long
Dim WS1 As Range ' will be tab1 C1
Dim WS2 As Range ' will be tab2 C1
Dim WS3 As Range ' will be tab3 C1
Dim offsetToLastColumn As Integer
Dim cOffset As Integer
Dim rOffset As Long

offsetToLastColumn = Range(lastCol & "1").Column - _
Range(phraseCol & "1").Column

'find the path to the folder that this workbook
'and all the others is/are stored in.
basicPath = Left(ThisWorkbook.FullName, _
InStrRev(ThisWorkbook.FullName, "\"))
' get first filename
anyFile = Dir$(basicPath & "*.xls")
' speed things up
Application.ScreenUpdating = False
'begin the real work
Do While anyFile < ""
If anyFile < ThisWorkbook.Name And _
UCase(Right(anyFile, 4)) = ".XLS" Then
'open the other workbook, it becomes active
Workbooks.Open basicPath & anyFile
Set WS1 = Workbooks(anyFile).Worksheets(tab1Name). _
Range(phraseCol & firstDataRow)
Set WS2 = Workbooks(anyFile).Worksheets(tab2Name). _
Range(phraseCol & firstDataRow)
Set WS3 = Workbooks(anyFile).Worksheets(tab3Name). _
Range(phraseCol & firstDataRow)
'find row with "Total non-salary" in it
lastDataRow = _
Workbooks(anyFile).Worksheets(tab1Name). _
Range(phraseCol & Rows.Count).End(xlUp).Row - 1
cOffset = 0 ' initialize/clear
rOffset = 0 ' initialize/clear
For cOffset = 0 To offsetToLastColumn
For rOffset = 0 To lastDataRow
If cOffset 0 Then
'handles numeric values

'if you want hard values use this
'(remove ' in front of WS3.... and
'either comment or remove the formula
'creating statement below).
'WS3.Offset(rOffset, cOffset).Value = _
WS1.Offset(rOffset, cOffset).Value - _
WS2.Offset(rOffset, cOffset).Value

'if you want formulas, use this
'(which will override hard values above unless
'you comment/delete this next statement
'I believe this will be better for you, since
'you're working with 2007 values, and 2007 is
'not over with just yet!

WS3.Offset(rOffset, cOffset).Formula = _
"='" & tab1Name & "'!" & _
WS1.Offset(rOffset, cOffset).Address & _
" - '" & tab2Name & "'!" & _
WS2.Offset(rOffset, cOffset).Address
Else
'just copies from column C in WS1 to C in WS3
WS3.Offset(rOffset, cOffset).Value = _
WS1.Offset(rOffset, cOffset).Value
End If
Next ' rOffset loop end
Next ' cOffset loop end
ActiveWorkbook.Close True ' save with changes
End If ' filename test
anyFile = Dir$ ' get next file name
Loop ' file retrieval loop end
Set WS1 = Nothing
Set WS2 = Nothing
Set WS3 = Nothing
Application.ScreenUpdating = True
End Sub


"JLatham" wrote:

I agree with Don Guillett - not clear. You mention only 3 sheets, how do
we
get from those to the other 197?
Or are you talking about workBOOKs -
Workbook: Excel file with 1 or more worksheets in it
Worksheet: a single tab within a workbook.

Plus you say "...fill in the rows depending on what is in column C on tab
1
or 2."
fill in what rows on what tab? What type of data is in column C on tabs
1
and 2 (from earlier statement, sounds like it should be dollar values).

What I *think* I hear you asking for is a macro that will open each of
the
200 workbooks, calculate or place formulas in Tab 3 that will have the
differences between tabs 1 and 2 in that sheet, close the workbook and
move
on to the next?


"jlmccabes" wrote:

Tab one is 2006, tab two is 2007.. Tab 3 is difference.
Tab 1 (2006) will have text then $$ in columns C to O (12 columns and
total ), Tab 2 will have text then $$ in columns C to O ( 12 columns
and
total).
Tab 3 will have Tab 1 less Tab 2 in columns C to O.
Pretty much a standard worksheet BUT there will be abut 200 worksheets
and
it seems to me to be easier with a macro that will fill in the rows
depending on what is in column C on tab 1 or 2.
The worksheets will be different length, but the last line will read
"Total non-salary" ..
Any help would be appreciated as to getting this done without 5 minutes
a
worksheet or about 16 hours..






  #6   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 3,365
Default macro ?

The domath part is probably a lot faster!

One correction: needs End Sub to close Sub eachwb()

Of course, this all presumes I guessed right about there being 200 workbooks
involved in all of this.

"Don Guillett" wrote:

You might like this idea better
sub doworkbooks
for each wb in workbooks
wb.open
domath
next wb
end sub

sub eachwb
fNames = Dir("*.xls")

Do While FNames < ""
Set mybook = Workbooks.Open(FNames)


do math

mybook.Close true
FNames = Dir()
Loop


Sub domath()
With ActiveWorkbook
lr1 = Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lr2 = Sheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Row
lr = Application.Max(lr1, lr2)
Set frng = Sheets("sheet3").Range("a2:a" & lr)
With frng
.Formula = "=Sheet1!A2-Sheet2!A2"
.Formula = .Value
End With
End With
End Sub

--
Don Guillett
SalesAid Software

"JLatham" <HelpFrom @ Jlathamsite.com.(removethis) wrote in message
...
If I guessed right in my previous post about your needs, then you should
be
able to adapt this code to your needs. To use it, open a brand new
workbook
in Excel, use:
[Alt]+[F11] to get into the VB Editor, then from its menu bar, use Insert
|
Module to create a new code module. Copy the code below and paste it into
the module.

Save that workbook into the same folder with all of the 200 workbooks to
be
modified and then run the macro.

This code assumes that the workbook with the code and all of the other
workbooks are in the same folder (shouldn't be any other .xls files in
that
folder - they could get corrupted or code could fail). It also assumes
that
the names of the 3 worksheets in those other workbooks are all the same:
2006, 2007 and some other name you didn't specify ... you can alter the
code
to provide that 3rd sheet's name.

Sub RecordDifferences()
'to use this macro
'Create a folder that only contains this workbook
'and the 200 other workbooks that you have to work with
'Open this workbook and run this macro
'ASSUMPTIONS:
' the 3 worksheets involved have the same name
' in all workbooks to be used
' that you want to copy text from col C to
' the 3rd tab sheet along with putting
' values into cells in columns D-O there.
'
'change these constants as required
'
'names of sheets involved
'change names here as required
Const tab1Name = "2006"
Const tab2Name = "2007"
Const tab3Name = "Differences"
'column where phrase 'Total non-salary' occurs
Const phraseCol = "C"
'first column with numeric data to be used
Const firstDataCol = "D"
'column with last numeric entry
Const lastCol = "O"
'row with 1st set of numbers to work with
Const firstDataRow = 1 ' assumes no labels in row 1
'end of user change area

Dim basicPath As String
Dim anyFile As String
Dim lastDataRow As Long
Dim WS1 As Range ' will be tab1 C1
Dim WS2 As Range ' will be tab2 C1
Dim WS3 As Range ' will be tab3 C1
Dim offsetToLastColumn As Integer
Dim cOffset As Integer
Dim rOffset As Long

offsetToLastColumn = Range(lastCol & "1").Column - _
Range(phraseCol & "1").Column

'find the path to the folder that this workbook
'and all the others is/are stored in.
basicPath = Left(ThisWorkbook.FullName, _
InStrRev(ThisWorkbook.FullName, "\"))
' get first filename
anyFile = Dir$(basicPath & "*.xls")
' speed things up
Application.ScreenUpdating = False
'begin the real work
Do While anyFile < ""
If anyFile < ThisWorkbook.Name And _
UCase(Right(anyFile, 4)) = ".XLS" Then
'open the other workbook, it becomes active
Workbooks.Open basicPath & anyFile
Set WS1 = Workbooks(anyFile).Worksheets(tab1Name). _
Range(phraseCol & firstDataRow)
Set WS2 = Workbooks(anyFile).Worksheets(tab2Name). _
Range(phraseCol & firstDataRow)
Set WS3 = Workbooks(anyFile).Worksheets(tab3Name). _
Range(phraseCol & firstDataRow)
'find row with "Total non-salary" in it
lastDataRow = _
Workbooks(anyFile).Worksheets(tab1Name). _
Range(phraseCol & Rows.Count).End(xlUp).Row - 1
cOffset = 0 ' initialize/clear
rOffset = 0 ' initialize/clear
For cOffset = 0 To offsetToLastColumn
For rOffset = 0 To lastDataRow
If cOffset 0 Then
'handles numeric values

'if you want hard values use this
'(remove ' in front of WS3.... and
'either comment or remove the formula
'creating statement below).
'WS3.Offset(rOffset, cOffset).Value = _
WS1.Offset(rOffset, cOffset).Value - _
WS2.Offset(rOffset, cOffset).Value

'if you want formulas, use this
'(which will override hard values above unless
'you comment/delete this next statement
'I believe this will be better for you, since
'you're working with 2007 values, and 2007 is
'not over with just yet!

WS3.Offset(rOffset, cOffset).Formula = _
"='" & tab1Name & "'!" & _
WS1.Offset(rOffset, cOffset).Address & _
" - '" & tab2Name & "'!" & _
WS2.Offset(rOffset, cOffset).Address
Else
'just copies from column C in WS1 to C in WS3
WS3.Offset(rOffset, cOffset).Value = _
WS1.Offset(rOffset, cOffset).Value
End If
Next ' rOffset loop end
Next ' cOffset loop end
ActiveWorkbook.Close True ' save with changes
End If ' filename test
anyFile = Dir$ ' get next file name
Loop ' file retrieval loop end
Set WS1 = Nothing
Set WS2 = Nothing
Set WS3 = Nothing
Application.ScreenUpdating = True
End Sub


"JLatham" wrote:

I agree with Don Guillett - not clear. You mention only 3 sheets, how do
we
get from those to the other 197?
Or are you talking about workBOOKs -
Workbook: Excel file with 1 or more worksheets in it
Worksheet: a single tab within a workbook.

Plus you say "...fill in the rows depending on what is in column C on tab
1
or 2."
fill in what rows on what tab? What type of data is in column C on tabs
1
and 2 (from earlier statement, sounds like it should be dollar values).

What I *think* I hear you asking for is a macro that will open each of
the
200 workbooks, calculate or place formulas in Tab 3 that will have the
differences between tabs 1 and 2 in that sheet, close the workbook and
move
on to the next?


"jlmccabes" wrote:

Tab one is 2006, tab two is 2007.. Tab 3 is difference.
Tab 1 (2006) will have text then $$ in columns C to O (12 columns and
total ), Tab 2 will have text then $$ in columns C to O ( 12 columns
and
total).
Tab 3 will have Tab 1 less Tab 2 in columns C to O.
Pretty much a standard worksheet BUT there will be abut 200 worksheets
and
it seems to me to be easier with a macro that will fill in the rows
depending on what is in column C on tab 1 or 2.
The worksheets will be different length, but the last line will read
"Total non-salary" ..
Any help would be appreciated as to getting this done without 5 minutes
a
worksheet or about 16 hours..





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 1 February 5th 07 09:31 PM
using a cell value to control a counter inside a macro and displaying macro value ocset Excel Worksheet Functions 1 September 10th 06 05:32 AM
Macro needed to Paste Values and prevent Macro operation thunderfoot Excel Discussion (Misc queries) 1 June 11th 05 12:44 AM
Macro needed to Paste Values and prevent Macro operation thunderfoot Excel Discussion (Misc queries) 0 June 10th 05 03:38 PM


All times are GMT +1. The time now is 02:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"