Results 1 to 4 of 4

Thread: [RESOLVED] writing formulas from VB6

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Resolved [RESOLVED] writing formulas from VB6

    Hi
    I am adding formula to an excel worksheet.
    I am using the following code

    VB Code:
    1. Dim wkbSource As Workbook
    2. Dim oXLSheet As Worksheet
    3. Dim j As Integer
    4.  
    5.  
    6. Set wkbSource = Workbooks.Open("C:\test\test.xls")
    7.  
    8. Set oXLSheet = wkbSource.Worksheets("Stat")
    9.  
    10. sKey = InputBox("Enter the name of the Key that u want to add", "KeyManagement")
    11.  
    12. lastRow = oXLSheet.UsedRange.End(xlDown).Row
    13.     j = lastRow + 1
    14.    
    15. oXLSheet.Cells(j, 1).Value = sKey
    16. oXLSheet.Cells(j, 2).Formula = "=SUMPRODUCT(COUNTIF(INDIRECT(" & Chr(34) & "'Sheet1'!L2 : L1000" & Chr(34) & ")," & Chr(34) & "*" & sKey & "*" & Chr(34) & "))"
    17. oXLSheet.Cells(j, 3).Formula= "=SUMPRODUCT(COUNTIF(INDIRECT(" & Chr(34) & "'Sheet2'!L2 : L1000" & Chr(34) & ")," & Chr(34) & "*" & sKey & "*" & Chr(34) & "))"
    18. oXLSheet.Cells(j, 4).Formula= "=SUMPRODUCT(COUNTIF(INDIRECT(" & Chr(34) & "'Sheet3'!L2 : L1000" & Chr(34) & ")," & Chr(34) & "*" & sKey & "*" & Chr(34) & "))"

    let's take an example,let's take sKey="Sports" and j=20 so i want to write a formula into cell E20, the formula is "=sum(B20:D20)"

    I tried writing:

    oXLSheet.cells(j,5).Formula="=sum(cells(j,2):cells(j,4))"but it didn't work
    so how can i do it?
    thanks
    Last edited by RobDog888; Jun 7th, 2006 at 03:48 AM.

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

    Re: writing formulas from VB6

    You have your double quotes canceling out the evaluation of the variables.
    VB Code:
    1. oXLSheet.cells(j,5).Formula="=sum(" & oXLSheet.cells(j,2) & ":" & oXLSheet.cells(j,4) & ")"
    But this will take the value of the cells and place it in the formula, I dont think this is what you want?
    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: writing formulas from VB6

    This should work for "=sum(B20:D20)"
    VB Code:
    1. oXLSheet.cells(j,5).Formula = "=sum(B" & j & ":D" & j & ")"
    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
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: [RESOLVED] writing formulas from VB6

    Thanks RobDog888,that's it

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