Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 5th, 2010, 09:21 PM
Registered User
 
Join Date: Aug 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation VB6 EXCEL PROBLEM

Hi,

I have a excel file with header on the first row and large amount of rows. what i want i to split the number of rows in the first excel workbook evenly into multiple new workbook with the header included. the format of the new workbook must be the same as the original data

Could anyone show me how to do it using vb6
 
Old August 5th, 2010, 10:19 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Coding with VB6 is similar to VBA. You need to add a Reference to Microsoft Excel Object library and use the Excel objects.

You can try the code in Excel's VB editor and use it in VB6.0

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 10th, 2010, 08:16 PM
Registered User
 
Join Date: Aug 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Shasur,

Could you guide or show me how to start?

Thank you
 
Old August 11th, 2010, 09:44 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Quote:
Originally Posted by Zam Zam View Post
Hi Shasur,

Could you guide or show me how to start?

Thank you
First add a reference to Microsoft Excel library in VB6.0. You can do that by Projects-->References-->Browse from Microsoft Excel and enable the check box

Once you add this all the Excel objects are available for your project

Now you need to Open and Excel instance. You can do that by

Dim oXL - Excel.Application
Set oXL = New Excel,Application

this opens a new Excel. You can make that visible by

oXl.Visible = True

and then you can open the workbook

Dim oWB as Excel.Workbook
Set oWB = oXL.Workbooks.Open (Path and Name of Workbook here)

now you can get the sheets

Dim oWS as worksheet
Set oWS = oWB.Worksheets(1)

from here you can add the logic to the worksheet .

once you are done. you need to close all you have opened

oWB.Save 'If you want to save the workbook
oWB.Close

oXL.Quit ' will close Excel

and set the objects to nothing
oWB =nothing
oXL = nothing

Follow each step and take your own time. Happy coding

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 16th, 2010, 08:39 PM
Registered User
 
Join Date: Aug 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Shasur,

I can read through teh main excel sheets/workbook.
but what i want is is copy the exect same data without formating the numeric value from the main workbook to several new workbook evenly.

how could i do that?

regards
 
Old August 16th, 2010, 11:11 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Do you want to copy the entire sheet to a new workbook or some contents of the sheet to a new workbook

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 16th, 2010, 11:21 PM
Registered User
 
Join Date: Aug 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Shasur,

My scenario is like this
i have an excel sheet eg. a.xls and in a.xls there are 501 rows of data. where first row is the header for each column

what i want is to split the number of rows to b.xls (first 250 rows from a.xls) and c.xls (next 250 rows from a.xls) with the header included from a.xls

meaning i want the data and format copy into new workbook remain unchanged and also the original data a.xls remain unchanged

the rows and columns of data in the a.xls will be different every time where i just need to split the main excel sheet evenly into 2 new workbook with the header included and data remain unchanged

kindly show me how to do that

regards
 
Old August 17th, 2010, 09:32 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Here is a hint:

Code:
Sub Split_Current_Sheet()
Dim oWS As Worksheet
Dim oWB As Workbook
Dim iMaxRow As Long
Dim iHalfRow As Long
Dim oCopy As Worksheet
Set oWS = ActiveSheet
iMaxRow = oWS.Cells.SpecialCells(xlCellTypeLastCell).Row
iHalfRow = iMaxRow / 2
Set oWB = Workbooks.Add
Set oCopy = oWB.Sheets(1)
oWS.Rows(1).EntireRow.Copy oCopy.Range("A1")
oWS.Range("A2", "Z" & CStr(iHalfRow)).EntireRow.Copy oCopy.Range("A2")
oWB.SaveAs "c:\b.xls"
oWB.Close False
Set oWB = Workbooks.Add
Set oCopy = oWB.Sheets(1)
oWS.Rows(1).EntireRow.Copy oCopy.Range("A1")
oWS.Range("A" & CStr(iHalfRow + 1), "Z" & CStr(iMaxRow)).EntireRow.Copy oCopy.Range("A2")
oWB.SaveAs "c:\c.xls"
oWB.Close False
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 17th, 2010, 09:44 PM
Registered User
 
Join Date: Aug 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Shasur,

i got the error "Copy Method of Range Class Failed"
any advise on the error?


regards?
 
Old August 18th, 2010, 03:59 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Can you post the line where the error comes. There might be a possibility that the Range is not assigned properly

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Edit Excel in VB6 Hornet28 VB How-To 6 December 9th, 2009 08:54 AM
VB6 Excel App to .NET problem zayasv VB.NET 0 October 24th, 2005 04:08 PM
VB6 Excel App to .NET problem zayasv General .NET 0 October 24th, 2005 04:05 PM
VB6/Excel/SQL tmiller1 Excel VBA 2 April 6th, 2005 02:10 PM
How can i open Excel From VB6 chiefouko Beginning VB 6 1 June 30th, 2003 08:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.