+ Reply to Thread
Results 1 to 5 of 5

Active sheet / cell programming question

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Active sheet / cell programming question

    I have the following code


        'Insert/put HW formula
         Workbooks(Gradebook_Filename).Worksheets(Gradebook_Sht_Nm_Roster).Range("G2").Formula = _
                            "=IF(ISERROR(VLOOKUP(A2," & Gradebook_Sht_Nm_HW & "!$A$2:$V$" & _
                            Rster_lst_row & ",22,FALSE)),""NA"", " & "VLOOKUP(A2," & Gradebook_Sht_Nm_HW & "!$A$2:$V$" & _
                            Rster_lst_row & ",22,FALSE))"

    Which i thought would "select" cell G2 on the appropriate sheet and then paste the formula. However, what i get is the formula pasted into a totally different cell. To get the formula pasted into G2 i actually need to have the following code.


     'For some reason i need the next statement otherwise the formulas are not put into the correct place
        Application.Goto Workbooks(Gradebook_Filename).Worksheets(Gradebook_Sht_Nm_Roster).Range("G2")
        
        'Insert/put HW formula
         Workbooks(Gradebook_Filename).Worksheets(Gradebook_Sht_Nm_Roster).Range("G2").Formula = _
                            "=IF(ISERROR(VLOOKUP(A2," & Gradebook_Sht_Nm_HW & "!$A$2:$V$" & _
                            Rster_lst_row & ",22,FALSE)),""NA"", " & "VLOOKUP(A2," & Gradebook_Sht_Nm_HW & "!$A$2:$V$" & _
                            Rster_lst_row & ",22,FALSE))"


    Does anyone know why? I think its a "selection" issue but i am not sure.

    DAvid

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529
    Try this code, change the names of course...
    Sub InsertFormula()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim r As Range
        Dim f As String
        Set wb = Workbooks("11th Week.XLS")
        Set ws = wb.Worksheets("Sheet1")
        Set r = ws.Range("G1")
        f = "=Your Formula"
        r = f
    End Sub

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    thanks

    thanks.......i will check it out. thanks.

    So basically, i need to do a "set" first..........

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Your first construct looks fine to me. If you're using Excel 2007, you can shorten it to

    Workbooks(Gradebook_Filename).Worksheets(Gradebook_Sht_Nm_Roster).Range("G2").Formula = _
       "=IFERROR(VLOOKUP(A2," & Gradebook_Sht_Nm_HW & "!$A$2:$V$" & Rster_lst_row & ",22,FALSE), NA())"
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    thanks

    Thanks. But for some reason i need the additional line of code with the "Application.goto" statement otherwise the formula is put into the wrong location. Cant figure out why.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1