Solved

Working with Range VBA

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

932 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

14 Experts available now in Live!

Get 1:1 Help Now