Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replace words from ADO recordset


Hi expert,

I need to write a little excel vbscript with ADO recordset code to
change the first column, whenever the cell starts with first letter 'D'
(which means a district number), then I need to replace the cell to the
a district manager name.

First of all, I need to loop every row in column A only to find any
cell with a 'D' prefixed word, for example, D1009.
Then open a ADO connect to SQL Server, as long as I find the cell to
match DISTRICT, then it needs to be replaced by DISTRICT_MGR

Can someone help me to finish this VBScript? Thank you!

-------------------------------------------------------------------

Sub ReplaceTheDs()
Dim MaxRows As Long
Dim RowCounter As Long
Dim FoundRow
With Worksheets("Growing Real Sales")
MaxRows = .Range("a1").End(xlDown).Row
For RowCounter = 1 To MaxRows
If Left(.Range("a" & RowCounter).Value, 1) = "D" Then
'Open ADO Recordset here
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

Server_Name = "myservername"
Database_Name = "mydbname" ' Enter your database name here
User_ID = "id"
Password = "pw"
SQLStr = "SELECT 'D' + CAST(District_Num AS char(5)) AS District,
District_Mgr FROM micros.Store_Table"

Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name &
";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"

rs.Open SQLStr, Cn, adOpenStatic

' HOW TO REPLACE THE D TO DISTRICT MANAGER'S NAME????

rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing

End If
Next RowCounter
End With
End Sub


--
jenhu
------------------------------------------------------------------------
jenhu's Profile: http://www.excelforum.com/member.php...o&userid=28969
View this thread: http://www.excelforum.com/showthread...hreadid=527186

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace several words to One Wanna Learn Excel Discussion (Misc queries) 2 April 30th 08 01:56 PM
multiple sets words replace dk New Users to Excel 5 April 10th 08 12:20 AM
Search And Replace Whole Words Mary H Excel Programming 2 December 21st 05 08:58 PM
How can i find and replace words mean from one file to another?? atarodi Excel Programming 0 October 28th 05 05:46 PM
Excel to replace words ? Andy100 New Users to Excel 13 August 28th 05 05:17 PM


All times are GMT +1. The time now is 06:34 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"