|
-
Jun 7th, 2006, 03:44 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] writing formulas from VB6
Hi
I am adding formula to an excel worksheet.
I am using the following code
VB Code:
Dim wkbSource As Workbook
Dim oXLSheet As Worksheet
Dim j As Integer
Set wkbSource = Workbooks.Open("C:\test\test.xls")
Set oXLSheet = wkbSource.Worksheets("Stat")
sKey = InputBox("Enter the name of the Key that u want to add", "KeyManagement")
lastRow = oXLSheet.UsedRange.End(xlDown).Row
j = lastRow + 1
oXLSheet.Cells(j, 1).Value = sKey
oXLSheet.Cells(j, 2).Formula = "=SUMPRODUCT(COUNTIF(INDIRECT(" & Chr(34) & "'Sheet1'!L2 : L1000" & Chr(34) & ")," & Chr(34) & "*" & sKey & "*" & Chr(34) & "))"
oXLSheet.Cells(j, 3).Formula= "=SUMPRODUCT(COUNTIF(INDIRECT(" & Chr(34) & "'Sheet2'!L2 : L1000" & Chr(34) & ")," & Chr(34) & "*" & sKey & "*" & Chr(34) & "))"
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.
-
Jun 7th, 2006, 03:50 AM
#2
Re: writing formulas from VB6
You have your double quotes canceling out the evaluation of the variables.
VB Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 7th, 2006, 04:00 AM
#3
Re: writing formulas from VB6
This should work for "=sum(B20:D20)"
VB Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 8th, 2006, 12:53 AM
#4
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|