Looking for help with a macro/vba for find/copy/paste
Closed
nsettlemire
Posts
4
Registration date
Sunday 1 February 2015
Status
Member
Last seen
29 April 2015
-
1 Feb 2015 à 14:36
RayH Posts 122 Registration date Tuesday 31 August 2010 Status Contributor Last seen 20 June 2016 - 2 Feb 2015 à 22:28
RayH Posts 122 Registration date Tuesday 31 August 2010 Status Contributor Last seen 20 June 2016 - 2 Feb 2015 à 22:28
Related:
- Looking for help with a macro/vba for find/copy/paste
- Vba case like - Guide
- Excel online vba - Guide
- How to copy paste youtube link on android - Guide
- Copy paste e with accent - Guide
- Vba color index - Guide
3 responses
RayH
Posts
122
Registration date
Tuesday 31 August 2010
Status
Contributor
Last seen
20 June 2016
26
1 Feb 2015 à 18:11
1 Feb 2015 à 18:11
This is something that you don't see much of on here but it is very worthwhile:
Sub SQL()
' Requires "Microsoft ActiveX Data Objects 6.1 Library"
' Add using Tools, Reference
'Workbook MUST be saved at least once for this to work
Dim sqlstr As String
Dim rs As New ADODB.Recordset
Dim conn As ADODB.Connection
'######################################################################
'## Turn off all Application updating stuff
'######################################################################
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Set conn = New ADODB.Connection
MyName = ThisWorkbook.Name
myPath = ThisWorkbook.Path
MyFullname = ThisWorkbook.FullName
strConn = "DSN=Excel Files;DBQ=" & MyFullname & ";DefaultDir=" & myPath & ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"
conn.Open strConn
conn.CommandTimeout = 0
' Find a unique list of non blank parts WITHOUT the suffix
' and the highest value of the last character for that part.
' where the last character is between A and Z
sqlstr = ""
sqlstr = sqlstr + "select distinct left(part,len(part)-4) as part2, "
sqlstr = sqlstr + " MAX(right(part,1)) as maxright"
sqlstr = sqlstr + " FROM [" + ActiveSheet.Name + "$]"
sqlstr = sqlstr + " WHERE part IS not NULL "
sqlstr = sqlstr + " and ucase(part) like '%[A-Z]'"
sqlstr = sqlstr + " group by left(part,len(part)-4)"
'Run It!
rs.Open sqlstr, conn
' format range as Text
ActiveSheet.Range("D2:E" & Rows.Count).NumberFormat = "@"
' Clear the area
ActiveSheet.Range("D2:E" & Rows.Count).ClearContents
' put the dataset retrieved into the range
ActiveSheet.Range("D2").CopyFromRecordset rs
'close the open connection and recordset
rs.Close
conn.Close
Set conn = Nothing
'######################################################################
'## Turn on all Application updating stuff
'######################################################################
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
RayH
Posts
122
Registration date
Tuesday 31 August 2010
Status
Contributor
Last seen
20 June 2016
26
1 Feb 2015 à 18:16
1 Feb 2015 à 18:16
nsettlemire
Posts
4
Registration date
Sunday 1 February 2015
Status
Member
Last seen
29 April 2015
1 Feb 2015 à 20:36
1 Feb 2015 à 20:36

