Solved

Working with Range VBA

Posted on 2011-09-28
10
219 Views
Last Modified: 2012-06-22
I'm new to VBA, trying to learn as quickly as I can.  I am trying to do what I think is a simple task, but it's not working.  I simply want to select a group of cells, then change the sign (multiply by -1).  I'm not sure why.  Could someone help?

 
Sub Changesign(ByVal numsToChange As Range)
    Dim checkRange As Range
    For Each checkRange In numsToChange
    checkRange = checkRange * -1
End Sub

Open in new window

0
Comment
Question by:BBlu
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 8

Expert Comment

by:wchh
ID: 36812895
Add
Next checkrange
after checkRange = checkRange * -1
0
 
LVL 8

Accepted Solution

by:
wchh earned 100 total points
ID: 36812901
Complete code
Sub Changesign(ByVal numsToChange As Range)
    Dim checkRange As Range
    For Each checkRange In numsToChange
    checkRange = checkRange * -1
    Next checkRange '<--- Add
End Sub

Open in new window

0
 
LVL 5

Assisted Solution

by:GirardAndrew
GirardAndrew earned 100 total points
ID: 36813148
In addition to that, you also need to define checkRange as cells
Sub Changesign(ByVal numsToChange As Range)
    Dim checkRange As Range
    For Each checkRange In numsToChange.Cells '<--- Define Cells
    checkRange = checkRange * -1
    Next checkRange
End Sub

Open in new window

0
 
LVL 17

Assisted Solution

by:andrewssd3
andrewssd3 earned 100 total points
ID: 36813311
It's not essential, but I would recommend explicitly specifying the Value property for your checkRange as well.  You don't have to because it is the default property, but I think it is clearer, and better practice, and may help later if you want to come back to the code and specify a different property like Value2, etc.

Sub Changesign(ByVal numsToChange As Range)
    Dim checkRange As Range
    For Each checkRange In numsToChange.Cells '<--- Define Cells
    checkRange.Value = checkRange.Value * -1  ' <--- be explicit about the property you want
    Next checkRange
End Sub

Open in new window

0
 
LVL 31

Assisted Solution

by:Rob Henson
Rob Henson earned 100 total points
ID: 36814063
I do similar but use the Selection method:

Sub ChangeSign()

    If Application.Calculation = xlCalculationAutomatic Then
    Let CalcFlag = True

    With Application
        .Calculation = xlCalculationManual
    End With

    End If
    
    For Each cell In Selection
        If cell.Value <> "" And cell.Value <> 0 Then Let cell.Value = cell.Value * -1
    Next cell

    If CalcFlag = True Then

    With Application
        .Calculation = xlCalculationAutomatic
    End With

End If

Open in new window


Note, for all of these options if the cell contains a formula, the routine will convert it to values as well as changing the sign.

If you want to avoid doing so you can use the multiply option in the Paste Special dialogue.

Type -1 into a cell and then copy this cell.

Highlight the range which you want changing and choose Edit > Paste Special. Select the Formula radio button and then lower down select the Multiply option. This will amend the formula to add the *-1 to the end.

However, I guess if you are changing the sign of the source data, the result of the formulas will change sign anyway.

Thanks
Rob H
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:BBlu
ID: 36816982
Thank you all.  I'm not sure what I'm doing wrong here.  I've edited mine according to the first response and added another just for testing and learning.  But neither appears in my macros list when I try to run them.  See attached.


ScreenShot-ChangeSign1.png
0
 

Author Comment

by:BBlu
ID: 36816990
Not showing in Macro list
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36817653
The macros list will not show macros that take a parameter as yours does.  You need to create a macro that has no parameter and calls yours, like
Sub TestChange()
    Call ChangeSign(Selection)
End Sub

Open in new window

0
 

Author Comment

by:BBlu
ID: 36853303
Thank you all
0
 

Author Closing Comment

by:BBlu
ID: 36853763
As usual, Awesome!  Thanks to you all.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now