convert filesize to kb mb gb etc in VBA

AkaTrouble

Well-known Member
Joined
Dec 17, 2014
Messages
1,544
hi

i have a line in my VBA that currently converts the default bit file size to MB

Code:
Cells(NextRow, "D").Value = Format((objFile.Size / 1024 / 1024), "000") & " MB"

has anyone a way to do same thing but using KB, MB, GB, options

so if small file it shows in KB, medium medium file in MB and large file in GB

i thought about CF but as the entire sheet is created from the VBA routine I would like to do it as part of the loop


thanks for reading
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe something like

Code:
Select Case objFile.Size
    Case 0 To 1023
        Cells(nextrow, "D").Value = Format(objFile.Size, "000") & "B"
    Case 1024 To 104875
        Cells(nextrow, "D").Value = Format(ojbjFile.Size / 1024, "000") & "KB"
    Case 104876 To 1073741823
        Cells(nextrow, "D").Value = Format(ojbjFile.Size / 104876, "000") & "MB"
End Select

Add more lines as needed.
 
Upvote 0
It will depend on your definitions of small, medium and large - but you can use a Select Case statement like this:

Code:
Select case objFile.Size
Case 0 to 1000000
Cells(NextRow, "D").Value = Format((objFile.Size / 1024), "000") & " KB"
case 1000000 to 1000000000
Cells(NextRow, "D").Value = Format((objFile.Size / 1024 / 1024), "000") & " MB"
case else
Cells(NextRow, "D").Value = Format((objFile.Size / 1024 / 1024 / 1024), "000") & " GB"
End Select
 
Upvote 0
thanks both options are good i slightly tweeked code so far looks ok

Code:
Select Case objFile.Size
        Case 0 To 1023
            Cells(NextRow, "D").Value = Format(objFile.Size, "0") & "B"
        Case 1024 To 104875
            Cells(NextRow, "D").Value = Format(objFile.Size / 1024, "0") & "KB"
        Case 104876 To 1073741823
            Cells(NextRow, "D").Value = Format(objFile.Size / 104876, "0") & "MB"
        Case 1073741824 To 1.11111111111074E+20
            Cells(NextRow, "D").Value = Format(objFile.Size / 1073741823, "0.00") & "GB"
    End Select

so thanks to both of you
 
Upvote 0
I just noticed that I had some missing digits in the code which will give incorrect results for MB file sizes, corrected version below (including your tweaks).

Code:
Select Case objFile.Size
        Case 0 To 1023
            Cells(NextRow, "D").Value = Format(objFile.Size, "0") & "B"
        Case 1024 To 1048575
            Cells(NextRow, "D").Value = Format(objFile.Size / 1024, "0") & "KB"
        Case 1048576 To 1073741823
            Cells(NextRow, "D").Value = Format(objFile.Size / 1048576, "0") & "MB"
        Case 1073741824 To 1.11111111111074E+20
            Cells(NextRow, "D").Value = Format(objFile.Size / 1073741823, "0.00") & "GB"
    End Select

Would decimal results be preferable if the output is in KB / MB and say less than 100?
 
Upvote 0
Would decimal results be preferable if the output is in KB / MB and say less than 100?

glad you came back i was scratching head as to what was happening

i thought about decimal options for lower cases but wondered if any need as cant see in this day of GB and TB that it would impact much
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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