Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Working with Range VBA

Posted on 2011-09-28
10
Medium Priority
?
228 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 400 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 400 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
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!

 
LVL 17

Assisted Solution

by:andrewssd3
andrewssd3 earned 400 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 400 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

Independent Software Vendors: 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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

722 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