Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Insert formula into a vairable range of cells in Excel from VB

Insert formula into a vairable range of cells in Excel from VB

Scheduled Pinned Locked Moved Visual Basic
databasehelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nhsal69
    wrote on last edited by
    #1

    Hi there, I have the following code which takes data from SQL and then adds it to a Excel Spreadsheet. This all works fine, but I need to subtract the column "K" from Column "L" and store it in "M" It inserts the formula into the required number of rows (dependant of the value   "R2") but the formula shows as =(K & D1)-(L & D1) in each cell and not for example =(K 28)-(L 28) etc.... Can you help?? Thanks <code>            Dim SQlQuery2 As String = "select * from growth where Name <> '[Files]'order by FullPath"             Dim SQLCommand2 As New SqlCommand(SQlQuery2, myConn)             Dim SQlReader2 As SqlDataReader = SQLCommand2.ExecuteReader             Dim R2 As Integer = 27             While SQlReader2.Read                   R2 = R2 + 1                   osheet.Range("I" & R2).Value = SQlReader2.GetValue(0).ToString                   osheet.Range("I" & R2).BorderAround(8)                   osheet.Range("J" & R2).Value = SQlReader2.GetValue(1).ToString                   osheet.Range("J" & R2).BorderAround(8)                   osheet.Range("K" & R2).Value = SQlReader2.GetValue(2).ToString                   osheet.Range("K" & R2).BorderAround(8)                   osheet.Range("L" & R2).Value = SQlReader2.GetValue(3).ToString                   osheet.Range("L" & R2).BorderAround(8)             End While             SQlReader2.Close()             SQlReader2 = Nothing             Dim D1 As Integer = 28             Do Until D1 = R2

    J 1 Reply Last reply
    0
    • N nhsal69

      Hi there, I have the following code which takes data from SQL and then adds it to a Excel Spreadsheet. This all works fine, but I need to subtract the column "K" from Column "L" and store it in "M" It inserts the formula into the required number of rows (dependant of the value   "R2") but the formula shows as =(K & D1)-(L & D1) in each cell and not for example =(K 28)-(L 28) etc.... Can you help?? Thanks <code>            Dim SQlQuery2 As String = "select * from growth where Name <> '[Files]'order by FullPath"             Dim SQLCommand2 As New SqlCommand(SQlQuery2, myConn)             Dim SQlReader2 As SqlDataReader = SQLCommand2.ExecuteReader             Dim R2 As Integer = 27             While SQlReader2.Read                   R2 = R2 + 1                   osheet.Range("I" & R2).Value = SQlReader2.GetValue(0).ToString                   osheet.Range("I" & R2).BorderAround(8)                   osheet.Range("J" & R2).Value = SQlReader2.GetValue(1).ToString                   osheet.Range("J" & R2).BorderAround(8)                   osheet.Range("K" & R2).Value = SQlReader2.GetValue(2).ToString                   osheet.Range("K" & R2).BorderAround(8)                   osheet.Range("L" & R2).Value = SQlReader2.GetValue(3).ToString                   osheet.Range("L" & R2).BorderAround(8)             End While             SQlReader2.Close()             SQlReader2 = Nothing             Dim D1 As Integer = 28             Do Until D1 = R2

      J Offline
      J Offline
      John M Bundy
      wrote on last edited by
      #2

      The below works, you just have to build the string, yours is a common issue. Everything in the formula should be in parenthesis, then go back and replace all numbers with " & D1 & "

      osheet.Range("M" & D1).Formula = "=(K" & D1 & ")-(L" & D1 & ")"

      N 1 Reply Last reply
      0
      • J John M Bundy

        The below works, you just have to build the string, yours is a common issue. Everything in the formula should be in parenthesis, then go back and replace all numbers with " & D1 & "

        osheet.Range("M" & D1).Formula = "=(K" & D1 & ")-(L" & D1 & ")"

        N Offline
        N Offline
        nhsal69
        wrote on last edited by
        #3

        Hi there, Thats great, working fine now... Ta v much.....

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups