Macro Divide Cells In a Range by Value

shawnjfulton

New Member
Joined
Mar 27, 2013
Messages
5
Hi, and thank you in advance for your help. This is probably pretty simple for someone who knows what they are doing!

I have a range, K4:W63. I want to divide each cell, by 1024. For Example =K4/1024; and have that value placed in the cell of K4.

Thanks again for your help!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
shawnjfulton,

Welcome to the MrExcel forum.

Sample raw data (not all rows are shown for brevity):


Excel 2007
KLMNOPQRSTUVW
412345678910111213
512345678910111213
612345678910111213
712345678910111213
812345678910111213
912345678910111213
1012345678910111213
Sheet1


After the macro using an array in memory:


Excel 2007
KLMNOPQRSTUVW
40.0009770.0019530.002930.0039060.0048830.0058590.0068360.0078130.0087890.0097660.0107420.0117190.012695
50.0009770.0019530.002930.0039060.0048830.0058590.0068360.0078130.0087890.0097660.0107420.0117190.012695
60.0009770.0019530.002930.0039060.0048830.0058590.0068360.0078130.0087890.0097660.0107420.0117190.012695
70.0009770.0019530.002930.0039060.0048830.0058590.0068360.0078130.0087890.0097660.0107420.0117190.012695
80.0009770.0019530.002930.0039060.0048830.0058590.0068360.0078130.0087890.0097660.0107420.0117190.012695
90.0009770.0019530.002930.0039060.0048830.0058590.0068360.0078130.0087890.0097660.0107420.0117190.012695
100.0009770.0019530.002930.0039060.0048830.0058590.0068360.0078130.0087890.0097660.0107420.0117190.012695
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub DivideBy1024()
' hiker95, 03/27/2013
' http://www.mrexcel.com/forum/excel-questions/693943-macro-divide-cells-range-value.html
Dim k As Variant, i As Long, ii As Long
k = Range("K4:W63").Value
For i = 1 To UBound(k, 1)
  For ii = 1 To UBound(k, 2)
    k(i, ii) = k(i, ii) / 1024
  Next ii
Next i
Range("K4:W63").Value = k
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the DivideBy1024 macro.
 
Upvote 0
Thank you very much. It seems that this will work. However, when I run this, I receive an error. In this range I have a some cells that contain "N/A". Is there some code to insert to ignore the cells with text?
 
Upvote 0
Argh!! Why all that coding? Here's the easy way:
  1. Go to a blank cell and enter 1024.
  2. Hit Ctrl-C to copy that one cell.
  3. Select K4:W63.
  4. On the Home ribbon drop-down the Paste button and choose Paste Special
  5. Under Paste choose Values, and under Operation choose Divide.
  6. Click OK.
 
Upvote 0
shawnjfulton,

In the future when asking for help you should provide screenshots of before and after so that we can see exactly what we are working with, and, so that we can get it right the first time.

What version of Excel are you using?


I will need to see your workbook/worksheet.


Can you post a screenshot of the raw data worksheet, and, post a screenshot of the worksheet results (manually formatted by you) that you are looking for?

To post your data, you can download and install one of the following two programs:
Excel Jeanie
MrExcel HTML Maker


Even better:

You can upload your workbook to Box Net,
sensitive data scrubbed/removed/changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
OK -- turn on macro recording before you folow my instructions, and turn it off when you're done. Then you should be able to change the code just a little to suit your needs. Macro recording is on the Developer ribbon, which is normally not shown, so you have to find it in Customize Ribbon.
 
Upvote 0
Sorry I did not post in the correct format. However I have modified the code to work for my situation:

Code:
Dim k As Variant, i As Long, ii As Long
k = Range("K4:O63").Value
For i = 1 To UBound(k, 1)
  For ii = 1 To UBound(k, 2)
        If k(i, ii) <> "N/A" Then
            k(i, ii) = k(i, ii) / 1024
        End If
  Next ii
Next i
Range("K4:O63").Value = k

Thank you for your help
 
Upvote 0
shawnjfulton,

Nice catch and fix for my macro.

If you had explained that the range contained formulae, and, that some of the cells displayed errors (and what the errors were) then I could have posted the macro to work correctly for you from the start.

Thanks for the final feedback.

Glad I could get you started in the right direction for a solution.

Come back any time.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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