I have written the below VBA. What it is trying to do: It pulls data from another sheet copies and pastes it, does a find and replace and then uploads the data to a database.
The problem: something in the code is turning off the Find and Replace function, so once it gets to this point, it gives an error. Once I close the macro and try a regular find and replace, no matter what I try to find, it says it cannot find it. Does anyone see anything in this code that would cause the find and replace to no longer work?
Sub RRUpload() Dim cnt As New ADODB.Connection, _ rst As New ADODB.Recordset, _ dbPath As String, _ tblName As String, _ rngColHeads As Range, _ rngTblRcds As Range, _ colHead As String, _ rcdDetail As String, _ ch As Integer, _ cl As Integer, _ notNull As Boolean Dim ws As Worksheet Set ws = Worksheets("RentRollUpload") Set ms = Worksheets("DB") 'Set the string to the path of the database as defined on the worksheet dbPath = ms.Range("A50") tblName = ms.Range("A57") Set rngColHeads = ws.Range("RRhdrs") Set rngTblRcds = ws.Range("RRdat") 'Concatenate a string with the names of the column headings colHead = " (" For ch = 1 To rngColHeads.Count colHead = colHead & rngColHeads.Columns(ch).Value Select Case ch Case Is = rngColHeads.Count colHead = colHead & ")" Case Else colHead = colHead & "," End Select Next ch 'Open connection to the database cnt.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & dbPath & ";" 'Begin transaction processing 'On Error GoTo EndUpdate cnt.BeginTrans 'Insert records into database from worksheet table For cl = 1 To rngTblRcds.Rows.Count 'Assume record is completely Null, and open record string for concatenation notNull = False rcdDetail = "('" 'Evaluate field in the record For ch = 1 To rngColHeads.Count Select Case rngTblRcds.Rows(cl).Columns(ch).Value 'if empty, append value of null to string Case Is = Empty Select Case ch Case Is = rngColHeads.Count rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL)" Case Else rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL,'" End Select 'if not empty, set notNull to true, and append value to string Case Else notNull = True Select Case ch Case Is = rngColHeads.Count rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "')" Case Else rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "','" End Select End Select Next ch 'If record consists of only Null values, do not insert it to table, otherwise 'insert the record Select Case notNull Case Is = True rst.Open "INSERT INTO " & tblName & colHead & " VALUES " & rcdDetail, cnt Case Is = False 'do not insert record End Select Next cl EndUpdate: 'Check if error was encounted If Err.Number <> 0 Then 'Error encountered. Rollback transaction and inform user On Error Resume Next cnt.RollbackTrans MsgBox "Error! RentRoll Data upload was not succesful!", vbCritical, "Error!" Else On Error Resume Next cnt.CommitTrans MsgBox "RentRoll Data Upload was Succesful.", vbInformation, "Success!" End If 'Close the ADO objects cnt.Close Set rst = Nothing Set cnt = Nothing On Error GoTo 0 End Sub
Last edited by mjhopler; 08-30-2011 at 04:42 PM.
I just realized it is VBA ran before this that causes the issue. I will post the true problem in a second.
Below is the code presenting problems. It pulls data from another worksheet and pastes it in a sheet to be ready to be uploaded to a database. It is after this step that the find and replace no long works (works before running, but not after).
Sub PullQCRetailDT() Dim maxvalue As Integer Dim maxvalue2 As Integer Application.ScreenUpdating = False 'Empty Contents of Upload file UploadFile = ActiveWorkbook.Name Sheets("Upload").Range("A8:AZ20").ClearContents Sheets("Upload").Range("A31").ClearContents Sheets("Upload").Range("A42:AZ44").ClearContents Sheets("Upload").Range("A53").ClearContents Sheets("RentRollUpload").Range("A5:H1500").ClearContents 'Activate target file and select data to copy Workbooks.Open Filename:= _ Sheets("Upload").Range("E2"), ReadOnly:=True 'Select data from CF Summary tab Sheets("Cashflow Summary").Activate vFinancialData = Range("A5:AV17") vMasterLoanData = Range("A33:J33") vAddLoanData = Range("A43:M45") vPropertyData = Range("A53:M53") 'Select data from RR tab Sheets("RentRoll").Activate maxvalue = ActiveSheet.Range("AB1").Value maxvalue2 = ActiveSheet.Range("AC1").Value vRRDate = Range("C4") vTenants = ActiveSheet.Range("A9:H" & maxvalue) 'Activate upload file and place data to be uploaded Workbooks(UploadFile).Activate Sheets("Upload").Activate Range("A8:AV20") = vFinancialData Range("A31:J31") = vMasterLoanData Range("A42:M42") = vAddLoanData Range("A53:M53") = vPropertyData Range("N53") = vRRDate ' Replace ' by blanks Range("A8:AV20").Select Selection.Replace What:="'", Replacement:="", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Range("A31:J31").Select Selection.Replace What:="'", Replacement:="", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Range("A42:M42").Select Selection.Replace What:="'", Replacement:="", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Range("A53:M53").Select Selection.Replace What:="'", Replacement:="", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Sheets("RentRollUpload").Activate ActiveSheet.Range("A5:H" & maxvalue - 4) = vTenants Range("B5:B" & maxvalue).Select Selection.Replace What:="'", Replacement:="", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False 'Delete excess blank rows in RentRollUpload 'ActiveSheet.Rows(maxvalue2 & ":1500").Select 'Selection.EntireRow.Delete Application.ScreenUpdating = True End Sub
Maybe I've misunderstood, but I can't see a find or replace in that code.
Never mind, the problem I encounter most often is that if you use a find or replace in a macro it sets the parameters you've used as the default ones. So loads of times I've used LookAt:=xlWhole and then wondered why manual finds weren't working.
So, have you checked what search options you're using after the macro has run?
I am using LookAt:=XlWhole. It is changing the options to Match entire cell contents. How do I keep it from doing that.
The Find and Replace code is in the second code posting and follows the comment ' Replace ' by blanks
at a guess add a line at end to change back to what you want
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and a dabbler in Cisco
where does code go ?
look here
how to insert code
how to enter array formula
why use -- in sumproduct
recommended reading
wiki Mojito
how to say no convincingly
most important thing you need
Martin Wilson: SPV
and RSMBC
Sorry, I was typing my response on my iPhone and it took a while - you posted the updated code while I was doing my one-finger typing bit.
Martin's suggestion seems the best idea - add another .find at the end of the code, as that will set the search defaults to whatever you specify. I must admit that this is one of the little Excel quirks that annoys me the most, especially as I can't seem to write more than 2 lines of code without using a .Find![]()
Thank you for your help!!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks