+ Reply to Thread
Results 1 to 3 of 3

Code to use a varible integer inside a formula in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-28-2008
    Posts
    36

    Code to use a varible integer inside a formula in VBA

    I am working on a workbook that has a summary sheet. The original workbook was created with 40 sheets that were linked back to the summary sheet. I have changed the workbook so there are only 5 sheets to start out with, but have created a macro to add an additional sheet if needed. This way there isnt a bunch of empty sheets when the project is finished. The problem I am having is with the summary sheet. All of the cells come up with a ref error because the sheet that the cell was originally linked to has been deleted. I need to create a macro that will adjust the cell to the name of the sheet that is created with the macro. I think this should be easy because the names of the sheets that are being added are just 6, 7, 8, and so on, so if I could figure out how use a varible integer inside a formula in vb i could loop it through the cells and get it to work, but Im having a heck of a time using these integers. Here is an example of the formula in vb i need.

    Range("b15").Select
         ActiveCell.FormulaR1C1 = "='8'!R3C[18]"
    I need the red 8 to be a variable integer such as "x" that would be based on the sheet count.

    Thanks any help is appreciated
    Last edited by VBA Noob; 02-28-2008 at 01:24 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello westonkw,

    The formula being assigned to the cell by your macro is a string. If the sheet name is number, it needs to converted to string and concatenated with the rest of the formula string.

    Example
      Dim ShtName As String
    
        ShtName = Trim(Str(8))
    
        Range("b15").Select
        ActiveCell.FormulaR1C1 = "='" & ShtName & "'!R3C[18]"
    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    02-28-2008
    Posts
    36
    thanks I used
    ShtName = Sheets.Count
    instead but it works
    Thankyou very much
    Last edited by VBA Noob; 02-28-2008 at 04:23 PM.

+ 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