J
jluckhoff
Hi,
Im trying to devise a way to take a range and pull out each cell i
there's value, formula, font etc etc.
I have managed to do this so far in a very clumsy way by loopin
through the range as in the code below but this is proving very slow.
The idea is to serialise the sheet to xml so that I can transfer it t
a remote client and deserialise it there back into xl.
If anyone has dealt with the kind of problem I would really appreciat
any comments you might have to make it go faster.
here is the code:
Dim Excel As New Excel.Application
Dim rng As Excel.Range
Dim int As Integer
Dim address As String
Dim Cellcount As Integer
int = 0
'open the existing workbook
Excel.Workbooks.Open("c:\test.xls")
'MsgBox(rng.Value)
'Msdn Ref for code below
'ms-help://MS.MSDNQTR.2003JUL.1033/vbaxl10/html/xlhowhowtoloopthrougharangeofcells.htm
Cellcount = Excel.Worksheets("Sheet2").usedrange.Cells.count
1
'MsgBox(Cellcount)
Dim DataArr(Cellcount, 5)
For Each rng In Excel.Worksheets("Sheet2").UsedRange.cells
'Here we add the cell ref, value, font and formula to the array
DataArr(int, 0) = rng.Cells.Address
DataArr(int, 1) = rng.Value
DataArr(int, 2) = rng.Font.Name
DataArr(int, 3) = rng.Formula
If rng.Font.Bold = True Then
DataArr(int, 4) = "True"
End If
DataArr(int, 5) = rng.Font.Color
int = int + 1
Next
' here is the code that builds the array back into the ne
worksheet
'create new worksheet
Dim WSheet As New Excel.Worksheet
'Adding new worksheet to excel workbooks
WSheet = Excel.Workbooks.Add.Worksheets.Add
WSheet.Name = "CC1Duplicate"
'Now write the values, formulas and format to the cell in th
new sheet
int = 0
'the arraylen is still hardcoded and should be replaced b
Ubound
While int <= Cellcount
address = DataArr(int, 0)
Excel.Range(address).Value = DataArr(int, 1)
Excel.Range(address).Font.Name = DataArr(int, 2)
Excel.Range(address).Formula = DataArr(int, 3)
Excel.Range(address).Font.Bold = DataArr(int, 4)
Excel.Range(address).Font.Color = DataArr(int, 5)
int = int + 1
End While
'we want to see what is happening
Excel.Visible = True
'********************************************
Im trying to devise a way to take a range and pull out each cell i
there's value, formula, font etc etc.
I have managed to do this so far in a very clumsy way by loopin
through the range as in the code below but this is proving very slow.
The idea is to serialise the sheet to xml so that I can transfer it t
a remote client and deserialise it there back into xl.
If anyone has dealt with the kind of problem I would really appreciat
any comments you might have to make it go faster.
here is the code:
Dim Excel As New Excel.Application
Dim rng As Excel.Range
Dim int As Integer
Dim address As String
Dim Cellcount As Integer
int = 0
'open the existing workbook
Excel.Workbooks.Open("c:\test.xls")
'MsgBox(rng.Value)
'Msdn Ref for code below
'ms-help://MS.MSDNQTR.2003JUL.1033/vbaxl10/html/xlhowhowtoloopthrougharangeofcells.htm
Cellcount = Excel.Worksheets("Sheet2").usedrange.Cells.count
1
'MsgBox(Cellcount)
Dim DataArr(Cellcount, 5)
For Each rng In Excel.Worksheets("Sheet2").UsedRange.cells
'Here we add the cell ref, value, font and formula to the array
DataArr(int, 0) = rng.Cells.Address
DataArr(int, 1) = rng.Value
DataArr(int, 2) = rng.Font.Name
DataArr(int, 3) = rng.Formula
If rng.Font.Bold = True Then
DataArr(int, 4) = "True"
End If
DataArr(int, 5) = rng.Font.Color
int = int + 1
Next
' here is the code that builds the array back into the ne
worksheet
'create new worksheet
Dim WSheet As New Excel.Worksheet
'Adding new worksheet to excel workbooks
WSheet = Excel.Workbooks.Add.Worksheets.Add
WSheet.Name = "CC1Duplicate"
'Now write the values, formulas and format to the cell in th
new sheet
int = 0
'the arraylen is still hardcoded and should be replaced b
Ubound
While int <= Cellcount
address = DataArr(int, 0)
Excel.Range(address).Value = DataArr(int, 1)
Excel.Range(address).Font.Name = DataArr(int, 2)
Excel.Range(address).Formula = DataArr(int, 3)
Excel.Range(address).Font.Bold = DataArr(int, 4)
Excel.Range(address).Font.Color = DataArr(int, 5)
int = int + 1
End While
'we want to see what is happening
Excel.Visible = True
'********************************************