Solved

Working with Range VBA

Posted on 2011-09-28
10
226 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
[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
  • 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
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 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 33

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
 

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

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

615 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