Results 1 to 9 of 9

Thread: Problem To Move To 2nd Worksheet

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    Question Problem To Move To 2nd Worksheet

    I have a problem with my coding when wants to move all the data to the second worksheet. I try to change the setting to make the output in 2nd worksheet, but it didnt work.

    this is my code:
    VB Code:
    1. Set objExcel = CreateObject("excel.application") 'Starts the Excel Session
    2. Set objBook = objExcel.Workbooks.Add             'Add a Workbook
    3. Set objSheet = objBook.Worksheets.Item(1)        'Select a Sheet
    4. objExcel.Application.Visible = True
    5.  
    6.  
    7. objExcel.Application.Cells(1, 1).Value = "Kod Kategori BA"
    8. objExcel.Application.Cells(1, 2).Value = "No.BA"
    9.  
    10.  
    11. With objExcel
    12.            
    13.             .Application.Cells(n, 1).Value = ((Trim(DataHRMIS.Recordset.Fields("BACatCd").Value)))
    14.             .Application.Cells(n, 2).Value = ((Trim(DataHRMIS.Recordset.Fields("BANo").Value)))
    15.  
    16. end with

    i try to change here, but it still dont work!

    VB Code:
    1. Set objSheet = objBook.Worksheets.Item(2)

    how can i fix this? anyone plz help me...tq

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

    Re: Problem To Move To 2nd Worksheet

    Moved from Classic VB forum.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problem To Move To 2nd Worksheet

    Your not using the book or sheet objects and running everything off ogf the application object which is incorrect.
    VB Code:
    1. Set objExcel = CreateObject("excel.application") 'Starts the Excel Session
    2.     Set objBook = objExcel.Workbooks.Add             'Add a Workbook
    3.     Set objSheet = objBook.Sheets.Item(2)        'Select second Sheet
    4.    
    5.     objExcel.Visible = True
    6.    
    7.     objSheet.Cells(1, 1).Value = "Kod Kategori BA"
    8.     objSheet.Cells(1, 2).Value = "No.BA"
    9.    
    10.     With objSheet
    11.         .Cells(n, 1).Value = ((Trim(DataHRMIS.Recordset.Fields("BACatCd").Value)))
    12.         .Cells(n, 2).Value = ((Trim(DataHRMIS.Recordset.Fields("BANo").Value)))
    13.     End With
    14.    
    15.     Set objSheet = Nothing
    16.     objBook.Close True
    17.     Set objBook = Nothing
    18.     objExcel.Quit
    19.     Set objExcel = Nothing
    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

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    Re: Problem To Move To 2nd Worksheet

    Thanks RobDog8888!

    I've tried the code already but it didnt work! it still display all the data in sheet 1.
    No changes from the previous one..

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    Re: Problem To Move To 2nd Worksheet

    hello guyz..is there anybody can help me on this? my system still cannot give output to the 2nd worksheet....

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

    Re: Problem To Move To 2nd Worksheet

    Are you just trying to add data to "Sheet2" or a different workbook?
    VB Code:
    1. Set objSheet = objBook.Sheets.Item("Sheet2") 'Set to second Sheet2 or #2
    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

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    Re: Problem To Move To 2nd Worksheet

    actually, i need to do both..But first, i try it to add data to sheet 2 within same work book.. i try both
    VB Code:
    1. Set objSheet = objBook.Sheets.Item("Sheet2")
    and also this way:
    VB Code:
    1. Set objSheet = objBook.Sheets.Item(2)
    both of this code not succesfull..

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

    Re: Problem To Move To 2nd Worksheet

    It really should be working but could you post the code you are using?
    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

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    52

    Re: Problem To Move To 2nd Worksheet

    hey Rob, I got it already.
    I cannot use objSheet to declare it!
    i used this way :

    VB Code:
    1. Set objExcel = CreateObject("excel.application") 'Starts the Excel Session
    2. Set objBook = objExcel.Workbooks.Add             'Add a Workbook
    3. objExcel.Sheets("Sheet1").Select
    4. objExcel.Sheets("Sheet1").Name = "Maklumat BA"

    This code works to me!
    But how can I format the cell to be in Text?
    It because, when i send the data from DB to excel, excel cannot read '07'..
    it only can recognize 7..

    Anyway, thanks to you to be so concern! TQ

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