Writing cell contents to binary file

bluto32

New Member
Joined
Jan 5, 2011
Messages
37
(I have Excel 2010 running on Windows 7 Ultimate)

I am trying to write an Excel macro which will (1) pull in all satellite transponder info from a webpage; (2) sort it according to which beams are desired; (3) break down the appropriate data into bytes, one per cell; (4)write the appropriate data to a file.

So far, with the help of the built-in macro recorder and searching this forum, I have managed to sort out (1), (2) and (3). Part (4) ought to be the easy part, but I'm struggling...:eeek:

Suppose I have already have a file called "test.ndf" with initial file size 1000 bytes. After doing stages (1)-(3) above, my spreadsheet has the relevant data in cells A1 to A2000, with each cell containing a decimal between 0 and 255 inclusive. I would like to write a macro which will loop through these 2000 cells, appending each value as a byte to my orginal file, so that it has file size 3000 bytes at the end.

Thanks for taking the time to read this,
Bluto
 
Thank you so much for replying!

I notice that you have still used a loop. Am I correct in saying that your code will be faster than mine in creating the byte array, since your loop is entirely between arrays and thus has no interaction with the worksheet whatsoever?

Purely out of interest, is it impossible to copy the data from ByteArray2D (variant type) to ByteArray1D (byte type) without using a loop?
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You can time it yourself.

Insert a new class module, name it "HighResTimer", paste the following. Edit: I got the code from the Net while working on a project.

VBA Code:
Option Explicit

'' High Resolution Timer
Private Declare PtrSafe Function QueryFrequency Lib "kernel32" Alias "QueryPerformanceFrequency" (ByRef Frequency As Currency) As Long
Private Declare PtrSafe Function QueryCounter Lib "kernel32" Alias "QueryPerformanceCounter" (ByRef PerfromanceCount As Currency) As Long

Dim Frequency As Currency
Dim Started As Currency
Dim Stopped As Currency

Private Sub Class_Initialize()

    Call QueryFrequency(Frequency)

End Sub

Public Sub StartTimer()

    QueryCounter Started
  
End Sub

Public Sub StopTimer()

    QueryCounter Stopped

End Sub

Public Sub ResetTimer()

    QueryCounter Started
    Stopped = 0

End Sub

Public Property Get Elapsed() As Double

    Dim Timer As Currency
  
    If Stopped = 0 Then
        QueryCounter Timer
    Else
        Timer = Stopped
    End If
  
    If Frequency > 0 Then
        Elapsed = (Timer - Started) / Frequency
    End If

End Property

Then in your code:

VBA Code:
Dim TestTimer as New HighResTimer

TestTimer.StartTimer

' Code To Measure Elapsed Time Here

TestTimer.StopTimer

Debug.Print "Elapsed Time (ms): " & (TestTimer.Elapsed * 1000)
 
Last edited:
Upvote 0
I notice that you have still used a loop. Am I correct in saying that your code will be faster than mine in creating the byte array, since your loop is entirely between arrays and thus has no interaction with the worksheet whatsoever?
That's correct. Loading sheet data into an array and then reading that array is much faster than interacting directly with sheet cells.

Purely out of interest, is it impossible to copy the data from ByteArray2D (variant type) to ByteArray1D (byte type) without using a loop?
Again correct, AFAIK. There's a VBA trick using Application.Transpose and Application.Index which converts a 2-d array to a 1-d array in one statement:


however the resultant 1-d array can only be a Variant array, not a Byte array.

Edit - fixed example.
VBA Code:
Sub Test()

    Dim Var2DArray As Variant
    Dim Var1DArray As Variant

    Var2DArray = ThisWorkbook.Worksheets("Sheet1").Range("A1:A4")
    Var1DArray = Application.Transpose(Application.Index(Var2DArray, 0, 1))

End Sub
 
Last edited:
Upvote 0
John - many thanks again for your helpful replies.

Gokhan - thank you for providing the timer code. I normally use "t = timer" at the start and "Debug.Print timer - t" at the end to time code, but am interested by your class module. (I've never used one before - or really understood them - and shall experiment with it.)
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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