Code for Complex Conditions Needed

Oceangirle

New Member
Joined
Mar 31, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I am creating a VBA code...so far it allows the user to push a button and navigate to the proper folder. It also copies and pastes all of the rows and columns from the original file. However, I need to revise this a bit. I keep trying to create a loop so that only specific records are captured. I need the loop to look at the first two letters and capture everything that begins with ST and TT. However, I am really struggling with the final condition. From those ST and TT selections, I want the code to look between two hyphens, determine if 5 or 8 numbers lie within the two hyphens and if so to copy and paste those rows into the specified sheet. So, I want ST OR TT with 5 or 8 digits between two hyphens to be captured. How can I create the conditions so that the code works? Can someone help me with the code for this? I am thinking of using the Isnumber function variation but I am not certain this will work. Please help!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Sounds like you have the ST or TT part figured out already, so here's a way to find the length of a string between 2 dashes. Just meld this in to what you have.
VBA Code:
Sub findDash()
Dim str As String
Dim int1 As Integer, int2 As Integer, int3 As Integer

str = Range("G14") 'my test cell range. Assign your cell values to str
int1 = InStr(str, "-") + 1
int2 = InStr(int1, str, "-")
int3 = Len(Mid(str, int1, (int2 - int1)))
If int3 = 5 Or int3 = 8 Then
     MsgBox int3
End If

End Sub
 
Upvote 0
I want the code to look between two hyphens, determine if 5 or 8 numbers lie within the two hyphens and if so to copy and paste those rows into the specified sheet. So, I want ST OR TT with 5 or 8 digits between two hyphens to be captured. How can I create the conditions so that the code works? Can someone help me with the code for this? I am thinking of using the Isnumber function variation but I am not certain this will work. Please help!

Slight modification to @Micron's code, to cater for the emphasis on numbers.

VBA Code:
Sub findDash_Micron()
Dim str As String
Dim int1 As Integer, int2 As Integer, int3 As Integer
Dim str3 As String

str = Range("G14") 'my test cell range. Assign your cell values to str
int1 = InStr(str, "-") + 1
int2 = InStr(int1, str, "-")

str3 = Mid(str, int1, (int2 - int1))
int3 = Len(str3)
If (int3 = 5 Or int3 = 8) And IsNumeric(str3) Then
     MsgBox "Length: " & int3 & " Extract: " & str3
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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