Thank you very much for your work on this. I have been trying to get your code to run on my workbook. I did add the Active X library as stated. However when I try to run the Macro, I get the following error.
System Error &H80040E10 (-2147217904)
I'm sure I have something not quite right.
I really appreaciate your efforts, and I have learned a great deal by studding this example.
RayH
Posts
122
Registration date
Tuesday 31 August 2010
Status
Contributor
Last seen
20 June 2016
26
1 Feb 2015 à 21:32
1 Feb 2015 à 21:32
Try downloading this and running it and see if you get the same error.
http://speedy.sh/cP2UT/sqltest2.xlsm
Which part is causing the error? Can you step through it using F8 to see.
Can you upload your sheet? Send link to me via Private message.
Do you have Access installed? It might be Jet related.
http://speedy.sh/cP2UT/sqltest2.xlsm
Which part is causing the error? Can you step through it using F8 to see.
Can you upload your sheet? Send link to me via Private message.
Do you have Access installed? It might be Jet related.
nsettlemire
Posts
4
Registration date
Sunday 1 February 2015
Status
Member
Last seen
29 April 2015
2 Feb 2015 à 12:49
2 Feb 2015 à 12:49
Your sheet seems to work great! Thank you again for the help. I'll let you know if I need any other tweaks.
nsettlemire
Posts
4
Registration date
Sunday 1 February 2015
Status
Member
Last seen
29 April 2015
2 Feb 2015 à 20:41
2 Feb 2015 à 20:41
Hi Ray H.
I could use your help on one additional detail. Some of the part numbers have values greater that Z. As you know I told you we wanted to identify the "largest" letter from A-Z, however in some cases we go to AA-AZ after Z has been used. Can you please help me with the code to where it will determine the "largest" between A and AZ? I know the area of the code that requires modification, but I'm afraid I will mess it up if I try to change it.
Also, I could also use a macro that would delete a row if a string of text in the cell of that row contains a certain string of text. It would be good if I could also input a wild card. Example would be that cell A2 contains 03E58302 and I said any cell in column A that contains 03E***** then delete row 2....and so on. If this is hard to do nevermind as my first request is more important. Thanks and I hope to more skilled at this soon.
I could use your help on one additional detail. Some of the part numbers have values greater that Z. As you know I told you we wanted to identify the "largest" letter from A-Z, however in some cases we go to AA-AZ after Z has been used. Can you please help me with the code to where it will determine the "largest" between A and AZ? I know the area of the code that requires modification, but I'm afraid I will mess it up if I try to change it.
Also, I could also use a macro that would delete a row if a string of text in the cell of that row contains a certain string of text. It would be good if I could also input a wild card. Example would be that cell A2 contains 03E58302 and I said any cell in column A that contains 03E***** then delete row 2....and so on. If this is hard to do nevermind as my first request is more important. Thanks and I hope to more skilled at this soon.
RayH
Posts
122
Registration date
Tuesday 31 August 2010
Status
Contributor
Last seen
20 June 2016
26
>
nsettlemire
Posts
4
Registration date
Sunday 1 February 2015
Status
Member
Last seen
29 April 2015
2 Feb 2015 à 22:28
2 Feb 2015 à 22:28
This code should do what you need:
sqlstr = ""
sqlstr = sqlstr + "select distinct left(part,instr(part,'.')-1) as part2, "
sqlstr = sqlstr + " MAX(right(part,len(part)-instr(part,'.')-2)) as maxright"
sqlstr = sqlstr + " FROM [" + ActiveSheet.Name + "$]"
sqlstr = sqlstr + " WHERE part IS not NULL "
sqlstr = sqlstr + " and (ucase(part) like '%[A-Z]'"
sqlstr = sqlstr + " or ucase(part) like '%[A-Z][A-Z]')"
sqlstr = sqlstr + " group by left(part,instr(part,'.')-1)"
I would like to see the full list if that's possible so that all bases and combinations can be allowed for.
You can use www.speedyshare.com to upload the list. If you feel that the data is confidential to be spread around for all to see then please send the link to me as a private message.
How certain are you that there is always '00' following the period?
Are there instances where you could have '01370579.00a' and '01370579.00A' while being very similar are entirely different parts? As right now it assumes a lowercase 'a' is the same as an uppercase 'A'
Regarding your 2nd request, please post it as new question as it is quite different from this one.
sqlstr = ""
sqlstr = sqlstr + "select distinct left(part,instr(part,'.')-1) as part2, "
sqlstr = sqlstr + " MAX(right(part,len(part)-instr(part,'.')-2)) as maxright"
sqlstr = sqlstr + " FROM [" + ActiveSheet.Name + "$]"
sqlstr = sqlstr + " WHERE part IS not NULL "
sqlstr = sqlstr + " and (ucase(part) like '%[A-Z]'"
sqlstr = sqlstr + " or ucase(part) like '%[A-Z][A-Z]')"
sqlstr = sqlstr + " group by left(part,instr(part,'.')-1)"
I would like to see the full list if that's possible so that all bases and combinations can be allowed for.
You can use www.speedyshare.com to upload the list. If you feel that the data is confidential to be spread around for all to see then please send the link to me as a private message.
How certain are you that there is always '00' following the period?
Are there instances where you could have '01370579.00a' and '01370579.00A' while being very similar are entirely different parts? As right now it assumes a lowercase 'a' is the same as an uppercase 'A'
Regarding your 2nd request, please post it as new question as it is quite different from this one.