Results 1 to 9 of 9

Thread: .range

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    .range

    hi there
    I need to display the data from a list of named cells (NameCells) when there is a value for that specific cell.

    here is my code
    VB Code:
    1. Private Sub PrintTest()
    2.     Dim xlApp As Object
    3.     Dim xlWkBk As Object
    4.     Dim xlSheet As Object
    5.     Dim filePath As String
    6.    
    7.     Dim NameVal As String
    8.     Dim DescVal As String
    9.     Dim DeptVal As String
    10.     Dim Purpose As String
    11.        
    12.     ' set values
    13.     NameVal = "AirCon1"
    14.     DescVal = " Air Con"
    15.     DeptVal = "IT"
    16.     PurposeVal = ""
    17.    
    18.     filePath = "c:\Test Excel\Test.xls"
    19.     Set xlApp = CreateObject("Excel.Application")
    20.     Set xlWkBk = xlApp.Workbooks.Open(filePath)
    21.     Set xlSheet = xlWkBk.Worksheets(1)
    22.    
    23.     With xlSheet
    24.    
    25.        
    26.         If NameVal <> Null Then
    27.              
    28.             '.Range(get from the NamedCell).Value = NameVal
    29.            
    30.          End If
    31.      
    32.         ' and so on
    33.      
    34.      End With
    35.     xlWkBk.PrintOut
    36.     xlWkBk.Close SaveChanges:=False
    37.     xlApp.Quit
    38.     Set xlSheet = Nothing
    39.     Set xlWkBk = Nothing
    40.     Set xlApp = Nothing
    41.    
    42. End Sub
    43.  
    44. Private Sub NamedCell(TemplatePath As String, _
    45.                                 Name As String, _
    46.                                 Desc As String, _
    47.                                 Dept As String, _
    48.                                 Purpose As String)
    49.  
    50.                                
    51. Const NamedCell_Name As String = "Name"
    52. Const NamedCell_Desc As String = " Desc"
    53. Const NamedCell_Dept As String = " Dept"
    54. Const NamedCell_Purpose As String = " Purpose"
    55.  
    56. End Sub

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Re: .range

    any help please

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: .range

    Moved to Office Development

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: .range

    If I understand you correctly your looking for the range of a named range?
    VB Code:
    1. 'Range "Test" is from A1 to B5 on sheet1
    2. Debug.Print Workbooks(1).Sheets(1).Range("Test").Name
    3. '=Sheet1!$A$1:$B$5
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Re: .range

    i have already named the cells in my test.xls, if the system does not return a value of a specific field for example 'purpose', then i should only get the named cells that is the name, description and department from the sub NamedCell.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: .range

    Sorry, I dont quite follow you.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Re: .range

    i know its not clear. the situation is i need to get the named cells from a sub which contains a list of defined named cells when there are data for them. For example,

    'for testing
    NameVal = "AirCon1"
    DescVal = " Air Con"
    DeptVal = "IT"
    PurposeVal = ""

    therefore on my excel report i'm going to have the name , description and department values printed but not 'purpose' because it has no value. This is just a concept i'm testing which will be then implemented in a real world application which will have more than 20 named cells.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Re: .range

    please help

  9. #9
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: .range

    Prav:

    I think your "Null" test is your problem. The definition I have for "Null" is:

    Null indicates that no space is reserved in memory for the variable.

    Here is some code to look at with 3 different tests. Play with it and try to understand how to get all 6 messages to occur (hint: look at the definition of Null closely ... to get the "NOT Null" message you'll have to move some code around!):
    Code:
    Option Explicit
    Sub Macro1()
    Dim aRange As Range
    
    'Set a Range variable to refer to a named cell
    Set aRange = Range("CellC3")  '< NameOf $C$3 was set to "CellC3"
    aRange.Select  'Unnecessary - just show that correct cell is loaded in variable
    '
    'Three different test blocks
    If IsNull(aRange) Then
        MsgBox "NULL"
    Else
        MsgBox "NOT Null"
    End If
        
    If IsEmpty(aRange) Then
        MsgBox "IS EMPTY"
    Else
        MsgBox "is NOT empty"
    End If
    
    If aRange.Value = "" Then
        MsgBox "IS EMPTY STRING"
    Else
        MsgBox "is NOT empty string"
    End If
    
    End Sub
    I hope this helps.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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