replacing characters in sql code

baha17

Board Regular
Joined
May 12, 2010
Messages
183
Dear Masters,

I wrote a code which allows me extract data from server for the sales in my company. If I run it for one month period it takes very long time. Actually I realised it is nothing to the with the server, it slows down because it involves other vba command to format the cells. Since it gets over 100K rows it slows down. Is there anyway to replace those command in the sql statement cuch as below? Thanks for the helpw
Baha

Code:
    Sheets("CustomerData").Range("A2:A" & LastRow).Replace What:="V2", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False

and

Code:
 For Each cel In Sheets("CustomerData").Range("A2:A" & LastRow)
If cel.Text <> "" Then
cel.Offset(0, 1) = cel.Offset(0, 3).Text & " " & cel.Offset(0, 2).Text
cel.Offset(0, 16) = _
Application.WorksheetFunction.SumIf(Sheets("CustomerData").Range("A2:A" & LastRow), cel.Value, Sheets("CustomerData").Range("N2:N" & LastRow))

End If
Next cel

Here is my whole code:

Code:
Dim DBFullName, TableName As String
Dim TargetRange As Range
Dim Conn As ADODB.Connection, intColIndex As Integer
Dim cel As Range
Dim TD As Long
Dim qdate As Double
Dim qdate2 As Double
Dim LastRow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
LastRow = Sheets("CustomerData").Range("A" & Sheets("CustomerData").Rows.Count).End(xlUp).Row
On Error Resume Next
qdate = Range("trddate").Value
qdate2 = Range("trddate2").Value
    
    Sheets("CustomerData").Range("AA:AZ").ClearContents
    Sheets("CustomerData").Range("A2:T" & LastRow + 2).ClearContents
    Sheets("CustomerData").Select
    Columns("A:R").AutoFilter                                     'sales.stat_date
    Range("A2").Select
    Selection.Activate
    Set TargetRange = Range("A2")
    Set Conn = New ADODB.Connection
     Conn.Open "driver={SQL Server};" & _
    "server=AACTM12;database=SalesDataBase;"
        Set RecSet = New Recordset
        
    RecSet.Open "SELECT  sales.card_id, '', custmast.fname, custmast.lname, sales.table_id, sales.game_date, " & _
    "sales.empl_id,  " & _
    "sales.dealer_id, '', sales.time_in, sales.time_out, " & _
    "sales.hours, sales.totalin, sales.estwl, sales.avgbet, sales.maxbet, '', " & _
    "setup_casino.pit_name " & _
    "FROM SalesDataBase.dbo.custmast custmast, SalesDataBase.dbo.sales sales, SalesDataBase.dbo.setup_casino setup_casino " & _
    "WHERE sales.card_id = custmast.card_id AND  sales.table_id = setup_casino.table_id AND " & _
    "sales.game_date>='" & qdate & "'And sales.game_date<='" & qdate2 & "'", Conn, , , adCmdText
    TargetRange.CopyFromRecordset RecSet
    
    RecSet.Close
    Set RecSet = Nothing
    Conn.Close
    Set Conn = Nothing
LastRow = Sheets("CustomerData").Range("A" & Sheets("CustomerData").Rows.Count).End(xlUp).Row
    Sheets("CustomerData").Range("A2:A" & LastRow).Replace What:="V2", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False
        
For Each cel In Sheets("CustomerData").Range("A2:A" & LastRow)
If cel.Text <> "" Then
cel.Offset(0, 1) = cel.Offset(0, 3).Text & " " & cel.Offset(0, 2).Text
cel.Offset(0, 16) = _
Application.WorksheetFunction.SumIf(Sheets("CustomerData").Range("A2:A" & LastRow), cel.Value, Sheets("CustomerData").Range("N2:N" & LastRow))

End If
Next cel
    Columns("L:L").NumberFormat = "0.00"
    Columns("J:K").NumberFormat = "h:mm;@"
    Columns("I:I").NumberFormat = "m/d/yyyy"
