PC Review


Reply
Thread Tools Rate Thread

Excel problem: Cant hide column.

 
 
Sorcerdon
Guest
Posts: n/a
 
      10th Oct 2006
Hello experts,

I am currently opening an excel file from a VB.net application.
The excel spreadsheet gets all its data from a Dataset.
I need to hide a column form the results.
I dont want the users to have access to that perticular column in the
excel spreadsheet.

Here is my current code:

Dim dt As DataTable = dsSql.Tables("TableName")
Dim col As DataColumn
Dim mrow As DataRow
Dim objxl As Excel.Application
Dim objwbs As Excel.Workbooks
Dim objwb As Excel.Workbook
Dim objws As Excel.Worksheet
Dim colindex As Integer
Dim rowindex As Integer


objxl = New Excel.Application
objwbs = objxl.Workbooks
objwb = objwbs.Add
objws = CType(objwb.Worksheets(1), Excel.Worksheet)

If includeheader Then
For Each col In dt.Columns
colindex += 1
objws.Cells(1, colindex) = col.ColumnName
Next
rowindex = 1
Else
rowindex = 0
End If
For Each mrow In dt.Rows
rowindex += 1
colindex = 0
For Each col In dt.Columns
colindex += 1
objws.Cells(rowindex, colindex) =
mrow(col.ColumnName).ToString()
Next
Next

objxl.DisplayAlerts = False 'Makes overwrites automatic so that
a prompt to overwrite doesnt pop up
objwb.SaveAs(sFileName)
objwb.Close()
objxl.DisplayAlerts = True 'Makes overwrites non-automatic so
that a prompt to overwrite does pop up
objxl.Quit()
objws = Nothing
objwb = Nothing
objwbs = Nothing
objxl = Nothing




What is the code to delete ONE entire column and where do I place it?

 
Reply With Quote
 
 
 
 
vezerid
Guest
Posts: n/a
 
      10th Oct 2006
Haven't played with .NET, but it should work anyway.

objws.Columns("D").Delete Shift:=xlToLeft

If I understand you correctly you want to first build the table and
then delete a column. If so, this line should go after all the loops.
You can probably put it before:
objxl.DisplayAlerts = False

Does this help?
Kostis Vezerides

Sorcerdon wrote:
> Hello experts,
>
> I am currently opening an excel file from a VB.net application.
> The excel spreadsheet gets all its data from a Dataset.
> I need to hide a column form the results.
> I dont want the users to have access to that perticular column in the
> excel spreadsheet.
>
> Here is my current code:
>
> Dim dt As DataTable = dsSql.Tables("TableName")
> Dim col As DataColumn
> Dim mrow As DataRow
> Dim objxl As Excel.Application
> Dim objwbs As Excel.Workbooks
> Dim objwb As Excel.Workbook
> Dim objws As Excel.Worksheet
> Dim colindex As Integer
> Dim rowindex As Integer
>
>
> objxl = New Excel.Application
> objwbs = objxl.Workbooks
> objwb = objwbs.Add
> objws = CType(objwb.Worksheets(1), Excel.Worksheet)
>
> If includeheader Then
> For Each col In dt.Columns
> colindex += 1
> objws.Cells(1, colindex) = col.ColumnName
> Next
> rowindex = 1
> Else
> rowindex = 0
> End If
> For Each mrow In dt.Rows
> rowindex += 1
> colindex = 0
> For Each col In dt.Columns
> colindex += 1
> objws.Cells(rowindex, colindex) =
> mrow(col.ColumnName).ToString()
> Next
> Next
>
> objxl.DisplayAlerts = False 'Makes overwrites automatic so that
> a prompt to overwrite doesnt pop up
> objwb.SaveAs(sFileName)
> objwb.Close()
> objxl.DisplayAlerts = True 'Makes overwrites non-automatic so
> that a prompt to overwrite does pop up
> objxl.Quit()
> objws = Nothing
> objwb = Nothing
> objwbs = Nothing
> objxl = Nothing
>
>
>
>
> What is the code to delete ONE entire column and where do I place it?


 
Reply With Quote
 
 
 
 
Sorcerdon
Guest
Posts: n/a
 
      10th Oct 2006
Thanks for the quick response.

What is the xlToLeft?
A parameter? a string?

regards,
Sorcerdon

 
Reply With Quote
 
Sorcerdon
Guest
Posts: n/a
 
      10th Oct 2006
BTW,

It still doesnt work.
I am working in VB.net
it doesnt like the line

objws.Columns("D").Delete Shift:=xlToLeft

it says Shift:=xlToLeft is not declared.
Meaning, it doesnt recognize it.

 
Reply With Quote
 
Sorcerdon
Guest
Posts: n/a
 
      10th Oct 2006
Your a genius!
I was missing the proper range format.

I used the following lines to achive what i wanted:

To get the "D" stringi used the following:
rangeArray(colindex) = objws.Cells(1, colindex).EntireColumn.Address()

Dim myStr As String
For Each myStr In rangeArray
If myStr <> "" Then
objws.Columns(myStr).Hidden = True
End If
Next

And this to finish it off.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I hide hide #REF! in Excel PerryK Microsoft Excel Misc 3 1st Mar 2009 11:13 AM
Specify which rows to NOT hide, and have excel hide the rest =?Utf-8?B?TW8y?= Microsoft Excel Programming 0 25th Apr 2007 03:44 AM
Hide And Un-hide Excel Toolbars Jim333 Microsoft Excel Programming 3 2nd Jul 2005 08:00 PM
How do I hide a worksheet in Excel and use a password to un-hide . =?Utf-8?B?RGNodW5n?= Microsoft Excel Misc 3 2nd Dec 2004 07:24 AM
Excel password but cant hide the sheets before entering password cakonopka Microsoft Excel Programming 1 30th Jan 2004 07:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:20 AM.