|
-
Mar 30th, 2007, 01:23 AM
#1
Thread Starter
Member
[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.
-
Mar 30th, 2007, 08:46 AM
#2
Lively Member
Re: How to add data to an Excel file ??
you can try the following:
vb Code:
Dim oExcel As New Object
Dim oBook As Object
Dim oSheet As Object
'oExcel = New excel.application
'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.add ' The Error Occurs here
'Create an array with 3 columns and 100 rows.
Dim DataArray(99, 2) As Object
Dim r As Integer
For r = 0 To 99
DataArray(r, 0) = "ORD" & Format(r + 1, "0000")
DataArray(r, 1) = Rnd() * 1000
DataArray(r, 2) = DataArray(r, 1) * 0.07
Next
'Add headers to the worksheet on row 1.
oSheet = oBook.ActiveSheet
oSheet.Range("A1").Value = "Order ID"
oSheet.Range("B1").Value = "Amount"
oSheet.Range("C1").Value = "Tax"
'Transfer the array to the worksheet starting at cell A2.
oSheet.Range("A2").Resize(100, 3).Value = DataArray
'Save the Workbook and quit Excel.
oBook.SaveAs("Book2.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
-
Apr 1st, 2007, 03:38 AM
#3
Thread Starter
Member
Re: How to add data to an Excel file ??
Hey cyberpd
thx a lot
cheers
abhilash
-
Apr 1st, 2007, 05:17 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 2nd, 2007, 01:09 AM
#5
Lively Member
Re: How to add data to an Excel file ??
You are most Welcome.
Always visit FAQ and old RESOLVED threads for more help...
-
Apr 2nd, 2007, 01:28 AM
#6
Thread Starter
Member
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
-
Apr 2nd, 2007, 03:21 AM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 2nd, 2007, 03:58 AM
#8
Thread Starter
Member
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
-
Apr 2nd, 2007, 08:57 AM
#9
Lively Member
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:
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("A2:A101")
If c.Value = "ORD0085" Then
MsgBox("HI")
End If
Next c
Catch ex As Exception
MsgBox(ex.Message)
oSheet = Nothing
oBook = Nothing
oExcel.visible = True
oExcel = Nothing
GC.Collect()
End Try
-
Apr 2nd, 2007, 11:17 AM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 3rd, 2007, 12:24 AM
#11
Lively Member
Re: How to add data to an Excel file ??
-
Apr 3rd, 2007, 02:47 AM
#12
Thread Starter
Member
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
-
Apr 3rd, 2007, 02:48 AM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 3rd, 2007, 06:54 AM
#14
Lively Member
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:
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
a little bit of modification will meet your requirement. the cell position is given by firstaddress..
-
Apr 3rd, 2007, 12:49 PM
#15
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 3rd, 2007, 10:29 PM
#16
Thread Starter
Member
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
-
Apr 3rd, 2007, 10:37 PM
#17
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 4th, 2007, 02:21 AM
#18
Thread Starter
Member
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
-
Apr 4th, 2007, 02:41 AM
#19
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 4th, 2007, 02:50 AM
#20
Thread Starter
Member
Re: How to add data to an Excel file ??
Hey Rob!
That works perfect.. thx
cheers
Abhilash
-
Apr 4th, 2007, 03:14 AM
#21
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 10th, 2007, 03:52 AM
#22
Thread Starter
Member
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
-
Apr 10th, 2007, 08:16 AM
#23
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:
Private Sub CommandButton1_Click()
Dim rowno As String
'Say the cell is A55 for which you want the Row number
aaa = Range("A55").Address
'Reverse Looping and Checking for "$"
For i = Len(Trim(aaa)) To 1 Step -1
If Mid(aaa, i, 1) <> "$" Then
rowno = rowno & Mid(aaa, i, 1)
Else
'The first "$" found from Right
'Exit Loop
Exit For
End If
Next i
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
-
Apr 10th, 2007, 10:30 PM
#24
Thread Starter
Member
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
-
Apr 11th, 2007, 01:05 AM
#25
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|