VBA Question

kbeltle

New Member
Joined
Mar 19, 2009
Messages
27
I am using the following macro to allow me to select a large range of numbers with my mouse, and then convert them to negative numbers. What bothers me is that if there is a blank cell in the range it puts a "-" in that cell.

How do I program this macro to skip a cell if it is blank?

Thanks,
Kyle

----------------------------------------------------------------------------
Sub Convert_to_Negative_Numbers()
Set r = Application.InputBox(Prompt:="Select The Numbers You Would Like to Convert:", Type:=8)
r.Select
Dim rep As String
For Each c In Selection.Cells
rep = Left(c, 0) & "-" & c
c.Value = rep
Next c
Selection.Style = "Comma"
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

does this give expected results?

Code:
Sub Convert_to_Negative_Numbers()
Set r = Application.InputBox(Prompt:="Select The Numbers You Would Like to Convert:", Type:=8)
r.Select
Dim rep As String
For Each C In Selection.Cells
If C <> 0 Then
    rep = Left(C, 0) & "-" & C
    C.Value = rep
    C.Style = "Comma"
End If
Next C
End Sub
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub Convert_to_Negative_Numbers()
Dim r As Range, c As Range
Set r = Application.InputBox(Prompt:="Select The Numbers You Would Like to Convert:", Type:=8)
For Each c In r
    If c.Value <> "" Then c.Value = -c.Value
Next c
r.Style = "Comma"
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,587
Messages
5,987,508
Members
440,098
Latest member
MickyMouse123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top