Looking for help with a macro/vba for find/copy/paste
Closed
nsettlemire
Posts
4
Registration date
Sunday February 1, 2015
Status
Member
Last seen
April 29, 2015
-
Feb 1, 2015 at 02:36 PM
RayH Posts 122 Registration date Tuesday August 31, 2010 Status Contributor Last seen June 20, 2016 - Feb 2, 2015 at 10:28 PM
RayH Posts 122 Registration date Tuesday August 31, 2010 Status Contributor Last seen June 20, 2016 - Feb 2, 2015 at 10:28 PM
Related:
- Looking for help with a macro/vba for find/copy/paste
- Vba case like - Guide
- Number to words in excel formula without vba - Guide
- Vba check if value is in array - Guide
- Vba color index - Guide
- Copy and paste fonts - Guide
3 responses
RayH
Posts
122
Registration date
Tuesday August 31, 2010
Status
Contributor
Last seen
June 20, 2016
26
Feb 1, 2015 at 06:11 PM
Feb 1, 2015 at 06:11 PM
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 August 31, 2010
Status
Contributor
Last seen
June 20, 2016
26
Feb 1, 2015 at 06:16 PM
Feb 1, 2015 at 06:16 PM
nsettlemire
Posts
4
Registration date
Sunday February 1, 2015
Status
Member
Last seen
April 29, 2015
Feb 1, 2015 at 08:36 PM
Feb 1, 2015 at 08:36 PM
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 August 31, 2010
Status
Contributor
Last seen
June 20, 2016
26
Feb 1, 2015 at 09:32 PM
Feb 1, 2015 at 09:32 PM
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 February 1, 2015
Status
Member
Last seen
April 29, 2015
Feb 2, 2015 at 12:49 PM
Feb 2, 2015 at 12:49 PM
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 February 1, 2015
Status
Member
Last seen
April 29, 2015
Feb 2, 2015 at 08:41 PM
Feb 2, 2015 at 08:41 PM
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 August 31, 2010
Status
Contributor
Last seen
June 20, 2016
26
>
nsettlemire
Posts
4
Registration date
Sunday February 1, 2015
Status
Member
Last seen
April 29, 2015
Feb 2, 2015 at 10:28 PM
Feb 2, 2015 at 10:28 PM
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.