Macro in excell

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bram2008
    New Member
    • Jan 2008
    • 13

    Macro in excell

    How to use macro in excel?
    Can i get some code to record excell data to vb?

    O y, i have project in vb, and i write data in vb, then i transfer it to excell to build chart. This is my code :

    Private Sub Command3_Click( )
    Set ep = GetObject("C:\D ocuments and Settings\Byon 2007\My Documents\1.xls ")
    Set ew = ep.sheets(1)
    ew.cells(1, 1) = Label15(0).Capt ion
    ew.cells(2, 1) = Label15(1).Capt ion
    ew.cells(3, 1) = Label15(2).Capt ion
    ew.cells(4, 1) = Label15(3).Capt ion
    ew.cells(5, 1) = Label15(4).Capt ion
    ew.cells(1, 2) = Label27(0).Capt ion
    ew.cells(2, 2) = Label27(1).Capt ion
    ew.cells(3, 2) = Label27(2).Capt ion
    ew.cells(4, 2) = Label27(3).Capt ion
    ew.cells(5, 2) = Label27(4).Capt ion
    ew.cells(1, 3) = Text12(0).Text
    ew.cells(2, 3) = Text12(1).Text
    ew.cells(3, 3) = Text12(2).Text
    ew.cells(4, 3) = Text12(3).Text
    ew.cells(5, 3) = Text12(4).Text
    ew.cells(1, 4) = Label24(0).Capt ion
    ew.cells(2, 4) = Label24(1).Capt ion
    ew.cells(3, 4) = Label24(2).Capt ion
    ew.cells(4, 4) = Label24(3).Capt ion
    ew.cells(5, 4) = Label24(4).Capt ion
    ew.cells(1, 5) = Label23(0).Capt ion
    ew.cells(2, 5) = Label23(1).Capt ion
    ew.cells(3, 5) = Label23(2).Capt ion
    ew.cells(4, 5) = Label23(3).Capt ion
    ew.cells(5, 5) = Label23(4).Capt ion
    ew.cells(1, 6) = Label17(0).Capt ion
    ew.cells(2, 6) = Label17(1).Capt ion
    ew.cells(3, 6) = Label17(2).Capt ion
    ew.cells(4, 6) = Label17(3).Capt ion
    ew.cells(5, 6) = Label17(4).Capt ion
    ep.application. Visible = True
    End Sub

    what is wrong with this code? Coz the application is not visible when i click the command button.

    As u know, i want to change my data in vb and when i click this command button, the application and the chart in excell is visible and change itself according to data in vb....

    Thanks for your help.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by Bram2008
    How to use macro in excel?
    Can i get some code to record excell data to vb?

    O y, i have project in vb, and i write data in vb, then i transfer it to excell to build chart. This is my code :

    Private Sub Command3_Click( )
    Set ep = GetObject("C:\D ocuments and Settings\Byon 2007\My Documents\1.xls ")
    Set ew = ep.sheets(1)
    ew.cells(1, 1) = Label15(0).Capt ion
    ew.cells(2, 1) = Label15(1).Capt ion
    ew.cells(3, 1) = Label15(2).Capt ion
    ew.cells(4, 1) = Label15(3).Capt ion
    ew.cells(5, 1) = Label15(4).Capt ion
    ew.cells(1, 2) = Label27(0).Capt ion
    ew.cells(2, 2) = Label27(1).Capt ion
    ew.cells(3, 2) = Label27(2).Capt ion
    ew.cells(4, 2) = Label27(3).Capt ion
    ew.cells(5, 2) = Label27(4).Capt ion
    ew.cells(1, 3) = Text12(0).Text
    ew.cells(2, 3) = Text12(1).Text
    ew.cells(3, 3) = Text12(2).Text
    ew.cells(4, 3) = Text12(3).Text
    ew.cells(5, 3) = Text12(4).Text
    ew.cells(1, 4) = Label24(0).Capt ion
    ew.cells(2, 4) = Label24(1).Capt ion
    ew.cells(3, 4) = Label24(2).Capt ion
    ew.cells(4, 4) = Label24(3).Capt ion
    ew.cells(5, 4) = Label24(4).Capt ion
    ew.cells(1, 5) = Label23(0).Capt ion
    ew.cells(2, 5) = Label23(1).Capt ion
    ew.cells(3, 5) = Label23(2).Capt ion
    ew.cells(4, 5) = Label23(3).Capt ion
    ew.cells(5, 5) = Label23(4).Capt ion
    ew.cells(1, 6) = Label17(0).Capt ion
    ew.cells(2, 6) = Label17(1).Capt ion
    ew.cells(3, 6) = Label17(2).Capt ion
    ew.cells(4, 6) = Label17(3).Capt ion
    ew.cells(5, 6) = Label17(4).Capt ion
    ep.application. Visible = True
    End Sub

    what is wrong with this code? Coz the application is not visible when i click the command button.

    As u know, i want to change my data in vb and when i click this command button, the application and the chart in excell is visible and change itself according to data in vb....

    Thanks for your help.
    It seems fine to me, but i'd do it this way (it might help you):
    [CODE=vb]
    Private Sub Command3_Click( )
    dim obj1 as object
    set obj1 = createobject("e xcel.applicatio n")
    obj1.workbooks. open("C:\Docume nts and Settings\Byon 2007\My Documents\1.xls ")
    with obj1.workbooks( 1).worksheets(1 )
    .cells(1, 1) = Label15(0).Capt ion
    .cells(2, 1) = Label15(1).Capt ion
    .cells(3, 1) = Label15(2).Capt ion
    .cells(4, 1) = Label15(3).Capt ion
    .cells(5, 1) = Label15(4).Capt ion
    .cells(1, 2) = Label27(0).Capt ion
    .cells(2, 2) = Label27(1).Capt ion
    .cells(3, 2) = Label27(2).Capt ion
    .cells(4, 2) = Label27(3).Capt ion
    .cells(5, 2) = Label27(4).Capt ion
    .cells(1, 3) = Text12(0).Text
    .cells(2, 3) = Text12(1).Text
    .cells(3, 3) = Text12(2).Text
    .cells(4, 3) = Text12(3).Text
    .cells(5, 3) = Text12(4).Text
    .cells(1, 4) = Label24(0).Capt ion
    .cells(2, 4) = Label24(1).Capt ion
    .cells(3, 4) = Label24(2).Capt ion
    .cells(4, 4) = Label24(3).Capt ion
    .cells(5, 4) = Label24(4).Capt ion
    .cells(1, 5) = Label23(0).Capt ion
    .cells(2, 5) = Label23(1).Capt ion
    .cells(3, 5) = Label23(2).Capt ion
    .cells(4, 5) = Label23(3).Capt ion
    .cells(5, 5) = Label23(4).Capt ion
    .cells(1, 6) = Label17(0).Capt ion
    .cells(2, 6) = Label17(1).Capt ion
    .cells(3, 6) = Label17(2).Capt ion
    .cells(4, 6) = Label17(3).Capt ion
    .cells(5, 6) = Label17(4).Capt ion
    end with
    obj1.Visible = True
    End Sub[/CODE]

    Comment

    • Bram2008
      New Member
      • Jan 2008
      • 13

      #3
      OK Big Thanks.....
      It works.

      Comment

      Working...