Results 1 to 3 of 3

Thread: Excel VBA: Define a named range.... [solved]

  1. #1

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Excel VBA: Define a named range.... [solved]

    hey, i have made a sub, to insert a new invoice record into my "flat file db" ETC, now, i have got it all working, except, i dont know how to 'include' the new item in the FFDB list..

    i have got this code so far:

    VB Code:
    1. Option Explicit
    2. Sub MakeNewRecord()
    3.  
    4. ' MakeNewRecord Macro
    5. ' Created by Wayne Pearsall to insert a new invoice record
    6. ' in the invoice list when entering a booking query.
    7.  
    8.     If MsgBox("Are you sure you wish to create a new invoice record?" & vbCrLf & _
    9.               "If you have made changes to a record, these changes will be lost!", _
    10.               vbQuestion + vbYesNo, "Insert Invoice Record?") = vbNo Then Exit Sub
    11.  
    12.     ' Set the application's cursor to the hourglass
    13.     ' (so as they know a process is going on)
    14.     Application.Cursor = xlWait
    15.  
    16.     ' Create the variables which are going to hold the
    17.     ' temporary data within this macro
    18.     ' lRecordToInsertAt: The row number that is going to be used as
    19.     '                    row in the cell to select
    20.     ' sCellToInsertAt:   The cell reference which will be selected
    21.     '                    before inserting a new row
    22.     ' lRecordRange:      The limit to select to, before setting the
    23.     '                    invoice_list's named range.
    24.     ' lRecordCount:      The number of records now in the list.
    25.     Dim sCellToInsertAt As String, _
    26.         lRecordToInsertAt As Long, _
    27.         lRecordRange As Long, _
    28.         lRecordCount As Long
    29.        
    30.     ' Get the record count, and add three
    31.     ' (since the list starts AFTER row two)
    32.     lRecordToInsertAt = CLng(Range("record_count").Value) + 3
    33.     ' Put together the cell reference using the row number
    34.     sCellToInsertAt = "'Invoice List'!A" & lRecordToInsertAt
    35.    
    36.     ' insert a row (Which is set to the bottom of the list)
    37.     Range(sCellToInsertAt).EntireRow.Insert
    38.    
    39.     ' Get the new record count
    40.     lRecordCount = CLng(Range("record_count").Value) + 1
    41.    
    42.     ' set the new record count into the record_count cell
    43.     Range("record_count").Value = lRecordCount
    44.    
    45.     ' set the record count in the new invoice record to
    46.     ' the record count (as the invoice #)
    47.     ' (This will be the same cell as we inserted the new line at)
    48.     Range(sCellToInsertAt).Value = lRecordCount
    49.  
    50.     ' calculate the row count
    51.     lRecordRange = lRecordToInsertAt + 1
    52.    
    53.     ' Select the list range
    54.     'Range("A3:A" & lRecordRange).Select
    55.     ' Set the cell naming
    56.    
    57.     ' ----------------------------------------------------------------------
    58.     '
    59.     '
    60.     '   This is where i need to set the selected range's name to "invoice_list"
    61.     '
    62.     '
    63.     ' ----------------------------------------------------------------------
    64.    
    65.     MsgBox "Need to setup the re-setting the named range for ""invoice_list"""
    66.    
    67.     ' Set the receive date of the query into the database sheet
    68.     Range("'Invoice List'!B" & lRecordToInsertAt).Value = Date
    69.     ' set the recieve date to todays date
    70.     Range("C10").Value = Date
    71.    
    72.     With Range("C9")
    73.         ' and then select the pick list
    74.         .Select
    75.         ' and set it to the new invoice record
    76.         .Value = lRecordCount
    77.     End With
    78.    
    79.     ' Set the application's cursor back to the default
    80.     ' (This lets them know that the process is complete)
    81.     Application.Cursor = xlDefault
    82.    
    83. End Sub

    i recorded this macro, thinking i could b able to edit the code of it, but i cannot figure out where it sets the cell reference....


    VB Code:
    1. 'BTW: this refers to the cell:
    2. ' ='Invoice Form'!$D$4
    3.  
    4. Sub Macro1()
    5. '
    6. ' Macro1 Macro
    7. ' Macro recorded 25/10/2003 by Wayne Pearsall
    8. '
    9. '
    10.     ActiveWorkbook.Names.Add Name:="new", RefersToR1C1:="='Invoice Form'!R4C4"
    11. End Sub


    can anybody help with this please?

    tnx.
    Last edited by wpearsall; Oct 27th, 2003 at 08:45 AM.
    Wayne

  2. #2
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    ActiveWorkbook.Names.Add Name:="new", RefersToR1C1:="='Invoice Form'!R4C4"

    'Invoice Form' is your worksheet and R4C4 (Row 4, Column 4) is the cell address.

  3. #3

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    thanks dood.

    dat fixed ma problem
    Wayne

Posting Permissions

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



Click Here to Expand Forum to Full Width