?
Solved

Total Excel columns from VB

Posted on 2002-04-17
7
Medium Priority
?
149 Views
Last Modified: 2010-05-02

I have written a VB program that creates an EXCEL spreadsheet.  I need to SUM columns E and F and G, then total those totals.  How do I do that?

I am able to get the last row used by doing:
        Range(Selection, Selection.End(xlDown)).Select
        varRow = Mid(Selection.End(xlDown).Address, 4)

Since I am working with test data and I know how many rows I have, the following formula works:
        ActiveCell.FormulaR1C1 = "=SUM(R[-32]C:R[-1]C)"

Obviously, when I use LIVE data, I won’t know what the last row will be, so I want to use my variable in the formula:
       ActiveCell.FormulaR1C1 = "=SUM(R[varRow]C:R[-1]C)"

but I get this error:
Run-time error ‘1004’:
Application-defined or object-defined error

Am I close?  What do I need to do?

Thanks for your help
0
Comment
Question by:jhartski
[X]
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
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6948421
Firts to all, Selection object works inside VBA workspaces not in VB (Anyway is a range object)
You must to strictly qualify that object so VB could know what you are talking about when you say Selection.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6948432
something like
dim rng as range
'sh points to a variable object of type worksheet
set rng = sh.range("E1:" & range("E1").end(xldown)
0
 
LVL 44

Accepted Solution

by:
bruintje earned 200 total points
ID: 6948466
Hi Jhartski,

Richie mentioned something interesting there how are you using the objects have you named them all??

you said the Activecell formula works but not with a variable

here's a little snippet

Dim lngLastRow As Long
Dim lngFirstRow As Long
Dim lngColumn As Long

  Range("F6").Select
  lngColumn = Selection.Column
  lngFirstRow = Selection.Row
  Selection.End(xlDown).Select
  lngLastRow = Selection.Row
  ActiveCell.Offset(1, 0).Select
  ActiveCell.Value = WorksheetFunction.Sum(Range(Cells(lngFirstRow, lngColumn), Cells(lngLastRow, lngColumn)))

the Range("F6") is hypothetical since i don't know what your selection is to start with but this will sum from the selected cell to the last and add the total below it

:O)Bruintje
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6948515
or
dim LastCell as string
LastCell=range("E1:" & range("E1").end(xldown).address)
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6948559
well thanks, glad it did work, then was this VBA or something
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6948943
btw Richie congrats! top-15 now i want to c u go for the top-5 :)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6948956
Thanks!
Well, if you continue collaborating this way i doubt!
:)))))
0

Featured Post

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.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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