Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3968394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:50:14+00:00 2026-05-20T03:50:14+00:00

I am trying to write a VBA script in Excel 2003 (not my choice

  • 0

I am trying to write a VBA script in Excel 2003 (not my choice of version) to partition a predefined range on a worksheet into ten named ranges. The worksheet name is “paste_data” and the ‘block’ of cells that I want to confine the script to is A4:AO111. Sometimes, when I run the script, it works, but at other times, it seems to shift the effective starting cell from A4 to another cell. Here is an example of bad results (sorry, I can’t post an image because I’m new):

The named range table.emergency.count refers to range V6:AO25 when it should refer to range V4:AO23.

My code is here:

Sub tables_assign()
Dim j As Integer
Dim range_ref, range_name, rref As String
Dim tbles(1 To 10) As String
Dim rw1, rw2 As Integer
'##########################################################################################
'CREATION AND NAMING OF TABLES
'##########################################################################################
tbles(1) = "table.emergency.score": tbles(2) = "table.emergency.count": tbles(3) = "table.eol.score": tbles(4) = "table.eol.count": tbles(5) = "table.inpatient.score": tbles(6) = "table.inpatient.count": tbles(7) = "table.outpatient.score": tbles(8) = "table.outpatient.count": tbles(9) = "table.sds.score": tbles(10) = "table.sds.count"
For j = 1 To 10
    If j Mod 2 <> 0 Then
        If j = 1 Then
            rw1 = 4
            rw2 = 23
        Else
            rw1 = 4 + 22 * Application.WorksheetFunction.Ceiling((j / 2 - 1), 1)
            rw2 = 23 + 22 * Application.WorksheetFunction.Ceiling((j / 2 - 1), 1)
        End If
        rref = Trim(Application.WorksheetFunction.Substitute("=paste_data!A" & Str(rw1) & ":T" & Str(rw2), " ", ""))
        ActiveWorkbook.Names.Add tbles(j), rref
    Else
        If j = 2 Then
            rw1 = 4
            rw2 = 23
        Else
            rw1 = 4 + 22 * (j / 2 - 1)
            rw2 = 23 + 22 * (j / 2 - 1)
        End If
        rref = Trim(Application.WorksheetFunction.Substitute("=paste_data!V" & Str(rw1) & ":AO" & Str(rw2), " ", ""))
        ActiveWorkbook.Names.Add tbles(j), rref
    End If
Next j
End Sub

Does anyone have an idea why this would happen? My hunch is that the worksheet’s ‘usedrange’ is the culprit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:50:15+00:00Added an answer on May 20, 2026 at 3:50 am

    When you use relative references in defined names, the definition is relative to the activecell. To avoid that, use absolute references, like $V$4:$AO$23. With absolute references, the named range will always point to the same cells.

    Example:
    Select cell A1 and define the name test_relative as “=A1”. Now select cell B10 and reopen the defined name box, select test_relative and you’ll see something like “=Sheet1!B10”

    To fix your code, insert the $ in the range references

    rref = Trim(Replace("=paste_data!$A$" & Str(rw1) & ":$T$" & Str(rw2), " ", ""))
    

    Also note that

    Dim rw1, rw2 As Integer
    

    dimensions rw1 as a Variant. Use

    Dim rw1 As Integer, rw2 As Integer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a VBA script which imports all of the Excel
I am trying to write a vba function that takes a range as an
I' trying to write an xml file from Excel VBA using Microsoft XML 6.0.
I'm trying to write an R-Excel vba addin and am having trouble using GetArrayToVBA
I'm trying to write a vba macro for a group tha has one workbook
I'm trying to convert a flat file into an Excel-readable format. We receive the
I am trying to write a WSH logon script. Administrators throughout the company need
I am trying to populate an excel spreadsheet using VBA (is actually something different
The VBA I'm trying to write is fairly simple but Ive never written VBA
My current knowledge: If you are trying to write text files in vbscript /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.