VBA code to divide range of cells by 1000 excluding blank and formula cells

ArslanButt

New Member
Joined
Nov 10, 2018
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi every one, i am newly registered but have been a vivid follower of this forum since last couple of year. Can anyone write a short code as i need to divide range of cells by 1000 but exclude blank and formula cells ? Any help will be highly appreciated.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Code:
Sub div()
Dim cel As Range
On Error Resume Next
For Each cel In Selection.SpecialCells(xlCellTypeConstants, 1)
    cel = cel / 1000
Next
On Error GoTo 0
End Sub
 
Upvote 0
Thanks. It works like a charm


Code:
Sub div()
Dim cel As Range
On Error Resume Next
For Each cel In Selection.SpecialCells(xlCellTypeConstants, 1)
    cel = cel / 1000
Next
On Error GoTo 0
End Sub
 
Upvote 0
Code:
Sub div()
Dim cel As Range
On Error Resume Next
For Each cel In Selection.SpecialCells(xlCellTypeConstants, 1)
    cel = cel / 1000
Next
On Error GoTo 0
End Sub
You do not have to process each cell individually... you can process each area (rectangular group of numeric constants) in the selection separately no matter how many cells are in each area. Note that the structure is almost identical to yours... the only difference is the specifying of the Areas property for the SpecialCells method.
Code:
Sub DivideBy1000()
  Dim Ar As Range
  For Each Ar In Selection.SpecialCells(xlConstants).[B][COLOR="#0000FF"]Areas[/COLOR][/B]
    Ar = Ar / 1000
  Next
End Sub
 
Last edited:
Upvote 0
You do not have to process each cell individually... you can process each area (rectangular group of numeric constants) in the selection separately no matter how many cells are in each area. Note that the structure is almost identical to yours... the only difference is the specifying of the Areas property for the SpecialCells method.
Code:
Sub DivideBy1000()
  Dim Ar As Range
  For Each Ar In Selection.SpecialCells(xlConstants).[B][COLOR=#0000FF]Areas[/COLOR][/B]
    Ar = Ar / 1000
  Next
End Sub

If you loop areas instead of cells, you still need to restrict the specialcells to numbers only, and include an error handler.
 
Upvote 0
You do not have to process each cell individually... you can process each area (rectangular group of numeric constants)
Doesn't work for me.

Maybe this :
Code:
Sub DivideBy1000()
Dim Ar As Range
On Error Resume Next
For Each Ar In Selection.SpecialCells(xlConstants, 1).Areas
    Ar = Evaluate(Ar.Address & "/ 1000")
Next
End Sub
 
Last edited:
Upvote 0
If you loop areas instead of cells, you still need to restrict the specialcells to numbers only, and include an error handler.

Whoops! I forgot to include the second argument to the SpecialCells function to handle that problem (I don't think you need an error handler when it is included)...
Code:
Sub DivideBy1000()
  Dim Ar As Range
  For Each Ar In Selection.SpecialCells(xlConstants[B][COLOR="#FF0000"], xlNumbers[/COLOR][/B]).Areas
    Ar = Ar / 1000
  Next
End Sub
 
Last edited:
Upvote 0
I don't think you need an error handler when it is included

Hi Rick, you do for me if there is only text (including numbers formatted as text) or blanks in the selection (i.e. no numbers).
 
Upvote 0
Hi Rick, you do for me if there is only text (including numbers formatted as text) or blanks in the selection (i.e. no numbers).
When I include a mix of real numbers, text numbers, text with no numbers and blanks cells within my selection, the code I posted in Message #7 divides the real numbers by 1000, leaves everything else alone and does not issue an error messages at all... is it really working differently for you? If so, what version of Excel are you using (I am using XL2010)?
 
Upvote 0
When I include a mix of real numbers, text numbers, text with no numbers and blanks cells within my selection, the code I posted in Message #7 divides the real numbers by 1000, leaves everything else alone and does not issue an error messages at all... is it really working differently for you? If so, what version of Excel are you using (I am using XL2010)?

As stated...
you do for me if there is only text (including numbers formatted as text) or blanks in the selection (i.e. no numbers).
If there are no real numbers in the range it does error.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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
Back
Top