Solved

Working with Range VBA

Posted on 2011-09-28
10
223 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

Technology Partners: 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!

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

756 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