Ignore cell with text, dicide the rest by 1000 with condition

LazySun

New Member
Joined
Feb 10, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I have an Excel table converted from PDF. It contains text and range of number. When I use the 1st procedure, if I understand correctly, it should select cell with constant to include in the result? Somehow I still include the text part? Using the 2nd code is okay but It took long time to edit the table.

Sub DividedbyThousand()

Dim ConstantCells As Range

Dim cell As Range

Set ConstantCells = Selection.SpecialCells(xlCellTypeConstants)

For Each cell In ConstantCells

If cell.Value > -100 Then

cell.Value = cell.Value / 1000

End If

Next cell

For Each cell In ConstantCells

If cell.Value < -100 Then

cell.Value = cell.Value / 1000

End If

Next cell



End Sub

Sub DividedbyThousandv2()

Dim cell As Range



For Each cell In Selection

If cell.Value > 100 Then

cell.Value = cell.Value / 1000

ElseIf cell.Value < -100 Then

cell.Value = cell.Value / 1000

End If

Next cell



End Sub
 

Attachments

  • Error.png
    Error.png
    33.6 KB · Views: 7

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
cell with text is known as constant and greater then any value.
Try below code, with only 1 loop by using ABS:
VBA Code:
Sub DividedbyThousand()
Dim ConstantCells As Range
Dim cell As Range
Set ConstantCells = Selection.SpecialCells(xlCellTypeConstants)
    For Each cell In ConstantCells
        If IsNumeric(cell.Value) Then ' work with cell with value only
            If Abs(cell.Value) > 100 Then cell.Value = cell.Value / 1000 ' if cell<-100 or cell >100
        End If
    Next
End Sub
 
Upvote 0
Welcome to the MrExcel board!

With SpecialCells(xlCellTypeConstants) you can specify the type of constants by adding a second argument, thereby eliminating the text values. That would be an easy change to your existing macro and you would not have to test each cell to see if it is a number. :)

Rich (BB code):
 Set ConstantCells = Selection.SpecialCells(xlCellTypeConstants, xlNumbers)

BTW, when posting vba code in the forum, please use the available code tags. Then your code retains the indentation etc of your actual code like in your image and like the code in post #2, making it much easier to read &/or debug. My signature block below has more details.
 
Upvote 0
Another option you could consider is to process them all at once, rather than looping one at a time.
Test with a copy of your data.

VBA Code:
Sub Divide1000()
  With Selection
    .Value = Evaluate(Replace("if(#="""","""",if(istext(#),#,if(abs(#)<=100,#,#/1000)))", "#", .Address))
  End With
End Sub
 
Upvote 0
Welcome to the MrExcel board!

With SpecialCells(xlCellTypeConstants) you can specify the type of constants by adding a second argument, thereby eliminating the text values. That would be an easy change to your existing macro and you would not have to test each cell to see if it is a number. :)

Rich (BB code):
 Set ConstantCells = Selection.SpecialCells(xlCellTypeConstants, xlNumbers)

BTW, when posting vba code in the forum, please use the available code tags. Then your code retains the indentation etc of your actual code like in your image and like the code in post #2, making it much easier to read &/or debug. My sddignature block below has more details.
Thanks for the reply, I will follow the guidelines for next post.
 
Upvote 0
No problem.

Did you compare the 'Evaluate' method for speed?
I did find an improvement in dividing by 1000 with my version as below. Sorry for late reply due to many tasks.
Code:
Sub DividedbyThousand()

Dim ConstantCells As Range

Dim cell As Range

Set ConstantCells = Selection.SpecialCells(xlCellTypeConstants)

For Each cell In ConstantCells

If cell.Value > 100 Then

cell.Value = cell.Value / 1000

End If

Next cell

For Each cell In ConstantCells

If cell.Value < 100 Then

cell.Value = cell.Value / 1000

End If

Next cell

End Sub



Thank you all of you for your kind support.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,215,042
Messages
6,122,810
Members
449,095
Latest member
m_smith_solihull

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