Results 1 to 25 of 25

Thread: [RESOLVED] How to find (only) the row address of an Excel Cell??

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Resolved [RESOLVED] How to find (only) the row address of an Excel Cell??

    Hey guys
    I am working on a project using visual basic 2005 which involves finding some data and adding it onto an existing excel file. the data in question is a set of numbers.

    Actually, i have to read a bitmap (.bmp) image dimensions in pixels and display them through excel. I am new to VB and dont know how to do this. Please advise.


    cheers
    Abhilash
    Last edited by Abhilash007; Apr 10th, 2007 at 03:44 AM.

  2. #2
    Lively Member
    Join Date
    Mar 2007
    Posts
    78

    Re: How to add data to an Excel file ??

    you can try the following:
    vb Code:
    1. Dim oExcel As New Object
    2.         Dim oBook As Object
    3.         Dim oSheet As Object
    4.    
    5.  
    6.         'oExcel = New excel.application
    7.         'Start a new workbook in Excel.
    8.         oExcel = CreateObject("Excel.Application")
    9.         oBook = oExcel.Workbooks.add ' The Error Occurs here
    10.         'Create an array with 3 columns and 100 rows.
    11.         Dim DataArray(99, 2) As Object
    12.         Dim r As Integer
    13.         For r = 0 To 99
    14.             DataArray(r, 0) = "ORD" & Format(r + 1, "0000")
    15.             DataArray(r, 1) = Rnd() * 1000
    16.             DataArray(r, 2) = DataArray(r, 1) * 0.07
    17.         Next
    18.         'Add headers to the worksheet on row 1.
    19.         oSheet = oBook.ActiveSheet
    20.         oSheet.Range("A1").Value = "Order ID"
    21.         oSheet.Range("B1").Value = "Amount"
    22.         oSheet.Range("C1").Value = "Tax"
    23.         'Transfer the array to the worksheet starting at cell A2.
    24.         oSheet.Range("A2").Resize(100, 3).Value = DataArray
    25.         'Save the Workbook and quit Excel.
    26.         oBook.SaveAs("Book2.xls")
    27.         oSheet = Nothing
    28.         oBook = Nothing
    29.         oExcel.Quit()
    30.         oExcel = Nothing
    31.         GC.Collect()

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to add data to an Excel file ??

    Hey cyberpd
    thx a lot


    cheers
    abhilash

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

    Re: How to add data to an Excel file ??

    For more info you could check the Office Development FAQ (link in my signature).

    This FAQ item is of relevance to your question.
    http://vbforums.com/showthread.php?t=406637
    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
    Lively Member
    Join Date
    Mar 2007
    Posts
    78

    Re: How to add data to an Excel file ??

    You are most Welcome.
    Always visit FAQ and old RESOLVED threads for more help...

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to add data to an Excel file ??

    Hey!
    I am actually required to add data into an 'existing' excel file (unitemp.xls).. i think the code you gave me is one to create a new excel file and then add data onto that.. can you please gimme the code in case i need to add data to an existing file instead of creating a new file..

    thx
    Abhilash

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

    Re: How to add data to an Excel file ??

    Just switch this line ...
    oBook = oExcel.Workbooks.add

    To this line ...
    oBook = oExcel.Workbooks.Open("C:\Whatever.xls")
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to add data to an Excel file ??

    thx rob!! the code works just fine...


    I am looking for a code in VB 2005 to search for a string in a particular column in an existing excel file. the string to be searched will be the filename of a image (ex: 00C5) w/o the extension. The code should look for the given string in the first column (col. A). the code should return the row location of the searched string.

    please advise

    cheers
    Abhilash

  9. #9
    Lively Member
    Join Date
    Mar 2007
    Posts
    78

    Re: How to add data to an Excel file ??

    abhilash..
    i have already posted a code that prints a order number
    one of the order number is ORD0085
    i am going to find the same in the worksheet and if i find it out i print "HI"

    vb Code:
    1. Dim oExcel As New Object
    2.         Dim oBook As Object
    3.         Dim oSheet As Object
    4.         Dim c
    5.  
    6.         Try
    7.             'Start a new workbook in Excel.
    8.             oExcel = CreateObject("Excel.Application")
    9.             oBook = oExcel.Workbooks.open("C:\Book2.xls")
    10.  
    11.             With oBook
    12.                 oSheet = .Worksheets("Sheet1")
    13.             End With
    14.             oSheet = oBook.ActiveSheet
    15.  
    16.             For Each c In oSheet.Range("A2:A101")
    17.                 If c.Value = "ORD0085" Then
    18.                     MsgBox("HI")
    19.                 End If
    20.             Next c
    21.  
    22.         Catch ex As Exception
    23.             MsgBox(ex.Message)
    24.             oSheet = Nothing
    25.             oBook = Nothing
    26.             oExcel.visible = True
    27.             oExcel = Nothing
    28.             GC.Collect()
    29.         End Try

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

    Re: How to add data to an Excel file ??

    Just noticed, you shouldnt be making an explicit call to the Garbage Collector.
    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

  11. #11
    Lively Member
    Join Date
    Mar 2007
    Posts
    78

    Re: How to add data to an Excel file ??

    You are right RobDog888

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to add data to an Excel file ??

    Hey cyberpod...

    thx for your code.. but i actually just need to find the location of the searched string (ex. A3 or A51 etc).. will it be in osheet.Range or something.. please advise


    cheers
    Abhilash

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

    Re: How to add data to an Excel file ??

    .Find should work for 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

  14. #14
    Lively Member
    Join Date
    Mar 2007
    Posts
    78

    Re: How to add data to an Excel file ??

    the following code is an example given in Microsoft Excel Visual Basic Reference This example finds all cells in the range A1:A500 on worksheet one that contain the value 2 and changes it to 5.

    vb Code:
    1. With Worksheets(1).Range("a1:a500")
    2.     Set c = .Find(2, lookin:=xlValues)
    3.     If Not c Is Nothing Then
    4.         firstAddress = c.Address
    5.         Do
    6.             c.Value = 5
    7.             Set c = .FindNext(c)
    8.         Loop While Not c Is Nothing And c.Address <> firstAddress
    9.     End If
    10. End With

    a little bit of modification will meet your requirement. the cell position is given by firstaddress..

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

    Re: How to add data to an Excel file ??

    Yes, as I suggested but that is the VBA code and will need re-writting in .NET code.
    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

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to add data to an Excel file ??

    hey.. thx for the code...

    can anyone help me translate the above VBA code into .net code.. i rilli need it... thx


    cheers
    Abhilash

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

    Re: How to add data to an Excel file ??

    Integrate it with post #9 code as it contains the same converted code sections.
    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

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to add data to an Excel file ??

    Hey Rob!
    I did wad u said but the address i get is a little distorted
    (eg. $C$17) i used c.address.Tostring as shown below..

    is it possible to remove the $ signs and just get the location.


    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim oExcel As New Object
            Dim oBook As Object
            Dim oSheet As Object
            Dim c
    
    
            Try
                'Start a new workbook in Excel.
                oExcel = CreateObject("Excel.Application")
                oBook = oExcel.Workbooks.open("C:\Book2.xls")
    
                With (oBook)
                    oSheet = .Worksheets("Sheet1")
                End With
                oSheet = oBook.ActiveSheet
    
                For Each c In oSheet.Range("C3:C101")
                    If c.Value = "ORD0015" Then
                        MessageBox.Show("Location: " & c.address.ToString)
                    End If
                Next (c)
    
            Catch ex As Exception
                MsgBox(ex.Message)
                oSheet = Nothing
                oBook = Nothing
                oExcel.visible = True
                oExcel = Nothing
                GC.Collect()
            End Try
    
        End Sub
    End Class
    cheers
    Abhilash

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

    Re: How to add data to an Excel file ??

    Here is the help topic for Address:
    http://office.microsoft.com/client/h...AiH-0&respos=3

    You can use
    .Address(RowAbsolute:=False, ColumnAbsolute:=False).ToString and that should give you what you want.
    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

  20. #20

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to add data to an Excel file ??

    Hey Rob!
    That works perfect.. thx

    cheers
    Abhilash

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

    Re: How to add data to an Excel file ??

    No prob, glad to help. I like to try to "teach a hungry man how to fish rather then just give him a fish"

    Ps, dont forget to Resolve your thread now that its solved
    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

  22. #22

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to find (only) the row address of an Excel Cell??

    Hey Guys!!

    I have encountered another problem while dealing with Excel. I have a code which searches for a string in an Excel file and gives the address of the cell where the string is found. (Please refer to post #18 for the code)


    I require only the row number of the cell as i need to use it later in the code.. Is there a way to get only the row number or maybe save it to a variable...


    Please advise



    cheers
    Abhilash

  23. #23
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to find (only) the row address of an Excel Cell??

    I require only the row number of the cell as i need to use it later in the code.. Is there a way to get only the row number or maybe save it to a variable...
    Does this help? A very basic example...

    vb Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. Dim rowno As String
    4.  
    5. 'Say the cell is A55 for which you want the Row number
    6. aaa = Range("A55").Address
    7.  
    8. 'Reverse Looping and Checking for "$"
    9. For i = Len(Trim(aaa)) To 1 Step -1
    10.  
    11.     If Mid(aaa, i, 1) <> "$" Then
    12.  
    13.         rowno = rowno & Mid(aaa, i, 1)
    14.  
    15.     Else
    16.         'The first "$" found from Right
    17.         'Exit Loop
    18.         Exit For
    19.     End If
    20.  
    21. Next i
    22.  
    23. MsgBox rowno
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  24. #24

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to find (only) the row address of an Excel Cell??

    Hey!
    Your code does separate the row number but it inverts the number when storing it to the variable... for example Row number for "A65" will come out to be "56".. I am trying to find out to solve this but havent made progress yet

    Please advise..

    cheers
    Abhilash

  25. #25
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to find (only) the row address of an Excel Cell??

    oops...

    change this

    rowno = rowno & Mid(aaa, i, 1)

    to

    rowno = Mid(aaa, i, 1) & rowno

    in the above code...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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