excel vba
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel - VBA

Excel - VBA

resolvedResolved · Urgent Priority · Version 2003

replyReply Tue 24 Jun 2008, 17:05Delegate Arabella said...

Arabella has attended:
Access Introduction course
Access Intermediate course
Access Advanced course

Excel - VBA

I want to select a range in one workbook and if the totel of this range is greater than 0 then 1 appears in a selected cell in another workbook
I can do if the range is just one cell

THis is the code
If Range("Test1").Value > 0 Then
I have named the range test1

If the code reads
If Range("E10").Value > 0 Then
it works but I want to include the whole column

Any help would be welcome

For upcoming training course dates see: Pricing & availability

replyReply Wed 25 Jun 2008, 10:18 Edited on Wed 25 Jun 2008, 10:20Trainer Stephen said...

RE: Excel - VBA

Hi Arabella

Thank you for your question

You need to create a module and copy the following code into it.

Sub ConditionalCopy()

Dim lngSum As Long

Application.Workbooks("Source").Activate

Sheets("Sheet1").Select

lngSum = Application.WorksheetFunction.Sum(Range("E3:E44"))

If lngSum > 0 Then

Application.Workbooks("target").Worksheets("sheet1").Range("A1").Value = 1

End If


End Sub



This code checks a range in a workbook called source and then copies 1 to a workbook called target if the sum of that range is greater then 0.

The key line of code is


lngSum = Application.WorksheetFunction.Sum(Range("E3:E44"))

This takes the sum of the range of cells that you want to check using the worksheetfunction "sum" and asigned it to a variable called lngSum

This variable is then tested to see if its value is greater than 0 and if it is then the target workbook is opened and the value 1 assigned to range("a1") in Sheet1.

Obviously you will need to rename the workbooks and worksheets

Hope this helps, let me know if you need anything further

Regards

Stephen Williams
Microsoft Office Specialist

 

Please browse our web site to find out more about
microsoft excel courses tate and other Microsoft training courses.

Excel tip:

Removing custom dictionary entries

If you add something to the custom dictionary in Excel you cannot remove it. The way to get around this is to go into word and remove it there.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard