Results 1 to 5 of 5

Thread: Using xlCellTypeLastCell in VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    4

    Question Using xlCellTypeLastCell in VB6

    Hi, new to the board as you will be able to tell!

    Anyway, I am trying to create an exe file using VB6, I only need the exe file to do a few things, them being...

    Open Excel, take some data, play with a little etc..

    I got it all to work using VBA, but I am stuggling adapting it to VB, especially xlCellTypeLastCell!!

    Here's my code as it stands,

    Private Sub Evident_Click()

    Dim objExcel As Object
    Dim objExcel_data As Object
    Dim wkbk1 As Object
    Dim WkSht As Object
    Dim wshSrc As Object

    Dim NWShareDrive As String
    Dim vExcel As String
    Dim FileName As String
    Dim vLastCell As String
    Dim vFirstCell As String

    Dim PATemplate As String
    Dim intX As Integer 'used to reference the current cell on the source sheet
    Dim intY As Integer 'used to reference the current cell on the output sheet
    Dim intZ As Integer 'used to reference the current column on both sheets
    Dim strProvider As String 'this is contains the last used provider


    NWShareDrive = "Path"
    PATemplate = "FILE.xls"
    FileName = "FILE2.xls"

    Set objExcel = CreateObject("Excel.Application")

    Set wkbk1 = Nothing
    Set wkbk1 = objExcel.application.Workbooks.Open(NWShareDrive & PATemplate)
    Set WkSht = wkbk1.ActiveSheet

    objExcel.Visible = True
    objExcel.DisplayAlerts = False
    objExcel.application.Interactive = True

    vFirstCell = "A1"

    'Copy Data

    objExcel.Sheets("Data").Select
    objExcel.ActiveCell.SpecialCells(xlCellTypeLastCell).Select
    vLastCell = objExcel.ActiveCell.Address
    objExcel.Range(vFirstCell & ":" & vLastCell).Select
    With objExcel.Selection
    .Copy
    objExcel.Workbooks.Add
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    End With

    Set wkbk1 = Nothing
    Set WkSht = Nothing

    '------------------------------------------------------
    'This will create a new sheet for each Content Provder
    '------------------------------------------------------

    intX = 2 'set the position to row 2 in the source sheet as this is where data starts

    Set wshSrc = objExcel.Sheets("Sheet1") 'set reference to the source sheet

    Do Until wshSrc.Cells(intX, 1).Value = "" 'loop through the source sheet until we hit an empty row

    'checking the source provider is the same, if not we create a new sheet
    If Not wshSrc.Cells(intX, 1).Value = strProvider Then
    'sets the new provider to the variable
    strProvider = wshSrc.Cells(intX, 1).Value
    'adds new sheet
    Set WkSht = ThisWorkbook.Sheets.Add
    'names the sheet
    WkSht.Name = strProvider
    'copies the headings
    For intY = 1 To 8
    WkSht.Cells(1, intY).Value = wshSrc.Cells(1, intY).Value
    WkSht.Cells(1, intY).Font.Bold = True
    Next
    'set the current row on the output sheet to 2, so data is entered after the heading
    intY = 2
    End If

    'loops through the data on the current row from the source sheet
    'and enters it to the current row on the output sheet
    For intZ = 1 To 8
    WkSht.Cells(intY, intZ).Value = wshSrc.Cells(intX, intZ).Value
    Next

    WkSht.Columns.AutoFit

    'add one to our row pointers so we move on a line
    intY = intY + 1
    intX = intX + 1

    Loop
    End Sub


    Any help would be greatly appreciated!!!!

    Thanks,

    Andy

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using xlCellTypeLastCell in VB6

    Welcome to the Forums.

    I noticed that your using late binding but you refer to the excel constant. If your late binding then you need to declare the const.

    VB Code:
    1. Const xlCellTypeLastCell = 11
    What errors are you receiving?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    4

    Re: Using xlCellTypeLastCell in VB6

    It says;

    Unable to get the SpecialCells property of the Range Class

    Thanks for your help.

    Andy

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    4

    Re: Using xlCellTypeLastCell in VB6

    Fixed that bit, I didnt have excel reference selected in VB6!!! D'oh!! But I am stuck here now....

    vLastCell = objExcel.ActiveCell.Address
    objExcel.Range(vFirstCell & ":" & vLastCell).Select
    With objExcel.Selection

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using xlCellTypeLastCell in VB6

    Whats the error? It could be because your not passing any parameters so its using all the defaults for the .Address.
    Your range arguments may not be in the correct format because of that.

    Whats the values for vLastCell and vFirstCell?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width