codes on vb6 to excel

Upload: julz-fonbuena-aniz

Post on 07-Apr-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Codes on Vb6 to Excel

    1/25

    Contents2 - In the beginning - Adding a Reference to Excel3 - Opening Excel4 - Creating/opening a Workbook5 - Preparing to work with data (setting and closing App/Book/Sheet references)

    6 - Reading and writing data7 - Basic formatting8 - Finding "special" cells (bottom row etc)9 - Installation Issues10 - Using a Late-Bound connection to Excel11 - "Excel doesn't close"12 - How do I write code to ? [a.k.a. recording macro's]13 - Useful functions and downloads14 - Frequently Asked Questions

    .

    Mar 8th, 2006, 04:32 AM #2

    si_the_geekSuper Moderator

    Join Date: Jul 02

    Location: Bristol, UKPosts: 33,133

    In the

    beginning -

    Adding a

    Reference to

    Excel

    To start your

    journey intothe world ofautomatingExcel, you firstneed to tellyour programthat you wantto referenceExcel, to dothis:

    Go to the"Project"menu, andselect"References".You will bepresented witha long list of

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385734&postcount=2http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385734&postcount=2http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385734&postcount=2
  • 8/4/2019 Codes on Vb6 to Excel

    2/25

    availablereferences, justscroll down to"MicrosoftExcelX.X

    ObjectLibrary"(where X.X isa versionnumber - seepost #14 for alist), then tickit and pressOK.

    NB: if it isn't in the list,there is probably an

    error with yourinstallation of Excel.You can try to select thereference file manuallyby clicking the "browse"button, the file you needit is likely to be calledsomething like"ExcelX.olb" (for ExcelXP or later it seems tobe "Excel.exe" instead).

    As an aside, ifyou are

    releasing yoursoftware tovarious usersthen having areference isn'tthe method youshould use, butdon't worryabout it fornow (this is themethod you

    should usewhilstdevelopingyour software,as it providesauto-completefeatures andHelp while you

  • 8/4/2019 Codes on Vb6 to Excel

    3/25

    are writingyour code).

    .

    Mar 8th, 2006, 04:36 AM #3

    si_the_geekSuper Moderator

    Join Date: Jul 02Location: Bristol, UKPosts: 33,133

    Opening Excel

    Once you have a reference set up, you can create an instance ofExcel. To do this you declare an object variable with a data type ofExcel.Application, and then you can do whatever you want with it,before closing/disconnecting from it.

    In this simple example we will start a new instance of an Excel

    application, display it, and then disconnect from it. You can copythis code and run it as it is:VB Code:

    1. Dim oXLApp as Excel.Application 'Declarethe object variable

    2.3.4.5. Set oXLApp = New Excel.Application 'Create

    a new instance of Excel6.7.8.9. oXLApp.Visible = True 'Show it

    to the user10.11.12.13. Set oXLApp =

    Nothing 'Disconnect from Excel(let the user take over)

    14.

    NB: The final line is something that several people forget in thissituation, but it is important - without it your program will take upmemory which it isn't actually using, and after this part of yourprogram has been run several times your computer may crash.

    You can connect to an already open Excel application instead ofopening a new one (using GetObject- as we will see later), but for

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385737&postcount=3http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385737&postcount=3http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385737&postcount=3
  • 8/4/2019 Codes on Vb6 to Excel

    4/25

    now it is better to just create a new instance, as it is more reliableand less confusing.

    You can also use CreateObject as a variation to "New ..", but wewill also get to this later (post #10).

    .

    Last edited by si_the_geek; Aug 19th, 2006 at 11:23 AM.

    Mar 8th, 2006, 04:40 AM #4

    si_the_geekSuper Moderator

    Join Date: Jul 02Location: Bristol, UKPosts: 33,133

    Creating/opening a Workbook

    The example in the previous section was very basic, and to be

    honest pretty pointless too. In order to actually do anything youneed to have a Workbook to interact with.

    There are times when you want to create a new Workbook, andtimes when you want to work with an existing one.

    To create a new Workbook:VB Code:

    1. Dim oXLApp as Excel.Application 'Declarethe object variables

    2.

    3. Dim oXLBook as Excel.Workbook4.5.6.7. Set oXLApp = New Excel.Application 'Create

    a new instance of Excel8.9.10.11. Set oXLBook =

    oXLApp.Workbooks.Add 'Add a new workbook12.13.14.15. oXLApp.Visible =

    True 'Show it to the user16.17.18.19. Set oXLBook =

    Nothing 'Disconnect from Excel(let the user take over)

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385745&postcount=4http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385745&postcount=4http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385745&postcount=4
  • 8/4/2019 Codes on Vb6 to Excel

    5/25

    20.21. Set oXLApp = Nothing

    To open an existing workbook, just change the " Set oXLBook ="

    line in the above example to this:VB Code:

    1. Set oXLBook = oXLApp.Workbooks.Open("c:\myfolder\my workbook.xls")'Open an existingworkbook

    2.

    By default a new Workbook has the number of sheets which arespecified in the users options. You can set this option via code, butshould change it back afterwards (else the user will have the option

    that you set). To do this you need a few extra lines around the "'Adda new workbook" line:VB Code:

    1. 2.3. Set oXLApp = New Excel.Application 'Create

    a new instance of Excel4.5.6.7. Dim iSheetsPerBook asInteger 'Add a

    new workbook (with one sheet)8.9. iSheetsPerBook = oXLApp.SheetsInNewWorkbook10.11. oXLApp.SheetsInNewWorkbook = 112.13. Set oXLBook = oXLApp.Workbooks.Add14.15. oXLApp.SheetsInNewWorkbook =

    iSheetsPerBook16.17.18.19. oXLApp.Visible =

    True 'Show it to the user20.21.

    .

  • 8/4/2019 Codes on Vb6 to Excel

    6/25

    Mar 8th,2006, 04:44AM

    #5

    si_the_geekSuper

    Moderator

    Join Date:Jul 02Location:Bristol, UKPosts:

    33,133

    Preparing to work with data (setting and closing App/Book/Sheet references)

    So far, we have got as far as having a Workbook open within the Excel application,which is getting better but doesn't let us do what we want - interact with the data!

    For those of you who have experience in this area, you will hopefully know alreadythat we are missing a vital ingredient in our examples so far - the Worksheet. Alldata in a workbook is held in specific worksheets, so we should be using them.

    Now it is time to start with the proper examples. There are a few different ways ofmodifying/reading data, but all of them start with the same basic block of code,which we will call "Part A":VB Code:

    1. Dim oXLApp as Excel.Application 'Declare theobject variables

    2.3. Dim oXLBook as Excel.Workbook4.5. Dim oXLSheet as Excel.Worksheet6.7.8.9. Set oXLApp = New Excel.Application 'Create a new

    instance of Excel10.11. Set oXLBook = oXLApp.Workbooks.Add 'Add a new

    workbook12.13. Set oXLSheet = oXLBook.Worksheets(1) 'Work with

    the first worksheet14.

    Note that you could open an existing workbook instead of creating a new one, byusing the code in the previous post.

    This should be followed by some sort of modification/reading code, which we willcome to in the next section - the important part for now is to finish properly,

    because if you don't then you won't see any of the changes you have made!

    When you have finished working with the data you can show, save, save as, or justclose the workbook. There are different methods for each of these options, all ofwhich we will call "Part B", which one of these you use is up to you (and willprobably vary depending on the project you are writing at the time).

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385750&postcount=5http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385750&postcount=5http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385750&postcount=5
  • 8/4/2019 Codes on Vb6 to Excel

    7/25

    Part B(1) - Show the workbook: (best while you are writing or testing!)VB Code:

    1. oXLApp.Visible = True 'Show it to the user2.3. Set oXLSheet = Nothing 'Disconnect from

    all Excel objects (let the user take over)4.5. Set oXLBook = Nothing6.7. Set oXLApp = Nothing

    or, Part B(2) - Save the existing workbook you opened:VB Code:

    1. Set oXLSheet = Nothing 'disconnect from theWorksheet

    2.3. oXLBook.Close SaveChanges:= True 'Save (and disconnect

    from) the Workbook4.5. Set oXLBook = Nothing6.7. oXLApp.Quit 'Close (and

    disconnect from) Excel8.9. Set oXLApp = Nothing

    or, Part B(3) - Save the workbook to a new file:VB Code:

    1. Set oXLSheet = Nothing 'disconnectfrom the Worksheet

    2.3. oXLBook.SaveAs "C:\My Documents\My File.xls" 'Save (and

    disconnect from) the Workbook4.5. oXLBook.Close SaveChanges:= False6.7. Set oXLBook = Nothing8.9. oXLApp.Quit 'Close

    (and disconnect from) Excel

    10.11. Set oXLApp = Nothingor, Part B(4) - Close the workbook (but don't save):VB Code:

    1. Set oXLSheet = Nothing 'disconnect from theWorksheet

  • 8/4/2019 Codes on Vb6 to Excel

    8/25

    2.3. oXLBook.Close SaveChanges:= False 'Close (and

    disconnect from) the Workbook4.5. Set oXLBook = Nothing6.7.

    oXLApp.Quit 'Close (anddisconnect from) Excel

    8.9. Set oXLApp = Nothing

    Once you have these two chunks of code you are ready to start the fun.

    .

    Mar 8th,2006, 04:48AM

    #6

    si_the_geekSuper

    Moderator

    Join Date:Jul 02Location:Bristol, UKPosts:33,133

    Reading and writing data

    This code needs to be run on an open worksheet, so should be placed between "PartA" and "Part B" from the previous post. It is probably best to use B(1) for now, sothat you can see what is going on more easily.

    a) using Cells, Range (and UsedRange)

    There are a few methods of working with the data depending on what you want toachieve.

    The simplest method is to read/write a single cell, which can be done using theCells collection of the WorkSheet like this:VB Code:

    1. Dim my_variable AsString2.3. my_variable = oXLSheet.Cells(2, 1).Value

    This copies the text in the cell A2 (specified in the brackets: row 2, column 1) tothe variable "my_variable".

    To write a single cell you just need to change the order of the expression like this:VB Code:

    1. Dim my_variable AsString2.

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385753&postcount=6http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385753&postcount=6http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385753&postcount=6
  • 8/4/2019 Codes on Vb6 to Excel

    9/25

    3. my_variable = "hello"4.5. oXLSheet.Cells(2, 1).Value = my_variable6.7. 'or just this:8.9.

    oXLSheet.Cells(2, 1).Value = "hello"

    To read or write the Formula of the cell rather than the displayed text, just changethe ".Value" to ".Formula", e.g.:VB Code:

    1. oXLSheet.Cells(3, 1).Formula = "=A1+2"

    All of the methods so far can also be used with the Range object instead of Cells,

    which allows you to specify the cell in the standard Excel "A1" address format,e.g.:VB Code:

    1. oXLSheet.Range("B4").Value = "range test"

    There is an advantage to using the Range object, in that you can work with multiplecells at the same time, for example you could set the value of all cells in the rangeD3:E5 to a single value like this:VB Code:

    1. oXLSheet.Range("D3:E5").Value = 3This may seem a little pointless, but with a little alteration you can see the power ofthis, as you can set all the values differently by using an array like this:VB Code:

    1. Dim lMyArray(2, 1)AsLong2.3. lMyArray(0, 0) = 14.5.

    lMyArray(0, 1) = 26.

    7. lMyArray(1, 0) = 38.9. lMyArray(1, 1) = 410.11. lMyArray(2, 0) = 512.13. lMyArray(2, 1) = 614.

  • 8/4/2019 Codes on Vb6 to Excel

    10/25

    15. oXLSheet.Range("D7:E9").Value = lMyArrayThe location of the cells within the range can be deduced from the array positionsin the same way as for the Cells collection, i.e.: (row, column).

    Unfortunately you can't quite do this the other way around (i.e.: you can't setlMyArray equal to the range), as VB will not allow you to assign to an array.Instead you need to use a Variant to hold the array instead, e.g.:VB Code:

    1. Dim vArray As Variant2.3. vArray = oXLSheet.Range("D7:E9").Value

    You can copy the entire used range of the spreadsheet simply using a special built-in range called UsedRange, like this:

    VB Code:

    1. Dim vArray As Variant2.3. vArray = oXLSheet.UsedRange.Value4.5. 'vArray now contains the values of ALL the cells in used

    range of the worksheet.6.7.8.9.10.11.

    'example usage of the array:12.

    13. Dim lngCol asLong, lngRow asLong14.15. For lngRow = 1ToUBound(vArray,1)16.17. For lngCol = 1ToUBound(vArray,2)18.19. MsgBox vArray(lngRow, lngCol)20.21. Next lngCol22.23. Next lngRow

    Setting values in the spreadsheet using an array has the advantage of being farquicker, as each time you enter data Excel does some work like re-calculatingformulas. Using an array means that this extra work is only done once for the entirearray, rather than once per cell.

    b) using a RecordSet

  • 8/4/2019 Codes on Vb6 to Excel

    11/25

    There is one more way of putting data into Excel that is extremely useful, which isto copy data directly from a recordset that you have gotten from a database. Yousimply say which is the first cell to put the data into, and Excel works out the rest ofthe cells that are required (although you can specify the maximum rows/columns touse in two optional parameters).

    I have not created the recordset in these examples, as there are many ways in whichyou can do it. If you need help with this, please see the tutorials (or post a newthread) in the Database Development forum on this site.VB Code:

    1. 'create and fill a recordset here, called oRecordset2.3. oXLSheet.Range("B15").CopyFromRecordset oRecordset

    Note that you can also fill in the field names using an array like this:

    VB Code:

    1. Dim iCount AsInteger2.3. With oXLSheet 'Fill with data4.5. For iCount = 0To(oRecordset.Fields.Count -1)6.7. .Cells(1, iCount+ 1) =

    oRecordset.Fields(iCount).Name8.9. Next iCount10.11.

    .Range("A2").CopyFromRecordset oRecordset12.

    13. EndWith"But I thought setting individual Cells was slow?" It is, but to use Range you needto know the target cells, in order to do that you need to know the column address of"B" + oRecordset.Fields.Count, which requires a bit more work. Luckily I haveprovided a function in section 13 "Useful functions and downloads" called xl_Col,which returns the column name for the column number that you specify (there isalso xl_ColNo, which converts the name back into a number). This function allowsyou to do use the Range object like this:VB Code:

    1. Dim iCount AsInteger2.3. Dim sFieldNames()AsString4.5. ReDim sFieldNames(oRecordset.Fields.Count - 1)AsString6.7. For iCount = 0To(oRecordset.Fields.Count -1)8.

  • 8/4/2019 Codes on Vb6 to Excel

    12/25

    9. sFieldNames(iCount) = oRecordset.Fields(iCount).Name10.11. Next iCount12.13. oXLSheet.Range("A1:" & xl_Col (1 +

    oRecordset.Fields.Count) & "1")

    .

    Last edited by si_the_geek; May 12th, 2006 at 08:04 AM. Reason: corrected

    "example usage of the array" (previously read rows as columns & vice-versa)

    Mar 8th, 2006,04:52 AM

    #7

    si_the_geek

    Super Moderator

    Join Date: Jul 02Location: Bristol,UKPosts: 33,133

    Basic formatting

    You can format cells by code in a similar way to how you wouldmanually. You can set things such as the font, the font style (bold,underline, etc), the alignment (left, right, etc) and the text colour. You canapply these formats to any kind of range, the two we have looked at so far(Cells and Range) and a few new ones.

    For example, to set the cell "A4" to be bold you can do the following:VB Code:

    1. oXLSheet.Range("A4").Font.Bold = True '(False toturn bold off)

    2.Or to set it bold, italic, and underlined, you can do this:VB Code:

    1. With oXLSheet.Range("A4")2.3. .Font.Bold = True '(False to turn bold

    off)4.5. .Font.Italic = True '(False to turn

    italic off)6.7. .Font.Underline = xlUnderlineStyleSingle8.9. EndWith

    Note that Underline does not use Boolean values, as there are various

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385757&postcount=7http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385757&postcount=7http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385757&postcount=7
  • 8/4/2019 Codes on Vb6 to Excel

    13/25

    options for underlining (such as single or double underline). When youtype in the = after Font.Underline a list of the values should appear, sothat you can simply select the appropriate one (the value to turnunderlining off is xlUnderlineStyleNone).

    To change the text colour of the same cell to green you can do this:VB Code:

    1. With oXLSheet.Range("A4")2.3. .Font.ColorIndex = 504.5. EndWith

    To align the text within cells you can use a couple of extra properties, onefor horizontal and one for vertical:VB Code:

    1. With oXLSheet.Range("A4")2.3. .HorizontalAlignment = xlRight '(other

    options include xlCenter and xlLeft)4.5. .VerticalAlignment = xlBottom '(other

    options include xlCenter and xlBottom)6.7. EndWith

    The alternative range objects we are interested in are Rows and Columns.Both of these take a single parameter, which is the row or column numberwe want to work with, and can be used in the same way as Cells orRange, eg:VB Code:

    1. oXLSheet.Rows(7).Font.Bold = True '(False toturn bold off)

    2.3. oXLSheet.Columns(3).Font.Bold = True '(False

    to turn bold off)

    4.You can also select multiple adjacent columns/rows by providing a stringparameter instead of a number, which contains the first and lastrow/column separated by a colon (if you provide a string for Columns, itmust be the column name rather than number), eg:VB Code:

  • 8/4/2019 Codes on Vb6 to Excel

    14/25

    1. oXLSheet.Rows("6:7").Font.Bold = True '(Falseto turn bold off)

    2.3. oXLSheet.Columns("C:E").Font.Bold =

    True '(False to turn bold off)

    4.

    .

    Mar 8th, 2006,05:01 AM

    #8

    si_the_geekSuper Moderator

    Join Date: Jul 02Location: Bristol,UKPosts: 33,133

    Finding "special" cells (bottom row etc)

    As you may already know, when you are editing a spreadsheet manually

    you can press Ctrl and an arrow key to go to the next/last cell in thatdirection which contains text. There are also methods of performing thisvia code.

    Here is one method to find the last used row/column in the sheet:VB Code:

    1. LastRow = oXLSheet.UsedRange.Rows.Count2.3. LastCol = oXLSheet.UsedRange.Columns.Count

    NB: UsedRange is a special pre-defined range which contains the entire used area of the sheet.

    Here is an alternative - but note that this method assumes that every rowhas data in all columns (and vice versa). If this is not the case, it is safer touse the options above/below instead.VB Code:

    1. LastRow = oXLSheet.Range("A1").End(xlDown).Row2.3. LastCol =

    oXLSheet.Range("A1").End(xlToRight).Column

    There is also another built-in function in Excel for detecting used ranges,the .SpecialCells function retrieves a number of "special range" valuesdepending on the constant value passed to the function. Here is theequivalent to the code above (works by going to the 'last' used cell):VB Code:

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385768&postcount=8http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385768&postcount=8http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385768&postcount=8
  • 8/4/2019 Codes on Vb6 to Excel

    15/25

    1. LastRow =oXLSheet.Cells.SpecialCells(xlCellTypeLastCell).Row

    2.3. LastCol =

    oXLSheet.Cells.SpecialCells(xlCellTypeLastCell).Col

    umn

    Some of the other parameters for the SpecialCells method are:

    xlCellTypeAllValidation - Cells having validation criteria xlCellTypeBlanks - Empty cells xlCellTypeComments - Cells containing notes xlCellTypeFormulas - Cells containing formulas

    The full list can be seen in the Help or the Object Browser.

    .

    Last edited by si_the_geek; Jun 24th, 2006 at 06:29 PM.

    Mar 8th, 2006, 05:06 AM #9

    si_the_geekSuper Moderator

    Join Date: Jul 02Location: Bristol, UKPosts: 33,133

    Installation Issues

    As you are probably aware there are several versions of Excel, all of

    which have subtle variations in the way they work, and the featuresthat they provide.

    In the vast majority of cases this will not affect the code you need towrite to perform the tasks required on the document (unless you use'new' features which aren't available in the version of Excel which is

    installed). There can however be a problem when your programtries to start Excel.

    The trouble is that with your installation package you installed aspecific version of the Excel type library, which could easily be

    different from the version of Excel which is installed on the userscomputer. Even though the installed version provides essentially thesame functionality as the one on your development computer,Windows may realise that there are version differences and stopyour program from using something that it thinks is likely to causeerrors.

    This has become even more pronounced recently, as versions of

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385775&postcount=9http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385775&postcount=9http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385775&postcount=9
  • 8/4/2019 Codes on Vb6 to Excel

    16/25

    Excel now get you to install an "Excel.exe" file along with yourprogram, rather than the traditional "ExcelX.olb" file

    Another issue is that Excel may not even be installed, which canstop your program from even starting!

    The best way around these issues is to use a Late-Bound connectionto Excel (see next section), with extra error handling around the firstline (CreateObject/GetObject) to deal with the possibility that itisnt installed.

    .

    Mar 8th, 2006, 05:09 AM #10

    si_the_geekSuper Moderator

    Join Date: Jul 02Location: Bristol, UKPosts: 33,133

    Using a Late-Bound connection to Excel

    The code so far in this tutorial has been Early-Bound (i.e.: VBknows in advance what Excel functions are available to you), whichas seen in the previous section can cause serious issues.

    Unless you can guarantee that all of your users have the sameversion of Excel installed as you do, I would strongly recommendconverting to Late-bound for software that you release "into thewild".

    The down-side to using this is that you no longer get some of thenice features of the VB IDE (like the drop-down lists of propertiesand methods that appear when you type oXLApp. ), so it may be agood idea to convert to this method once your code is finished.

    There are four steps to convert your code:1) Replace Excel data types with Object.If you have:VB Code:

    1. Dim oXLApp as Excel.Application2.

    3. Dim oXLBook as Excel.Workbook4.5. Dim oXLSheet as Excel.Worksheet6.7. ...

    You need to change it to:

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385780&postcount=10http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385780&postcount=10http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385780&postcount=10
  • 8/4/2019 Codes on Vb6 to Excel

    17/25

    VB Code:

    1. Dim oXLApp as Object2.3. Dim oXLBook as Object4.5. Dim oXLSheet as Object6.7. ...

    Note that you should also do this with any other variables that youhave declared as Excel.something

    2) Change the initialisation of the application object.

    If you have:

    VB Code:

    1. Set oXLApp = New Excel.Application

    You should replace it with:VB Code:

    1. Set oXLApp = CreateObject("Excel.Application")Note:if Excel isnt installed, this line of code will cause an error you should deal with this by using error handling in an appropriateway for your project.

    3) Define the constants that you have used.Excel constants (e.g.: xlLeft) are defined in the object library;however you will no longer have a link to this, so they wont bedefined any more!

    There are two main options here; the first is to use the ObjectBrowser to find the values/declarations for each one you have used

    (very slow, and prone to errors!), and the other (much simpler) is toadd a pre-made module to your project which declares them all foryou. You can find a link to one which Microsoft produced in theuseful functions and downloads section (post #13).

    4) Remove "Excel object Library" from the list in "Project" ->

  • 8/4/2019 Codes on Vb6 to Excel

    18/25

    "References"

    Your code is now late-bound, and should work with all versions ofExcel that support automation (as long as you havent used special

    functionality which wasnt available in earlier versions).

    .

    Mar 8th, 2006, 05:18 AM #11

    si_the_geekSuper Moderator

    Join Date: Jul 02Location: Bristol, UKPosts: 33,133

    "Excel doesn't close"

    If you find that after running your program you still have an opencopy of Excel that you weren't expecting (either visible, or shown in

    Task Manager) then I'm afraid that you have made a mistakesomewhere in your code.

    The usual cause is using objects that are ok in Excel VBA code, butnot in a VB application. Common examples of this are:

    Application (which needs to be oXLApp) Cells (which should be oXLSheet.Cells) Range (which should to be oXLSheet.Range) WorkSheets (which should be oXLApp.WorkSheets) Selection* (which needs to be oXLApp.Selection) ActiveSheet* (which should be a variable like oXLSheet)

    For any other Excel based items you use, you should do the same asall of the items in this list - specify the parent object (usuallyoXLSheet or oXLBook or oXLApp) that you want to refer to.

    * As a side note, it is advisable to avoid using Selection andActiveSheet altogether (even in Excel VBA code), as this can bechanged by the user if they are working in Excel while your code isrunning - which means you could well be working in the wrongworksheet/workbook!

    It may be that you haven't used appropriate code to close thesheet/workbook/application (see post #5 for valid examples). This isparticularly true if you have not used a variable to store theWorkSheet (as several people seem to do), as it is easy to refer tothe WorkBook instead (which isn't technically valid, even tho itworks); I would therefore strongly recommend using variable for it.

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385788&postcount=11http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385788&postcount=11http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385788&postcount=11
  • 8/4/2019 Codes on Vb6 to Excel

    19/25

    If as part of "closing down" you miss a child object variable (whichis basically what happens in the situation described above) anddestroy the application object, it will orphan the child objectvariable and cause the instance of Excel to remain because not all

    resources associated with the application instance have been closedand destroyed.

    .

    Last edited by si_the_geek; Feb 6th, 2009 at 01:07 PM. Reason:

    added more text

    Mar 8th, 2006, 05:26 AM #12

    si_the_geek

    Super Moderator

    Join Date: Jul 02Location: Bristol, UKPosts: 33,133

    How do I write code to ? [a.k.a. recording macro's]

    There are many things which can be done with Excel that haven'tbeen listed here, and there is a good reason - Excel will tell you thecode you need!

    If you record a macro in Excel, it is 'written' in VBA (a subset ofVB), and this code can be copied almost directly into your VBprogram, with only a few modifications to use the objects you havecreated rather than the default Application/Sheet/etc objects [failureto do this part will most likely cause your program to have bugs, or

    even cause your computer to crash].

    To record a macro:

    Go to "Tools" -> "Macro" -> "Record new macro". Manually do the things that you want your program to do. Press the "stop recording" button (a blue square). Go to "Tools" -> "Macro" -> "Macros...", which will show a

    list of macros. Select the macro you recorded, and click on "Edit" to see the

    code.

    There are 4 steps you should perform when taking code from amacro to use in your program, they are:

    1. Remove un-needed code (as Excel often does extra things,such as writing all values from an options screen).

    2. ChangeActiveDocument,Application etc to suit the

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385800&postcount=12http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385800&postcount=12http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385800&postcount=12
  • 8/4/2019 Codes on Vb6 to Excel

    20/25

    variables in your code.3. Change Selection to oXLApp.Selection.4. Prefix all instances of Range/Cells/Rows/Columns/etc with

    your sheet object.

    Example 1:The following code is created if you select a range, then set it toBold and Italic:VB Code:

    1. Range("C9:F16").Select2.3. Selection.Font.Bold = True4.5. Selection.Font.Italic = True

    In this case there is no un-needed code, so we can skip step 1.

    There is no use ofActiveDocumentetc, so we can also skip step 2.

    Following step 3 gives us this:VB Code:

    1. Range("C9:F16").Select2.3. oXLApp.Selection.Font.Bold = True4.5.

    oXLApp.Selection.Font.Italic = True

    And step 4 gives us this:VB Code:

    1. oXLSheet.Range("C9:F16").Select2.3. oXLApp.Selection.Font.Bold = True4.5. oXLApp.Selection.Font.Italic = True

    As mentioned in the previous post, it is best to avoid Selection

    altogether. This can be done by changing the code to this:VB Code:

    1. oXLSheet.Range("C9:F16").Font.Bold = True2.3. oXLSheet.Range("C9:F16").Font.Italic =

    True

  • 8/4/2019 Codes on Vb6 to Excel

    21/25

    ..or you can do it like this instead (a bit more efficient, and easier toread):VB Code:

    1. With oXLSheet.Range("C9:F16")2.

    3. .Font.Bold = True4.5. .Font.Italic = True6.7. EndWith

    Example 2:As another example, here is part of the code created if you go into"File"->"Page Setup", then set the orientation to Landscape, and set

    the "rows to repeat at top" to $1:$2VB Code:

    1. With ActiveSheet.PageSetup2.3. .PrintTitleRows = "$1:$2"4.5. .PrintTitleColumns = ""6.7. EndWith8.9. ActiveSheet.PageSetup.PrintArea = ""10.11. With ActiveSheet.PageSetup12.13. .LeftHeader = ""14.15. .CenterHeader = ""16.17. ... lots of lines removed! ...18.19. .CenterVertically = False20.21. .Orientation = xlLandscape22.23. .Draft = False24.25. .PaperSize = xlPaperA426.27. .FirstPageNumber = xlAutomatic28.29. .Order = xlDownThenOver30.31. .BlackAndWhite = False32.33. .Zoom = 100

  • 8/4/2019 Codes on Vb6 to Excel

    22/25

    34.35. EndWith

    Following step 1 gives us this:

    VB Code:

    1. With ActiveSheet.PageSetup2.3. .PrintTitleRows = "$1:$2"4.5. EndWith6.7. With ActiveSheet.PageSetup8.9. .Orientation = xlLandscape10.11. EndWith

    Which can be shortened to this:VB Code:

    1. With ActiveSheet.PageSetup2.3. .PrintTitleRows = "$1:$2"4.5. .Orientation = xlLandscape6.7. EndWith

    From there, following step 2 gives us this:VB Code:

    1. With oXLSheet.PageSetup2.3. .PrintTitleRows = "$1:$2"4.5. .Orientation = xlLandscape6.7. EndWith

    In this case, there is nothing to do for steps 3 and 4.

    .

    Last edited by si_the_geek; Mar 1st, 2007 at 02:55 PM.

    Mar 8th, 2006, 05:29 #13

    http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385805&postcount=13http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385805&postcount=13http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385805&postcount=13http://www.vbforums.com/showpost.php?s=f0b542be684680d936f9f8e0291471c1&p=2385805&postcount=13
  • 8/4/2019 Codes on Vb6 to Excel

    23/25

    AM

    si_the_geekSuper Moderator

    Join Date: Jul 02Location: Bristol, UKPosts: 33,133

    Useful functions and downloads

    Excel constants moduleThis is a pre-written module which contains all of the Excel constants

    (e.g.: xlLeft), to enable you to keep the constant names in your codewhen you are using Late-binding. Just add the module to yourproject! (NB: this download also contains similar modules for theother Office products).

    This is provided by Microsoft, and can be downloaded here:http://support.microsoft.com/kb/112671(alternative)

    Note that the above file is designed for Excel 97, so it does notcontain constants which have been added more recently (it doeshowever contain almost everything you need). You can see a list of

    constants in theExcel 2003 constants page, which contains those forExcel 97 plus newer ones.

    xl_col and xlColNameFunctions for converting column 'name' to/from column number.

    Simply add these functions to your code (in a module if you want touse them from multiple forms).VB Code:

    1. Function xl_Col(ByRef Col_No)AsString2. 'returns Excel column name from numeric

    position (e.g.: col_no 27 returns "AA")3. 'by Si_the_geek (VBForums.com)4.5. 'Only

    allow valid columns6. If Col_No < 1Or Col_No > 256ThenExit

    Function7.8. If Col_No < 27Then 'Single

    letter9. xl_Col = Chr(Col_No + 64)10. Else 'Two letters11. xl_Col = Chr(Int((Col_No - 1) / 26) +

    64) & _12. Chr(((Col_No - 1) Mod 26) +

    1 + 64)13. EndIf14.15. EndFunction16.

    http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://support.microsoft.com/kb/112671http://support.microsoft.com/kb/112671http://support.microsoft.com/default.aspx?scid=kb;en-us;112671http://support.microsoft.com/default.aspx?scid=kb;en-us;112671http://support.microsoft.com/default.aspx?scid=kb;en-us;112671http://msdn.microsoft.com/en-us/library/aa221100%28office.11%29.aspxhttp://msdn.microsoft.com/en-us/library/aa221100%28office.11%29.aspxhttp://msdn.microsoft.com/en-us/library/aa221100%28office.11%29.aspxhttp://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875http://msdn.microsoft.com/en-us/library/aa221100%28office.11%29.aspxhttp://support.microsoft.com/default.aspx?scid=kb;en-us;112671http://support.microsoft.com/kb/112671http://www.vbforums.com/member.php?s=f0b542be684680d936f9f8e0291471c1&u=34875
  • 8/4/2019 Codes on Vb6 to Excel

    24/25

  • 8/4/2019 Codes on Vb6 to Excel

    25/25

    number, rather than the displayed number.

    The version numbers are:

    7.0 - Excel 95 8.0 - Excel 97 9.0 - Excel 2000 10.0 - Excel XP 11.0 - Excel 2003

    How do I work with a copy of Excel that is already open?This will connect the application object to an open copy of Excel:VB Code:

    1. Set oXLApp = GetObject(,"Excel.Application")Please note that there is no way to specify which open copy of Excelto work with, so if there is more than one open you cannot predictwhich one will be referenced.

    Note also that if there are no instances of Excel running, this line ofcode will cause an error.