View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_5_] Jim Thomlinson[_5_] is offline
external usenet poster
 
Posts: 486
Default macro total for colums with changing # of rows

I removed the selects to clean things up a bit. I was not sure what to do if
the columns did not match so I just popped a message box...

Sub test()
ColumnTotal "f", Sheets("1")
ColumnTotal "g", Sheets("1")
ColumnTotal "h", Sheets("1")
ColumnTotal "i", Sheets("1")
ColumnTotal "H", Sheets("2")
ColumnTotal "I", Sheets("2")
ColumnTotal "J", Sheets("2")
ColumnTotal "K", Sheets("2")
If CompareColumns("f", Sheets("1"), "H", Sheets("2")) = False Then _
MsgBox "Error1"
If CompareColumns("g", Sheets("1"), "I", Sheets("2")) = False Then _
MsgBox "Error2"
If CompareColumns("h", Sheets("1"), "J", Sheets("2")) = False Then _
MsgBox "Error3"
If CompareColumns("i", Sheets("1"), "K", Sheets("2")) = False Then _
MsgBox "Error4"

End Sub

Sub ColumnTotal(ByVal strColumn As String, ByVal wks As Worksheet)
wks.Cells(wks.Rows.Count, strColumn).End(xlUp).Offset(2, 0).Value = _
cApplication.Sum(wks.Columns(strColumn))
End Sub

Function CompareColumns(ByVal strColumn1 As String, ByVal wks1 As Worksheet, _
ByVal strColumn2 As String, ByVal wks2 As Worksheet) As
Boolean
If wks1.Cells(Rows.Count, strColumn1).End(xlUp).Value = _
wks2.Cells(Rows.Count, strColumn2).End(xlUp).Value Then
CompareColumns = True
Else
CompareColumns = False
End If
End Function
--
HTH...

Jim Thomlinson


"BillyRogers" wrote:

Jim, thanks that works great. I added it to my macro with a few
adjustments(increasing offset to 2 so there is a space between the data and
totals).

I have another question. I have this running on two separate sheets in the
same workbook. I've made adjustments so that it works. I need to copy the
results from the first sheet and past them right below the totals for the
second sheet to compare them.(they are supposed to be equal). is there a way
to do this.

heres what i have so far

Sheets("1").Select
ColumnTotal "f"
ColumnTotal "g"
ColumnTotal "h"
ColumnTotal "i"
Sheets("2").Select
ColumnTotal "H"
ColumnTotal "I"
ColumnTotal "J"
ColumnTotal "K"

End Sub

Sub ColumnTotal(ByVal strColumn As String)
Cells(Rows.Count, strColumn).End(xlUp).Offset(2, 0).Value = _
Application.Sum(Columns(strColumn))
End Sub