Columns("A:R").AutoFilter
    Sheets("CustomerData").Select
    Columns("A:B").Select
    Selection.Copy
    Range("AA1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("Q:Q").Select
    Selection.Copy
    Range("AC1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    LastRow = Sheets("CustomerData").Range("AA" & Sheets("CustomerData").Rows.Count).End(xlUp).Row
    ActiveSheet.Range("AA2:AC" & LastRow).RemoveDuplicates Columns:=Array(1, 2, 3), Header:= _
        xlYes
    Range("A1").Select
    
LastRow = Sheets("CustomerData").Range("A" & Sheets("TableData").Rows.Count).End(xlUp).Row
For Each cel In Sheets("CustomerData").Range("E2:E" & LastRow)
If cel.Text <> "" Then
cel.Offset(0, 4) = SaleType(cel.Text)
End If
Next cel
   
Sheets("Main").Select

End Sub

Function SaleType(text_string As String)
If IsNumeric(Mid(text_string, 3, 1)) Then
SaleType = Left(text_string, 2)
Else
SaleType = Left(text_string, 3)
End If
End Function
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This is what i think you need to do

sales.card_id, ' YOUR SQL

replace(sales.card_id,'V2',''),
 
Upvote 0
Yes, looping through each cell one at a time is incredibly inefficient.

Consider replacing using the appropriate Excel formula. For example, B2 could be =IF(A2<>"",D2&" "&C2","").

You can enter the formula in B2:B{last row} in a single VBA statement. For more on improving VBA code see

Beyond Excel's recorder


Dear Masters,

I wrote a code which allows me extract data from server for the sales in my company. If I run it for one month period it takes very long time. Actually I realised it is nothing to the with the server, it slows down because it involves other vba command to format the cells. Since it gets over 100K rows it slows down. Is there anyway to replace those command in the sql statement cuch as below? Thanks for the helpw
Baha

Code:
    Sheets("CustomerData").Range("A2:A" & LastRow).Replace What:="V2", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False

and

Code:
 For Each cel In Sheets("CustomerData").Range("A2:A" & LastRow)
If cel.Text <> "" Then
cel.Offset(0, 1) = cel.Offset(0, 3).Text & " " & cel.Offset(0, 2).Text
cel.Offset(0, 16) = _
Application.WorksheetFunction.SumIf(Sheets("CustomerData").Range("A2:A" & LastRow), cel.Value, Sheets("CustomerData").Range("N2:N" & LastRow))

End If
Next cel

Here is my whole code:

Code:
Dim DBFullName, TableName As String
Dim TargetRange As Range
Dim Conn As ADODB.Connection, intColIndex As Integer
Dim cel As Range
Dim TD As Long
Dim qdate As Double
Dim qdate2 As Double
Dim LastRow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
LastRow = Sheets("CustomerData").Range("A" & Sheets("CustomerData").Rows.Count).End(xlUp).Row
On Error Resume Next
qdate = Range("trddate").Value
qdate2 = Range("trddate2").Value
    
    Sheets("CustomerData").Range("AA:AZ").ClearContents
    Sheets("CustomerData").Range("A2:T" & LastRow + 2).ClearContents
    Sheets("CustomerData").Select
    Columns("A:R").AutoFilter                                     'sales.stat_date
    Range("A2").Select
    Selection.Activate
    Set TargetRange = Range("A2")
    Set Conn = New ADODB.Connection
     Conn.Open "driver={SQL Server};" & _
    "server=AACTM12;database=SalesDataBase;"
        Set RecSet = New Recordset
        
    RecSet.Open "SELECT  sales.card_id, '', custmast.fname, custmast.lname, sales.table_id, sales.game_date, " & _
    "sales.empl_id,  " & _
    "sales.dealer_id, '', sales.time_in, sales.time_out, " & _
    "sales.hours, sales.totalin, sales.estwl, sales.avgbet, sales.maxbet, '', " & _
    "setup_casino.pit_name " & _
    "FROM SalesDataBase.dbo.custmast custmast, SalesDataBase.dbo.sales sales, SalesDataBase.dbo.setup_casino setup_casino " & _
    "WHERE sales.card_id = custmast.card_id AND  sales.table_id = setup_casino.table_id AND " & _
    "sales.game_date>='" & qdate & "'And sales.game_date<='" & qdate2 & "'", Conn, , , adCmdText
    TargetRange.CopyFromRecordset RecSet
    
    RecSet.Close
    Set RecSet = Nothing
    Conn.Close
    Set Conn = Nothing
LastRow = Sheets("CustomerData").Range("A" & Sheets("CustomerData").Rows.Count).End(xlUp).Row
    Sheets("CustomerData").Range("A2:A" & LastRow).Replace What:="V2", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False
        
For Each cel In Sheets("CustomerData").Range("A2:A" & LastRow)
If cel.Text <> "" Then
cel.Offset(0, 1) = cel.Offset(0, 3).Text & " " & cel.Offset(0, 2).Text
cel.Offset(0, 16) = _
Application.WorksheetFunction.SumIf(Sheets("CustomerData").Range("A2:A" & LastRow), cel.Value, Sheets("CustomerData").Range("N2:N" & LastRow))

End If
Next cel
    Columns("L:L").NumberFormat = "0.00"
    Columns("J:K").NumberFormat = "h:mm;@"
    Columns("I:I").NumberFormat = "m/d/yyyy"
Columns("A:R").AutoFilter
    Sheets("CustomerData").Select
    Columns("A:B").Select
    Selection.Copy
    Range("AA1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("Q:Q").Select
    Selection.Copy
    Range("AC1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    LastRow = Sheets("CustomerData").Range("AA" & Sheets("CustomerData").Rows.Count).End(xlUp).Row
    ActiveSheet.Range("AA2:AC" & LastRow).RemoveDuplicates Columns:=Array(1, 2, 3), Header:= _
        xlYes
    Range("A1").Select
    
LastRow = Sheets("CustomerData").Range("A" & Sheets("TableData").Rows.Count).End(xlUp).Row
For Each cel In Sheets("CustomerData").Range("E2:E" & LastRow)
If cel.Text <> "" Then
cel.Offset(0, 4) = SaleType(cel.Text)
End If
Next cel
   
Sheets("Main").Select

End Sub

Function SaleType(text_string As String)
If IsNumeric(Mid(text_string, 3, 1)) Then
SaleType = Left(text_string, 2)
Else
SaleType = Left(text_string, 3)
End If
End Function
 
Upvote 0
Thank you both of you.
mole999 it worked fine. Do you know any other function to get only text from data.example: ABC1234 i just want to get ABC.
Actually I wrote a function as you can see "GameType".But if works very slow over 10K rows. Is there anyway to get data as above?I was thinking to put again replace funtion but there are too many arguments.
Thanks
 
Upvote 0
is it always three characters ?

early this morning glancing over it i thought you were back filling blanks from another column if it was empty
 
Upvote 0
i'm not sure I have worked out which of the columns contain numbers from your sql so modify the bold reference for the correct one

replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(sales.card_id,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9','')

you will need a comma before and after, be guided by what works currently

then you should be able to ditch the conversions in excel
 
Upvote 0
I recon substring(sales.card_id,0,patindex('%[0-9]%',sales.card_id)) is a bit easier to read :), though maybe not as resilient
 
Upvote 0
I prefer the short version, but couldn't find a quick reference from previous asked question out on the web
 
Upvote 0
It's pretty handy for striping out postcode regions which is all I use it for normally, yours is the only way I can think of that wil remove all numbers that doesn't involve a loop so may actually be a better option
 
Upvote 0
Thank you very much for both of you. it looks pretty smart and handy but i could only try when i go work on Sunday. I will let you know the respond.but looks like it will fix my problem.
thank you
have a good weekend
Baha
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top