Adding text to a data move between different cells - vba

So I currently have this code;
Function ISMERGED(CellAddress As Range) As Boolean
ISMERGED = CellAddress.MergeCells
End Function
Sub Demerge()
Dim CurrentCell As Range
For Each CurrentCell In ActiveSheet.UsedRange
If ISMERGED(CurrentCell) Then CurrentCell.UnMerge
Next
End Sub
Sub Txfer()
Dim x As Long
Dim TestRow As Range
Call Demerge
With Worksheets("Sheet2")
.UsedRange.Delete
.Cells(1, 1).Formula = "Test Name"
.Cells(1, 2).Formula = "Test Description"
.Cells(1, 3).Formula = "Step Name"
.Cells(1, 4).Formula = "Test Step"
.Cells(1, 5).Formula = "Expected Result"
End With
With Worksheets("Sheet2")
For x = 2 To Worksheets("Sheet1").UsedRange.Rows.Count
If Worksheets("Sheet1").Cells(x, 2).Value <> "" Then
.Cells(x, 2).Formula = Worksheets("Sheet1").Cells(x, 4).Value
.Cells(x, 3).Formula = Worksheets("Sheet1").Cells(x, 2).Value
.Cells(x, 4).Formula = Worksheets("Sheet1").Cells(x, 3).Value
.Cells(x, 5).Formula = Worksheets("Sheet1").Cells(x, 5).Value
'add in further columns
Else
.Cells(.UsedRange.Rows.Count, 4).Formula = .Cells(.UsedRange.Rows.Count, 4).Value & Chr(10) & Worksheets("Sheet1").Cells(x, 3).Value
.Cells(.UsedRange.Rows.Count, 5).Formula = .Cells(.UsedRange.Rows.Count, 5).Value & Chr(10) & Worksheets("Sheet1").Cells(x, 5).Value
'concatenate existing content of target cell with data from current source row
End If
Next x
End With
'now to tidy up the blanks...
Worksheets("Sheet2").Activate
Worksheets("Sheet2").Cells(Worksheets("Sheet2").UsedRange.Rows.Count, 2).Activate
Do
If Application.CountA(ActiveCell.EntireRow) = 0 Then ActiveCell.EntireRow.Delete
ActiveCell.Offset(-1, 0).Activate
Loop Until ActiveCell.Row < 2
Range("A1:E1").Font.Bold = True
End Sub
I want to add an "if" statement that if there is anything in the Test Data (This is the data in "Cells(x, 2).Formula = Worksheets("Sheet1").Cells(x, 4).Value") field of the source sheet, I would like to have the test description field contain: “Test Data: “ followed by the text from the source. If the source cell is empty, I would like the Description field to also be empty.
Basically I just want to add the words "Test Data" before the text that is being moved if their is text present in the original cell

Related

UserForm Submit Button to Calculate Dynamic Formula + Insert Row

I use following code to transfer my userform data (from textboxes to respective cell values in row A2) which works fine for me as i want the data to be pushed down when added:
Private Sub CommandButton1_Click()
Dim emptyRow As Long
'Validation
If WorksheetFunction.CountIf(Sheets("RawData").Range("A:A"),
Me.TextBox1.Value) = False Then
MsgBox "Ticket Does Not Exist", vbCritical
End If
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
With ThisWorkbook.Sheets("WOTracker")
.Cells(2, 1).EntireRow.Insert
.Cells(2, 1).Value = TextBox1.Value
.Cells(2, 5).Value = TextBox2.Value
.Cells(2, 2).Value = TextBox3.Value
.Cells(2, 3).Value = TextBox4.Value
.Cells(2, 6).Value = TextBox5.Value
.Cells(2, 7).Value = ComboBox1.Value
.Cells(2, 8).Value = ComboBox2.Value
.Cells(2, 9).Value = TextBox8.Value
.Cells(2, 4).Value = TextBox9.Value
End With
'Formatting
Dim dDate As Date
dDate = DateSerial(Month(Date), Day(Date), Year(Date))
TextBox2.Value = Format(TextBox2.Value, "mm/dd/yy")
dDate = TextBox2.Value
With ThisWorkbook.Sheets("WOTracker")
Sheets("WOTracker").Range("A2:Z2").Font.Bold = False
Sheets("WOTracker").Range("A2:Z2").Font.Underline = xlUnderlineStyleNone
End With
End Sub
In the same sheet, there is column L where i would like to calculate the difference between current date and the date mentioned in column E2 (in # of days) and i would like this to be dynamic so when a new line of data is added to row A2, this formula is applied in L2 while maintaining the formula in the rest of the column L for the data that will be pushed down.
I have tried copy/pastespecial and it doesnt work....
I hope this makes sense.
Would really appreciate assistance!
I would make some organizational changes to your code first
Private Sub CommandButton1_Click()
Dim emptyRow As Long
'Validation
If WorksheetFunction.CountIf(Sheets("RawData").Range("A:A"),
Me.TextBox1.Value) = False Then
MsgBox "Ticket Does Not Exist", vbCritical
End If
'Transfer information
With ThisWorkbook.Sheets("WOTracker")
.Cells(2, 1).EntireRow.Insert
'Determine emptyRow
emptyRow = .Range("L" & Rows.Count).End(xlUp).Row
'changed from WorksheetFunction.CountA(Range("A:A")) + 1 so we can retrieve last
'row in column L
.Cells(2, 1).Value = TextBox1.Value
.Cells(2, 5).Value = TextBox2.Value
.Cells(2, 2).Value = TextBox3.Value
.Cells(2, 3).Value = TextBox4.Value
.Cells(2, 6).Value = TextBox5.Value
.Cells(2, 7).Value = ComboBox1.Value
.Cells(2, 8).Value = ComboBox2.Value
.Cells(2, 9).Value = TextBox8.Value
.Cells(2, 4).Value = TextBox9.Value
.Range("L2:L" & emptyRow) = "=DAYS(TODAY(),$E$2)"
End With
'Formatting <- Might consider deleting the lines containing dDate since it's never
'used
Dim dDate As Date
dDate = DateSerial(Month(Date), Day(Date), Year(Date))
TextBox2.Value = Format(TextBox2.Value, "mm/dd/yy")
dDate = TextBox2.Value
With ThisWorkbook.Sheets("WOTracker")
Sheets("WOTracker").Range("A2:Z2").Font.Bold = False
Sheets("WOTracker").Range("A2:Z2").Font.Underline = xlUnderlineStyleNone
End With
End Sub
The line .Range("L2:L" & emptyRow) = "=DAYS(TODAY(),$E$2)" will calculate the difference in days between the current date and the date in cell E2 for every cell in column L.
If, instead, what you wanted is for each line to subtract from today with respect to it's row number, just change
.Range("L2:L" & emptyRow) = "=DAYS(TODAY(),$E$2)"
To
.Range("L2") = "=DAYS(TODAY(),$E$2)"
And the insert operation will update the formula automatically.

Using rangefind

I have three sheets, sheet S, Sheet P and Sheet Data.
I first copy the column of Sheet S to Sheet Data. Then in column E of sheet Data, I look for the ID. The ID In column E of data sheet, matches with the column A of P sheet, then I copy the corresponding ID.
The problem here is the Sheet data contains 214 rows, while sheet P contains 1110.
while comparing the ID, there are two different ID from row 870 and 871, which are not copied, even though they are same.
Could someone guide what could be the reason ?
Sub lookup()
Dim lLastrow, totalrows As Long
Dim rng As Range
Dim i As Long
'Copy lookup values from S to Data
With Sheets("S")
lLastrow = .Cells(.Rows.count, 1).End(xlUp).Row
.Range("P5:P" & lLastrow).Copy Destination:=Sheets("Data").Range("E5")
.Range("G5:G" & lLastrow).Copy Destination:=Sheets("Data").Range("H5")
End With
totalrows = Sheets("P").Cells(Sheets("P").Rows.count, "A").End(xlUp).Row
For i = 5 To lLastrow
'Search for the value on P_APQP
With Sheets("P")
Set rng = .Columns(1).Find(Sheets("Data").Cells(i, 5).Value & "*", lookat:=xlWhole)
End With
'If it is found put its value on the destination sheet
If Not rng Is Nothing Then
With Sheets("Data")
.Cells(i, 6).Value = rng.Value
.Cells(i, 1).Value = rng.Offset(0, 1).Value
.Cells(i, 2).Value = rng.Offset(0, 2).Value
.Cells(i, 3).Value = rng.Offset(0, 3).Value
.Cells(i, 4).Value = rng.Offset(0, 9).Value
.Cells(i, 9).Value = rng.Offset(0, 10).Value
.Cells(i, 13).Value = rng.Offset(0, 6).Value
.Cells(i, 14).Value = rng.Offset(0, 5).Value
.Cells(i, 15).Value = rng.Offset(0, 4).Value
.Cells(i, 16).Value = rng.Offset(0, 8).Value
End With
End If
Next i
End Sub
I'll post the whole code. I also made an adjustment to your first line of declarations - as you had it, only totalrows was being declared as Long. You have to spell each one out I'm afraid.
Sub lookup()
Dim lLastrow As Long, totalrows As Long
Dim rng As Range
Dim i As Long
With Sheets("S")
lLastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("P5:P" & lLastrow).Copy Destination:=Sheets("Data").Range("E5")
.Range("G5:G" & lLastrow).Copy Destination:=Sheets("Data").Range("H5")
End With
totalrows = Sheets("P").Cells(Sheets("P").Rows.Count, "A").End(xlUp).Row
For i = 5 To lLastrow
'Search for the value on P_APQP
With Sheets("P")
'amended below
Set rng = .Columns(1).Find(Trim(Sheets("Data").Cells(i, 5).Value) & "*", lookat:=xlWhole)
End With
'If it is found put its value on the destination sheet
If Not rng Is Nothing Then
With Sheets("Data")
.Cells(i, 6).Value = rng.Value
.Cells(i, 1).Resize(, 3).Value = rng.Offset(0, 1).Value
.Cells(i, 2).Value = rng.Offset(0, 2).Value
.Cells(i, 3).Value = rng.Offset(0, 3).Value
.Cells(i, 4).Value = rng.Offset(0, 9).Value
.Cells(i, 9).Value = rng.Offset(0, 10).Value
.Cells(i, 13).Value = rng.Offset(0, 6).Value
.Cells(i, 14).Value = rng.Offset(0, 5).Value
.Cells(i, 15).Value = rng.Offset(0, 4).Value
.Cells(i, 16).Value = rng.Offset(0, 8).Value
End With
End If
Next i
End Sub

Looping function optimisation - currently running slow

I was wondering if anyone can offer advice on the following looping code I have compiled. When I run this from my laptop it processes really quickly within 2-3 seconds however I run this on a works computer and it runs really slowly and takes 10+ minutes to do the 3000-4000 rows.
Dim LastRow As Long
Dim Cell, Rng, Table As Range
'Turn off Screen updating - Speed process - Turn back on prior to Exit Sub
Application.ScreenUpdating = False
Sheets("Del Data").Select
'Validate Data Exists in Range
If Range("B3").Value = "" Then
MsgBox "No Data Available to calculate." & vbNewLine & _
"Please ensure first consigment number is pasted in cell B3." & vbNewLine & vbNewLine & _
"For assistance please refer to user manual supplied with file.", _
vbCritical, "Error Compiling Stop Calculator"
Application.ScreenUpdating = True
Exit Sub
End If
'Identify Last possible row and set Range
With ActiveSheet
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
Set Rng = Range("B3:B" & LastRow)
'**********************************
' Calculate unique values
'**********************************
For Each Cell In Rng
Cell.Offset(0, 33).Value = Trim(Left(Cell.Offset(0, 5).Value, 3))
Cell.Offset(0, 34).Value = Trim(Left(Cell.Offset(0, 7).Value, 3))
Cell.Offset(0, 35).Value = Trim(Left(Cell.Offset(0, 17).Value, 3))
Cell.Offset(0, 38).Value = Trim(Cell.Offset(0, 21).Value)
Cell.Offset(0, 36).Value = Cell.Offset(0, 34).Value & Cell.Offset(0, 33).Value & Cell.Offset(0, 38).Value
Cell.Offset(0, 37).Value = Cell.Offset(0, 34).Value & Cell.Offset(0, 34).Value & Cell.Offset(0, 38).Value
Next
Application.ScreenUpdating = True
Exit Sub
Is there any possible way to improve the code above to allow this to speed up? I understand as this is running fine on my laptop it might just be a computer issue with my works PC although specs of the PC is really good and definitely up to the task.
?
Dim i As Long
'**********************************
' Calculate unique values
'**********************************
With ActiveSheet
For i = 3 To LastRow
.Cells(i, 35).Value = Trim$(Left$(.Cells(i, 7).Value, 3))
.Cells(i, 36).Value = Trim$(Left$(.Cells(i, 9).Value, 3))
.Cells(i, 37).Value = Trim$(Left$(.Cells(i, 19).Value, 3))
.Cells(i, 40).Value = Trim$(Left$(.Cells(i, 23).Value, 3))
.Cells(i, 38).Value = .Cells(i, 36).Value & .Cells(i, 35).Value & .Cells(i, 40).Value
.Cells(i, 39).Value = .Cells(i, 36).Value & .Cells(i, 36).Value & .Cells(i, 40).Value
Next i
End With
i used a VBA array to make things faster, and a bit to tweaking here and there.
I didn't test the code, so make a copy of your data first.
Option Explicit
Sub Test()
'Sheets("Del Data").Select
With thisworkbook.Sheets("Del Data")
'Validate Data Exists in Range
If .Range("B3").Value = vbNullString Then
MsgBox "No Data Available to calculate." & vbNewLine & _
"Please ensure first consigment number is pasted in cell B3." & vbNewLine & vbNewLine & _
"For assistance please refer to user manual supplied with file.", _
vbCritical, "Error Compiling Stop Calculator."
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
'i declared the variables after the condition to exit sub
Dim LastRow As Long, i&
Dim Rng As Range
Dim RngArray() 'is a variant type array, used to fast up the process
'Identify Last possible row and set Range
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set Rng = .Range(.Cells(3, 2), .Cells(LastRow, 2)) '"B3:B" & LastRow)
RngArray = .Range(.Cells(1, 2), .Cells(LastRow, 2 + 39)).Value2
'**********************************
' Calculate unique values
'beware: Cell.Offset(0, 0) is converted in my coding to RngArray(i, 1)
'**********************************
For i = 3 To LastRow
'Cell.Offset(0, 33).Value = Trim(Left(Cell.Offset(0, 5).Value, 3))
RngArray(i, 34) = Left(Trim(RngArray(i, 6)), 3)
'Cell.Offset(0, 34).Value = Trim(Left(Cell.Offset(0, 7).Value, 3))
RngArray(i, 35) = Left(Trim(RngArray(i, 8)), 3)
'Cell.Offset(0, 35).Value = Trim(Left(Cell.Offset(0, 17).Value, 3))
RngArray(i, 36) = Left(Trim(RngArray(i, 18)), 3)
'Cell.Offset(0, 38).Value = Trim(Cell.Offset(0, 21).Value)
RngArray(i, 39) = Left(Trim(RngArray(i, 22)))
'Cell.Offset(0, 36).Value = Cell.Offset(0, 34).Value & Cell.Offset(0, 33).Value & Cell.Offset(0, 38).Value
RngArray(i, 37) = RngArray(i, 35) + RngArray(i, 34) + RngArray(i, 39)
'Cell.Offset(0, 37).Value = Cell.Offset(0, 34).Value & Cell.Offset(0, 34).Value & Cell.Offset(0, 38).Value
RngArray(i, 38) = 2 * RngArray(i, 35) + RngArray(i, 39) 'OP readed twice same Cell , i used *2, might be OP miss
Next i
'write back values to sheet
.Range(.Cells(1, 2), .Cells(LastRow, 2 + 39)).Value2 = RngArray
End With
Set Rng = Nothing
Erase RngArray
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlAutomatic
End With
Exit Sub

vba For each loop writing to the same row

I wanted to reach out on a concern i am getting with with my loop. i need to copy all trainee data in the class list sheet from rows 13 on and put it into a roster registry sheet same workbook. however the code i wrote initially is giving me and error when i use .Range(i, 1) so i changed it to .Cells. Main concern is that the code writes the data into the roster registry sheet but only the last trainee data.
Option Explicit
Sub ExportcReg()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wbk1 As Workbook
Dim s1, cReg As Worksheet
Dim x, i, FinalRow As Long
Dim thisvalue As String
Set wbk1 = Workbooks.Open(ThisWorkbook.Worksheets("Info").Range("A1").Value)
Set s1 = wbk1.Sheets("ClassRegistry")
Set cReg = ThisWorkbook.Worksheets("Class Registry")
With cReg
' Find the last row of data in Column "A"
FinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' Loop through each row
For x = 2 To FinalRow
' Decide if to copy based on column A
thisvalue = .Cells(x, 1).Value
If thisvalue <> "" Then
i = s1.Cells(s1.Rows.Count, 1).End(xlUp).Row + 1
s1.Cells(i, 1).Value = thisvalue
s1.Cells(i, 2).Value = .Cells(x, 2).Value
s1.Cells(i, 3).Value = .Cells(x, 3).Value
s1.Cells(i, 4).Value = .Cells(x, 4).Value
s1.Cells(i, 5).Value = .Cells(x, 5).Value
s1.Cells(i, 6).Value = .Cells(x, 5).Value
s1.Cells(i, 7).Value = .Cells(x, 7).Value
s1.Cells(i, 8).Value = .Cells(x, 8).Value
s1.Cells(i, 9).Value = .Cells(x, 9).Value
s1.Cells(i, 10).Value = .Cells(x, 10).Value
s1.Cells(i, 11).Value = .Cells(x, 11).Value
s1.Cells(i, 12).Value = .Cells(x, 12).Value
s1.Cells(i, 13).Value = .Cells(x, 13).Value
s1.Cells(i, 14).Value = .Cells(x, 14).Value
s1.Cells(i, 15).Value = .Cells(x, 15).Value
s1.Cells(i, 16).Value = .Cells(x, 16).Value
s1.Cells(i, 17).Value = .Cells(x, 17).Value
s1.Cells(i, 18).Value = .Cells(x, 18).Value
End If
Next x
End With
wbk1.Close savechanges:=True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
You have too much logical and technical errors in your code which I cannot explain line by line. Besides you can achieve your goal with less coding. Maybe what you need is a working code to understand how things work. Take a look at this:
Sub CListtoRReg()
'From Class List to Roster Registry
Dim ws1 As Worksheet: Set ws1 = Worksheets("Class List")
Dim ws2 As Worksheet: Set ws2 = Worksheets("Roster Registry")
Dim i, j As Long
For i = 13 To ws1.Cells(1, 1).End(xlDown).Row
ws2.Cells(1, 1).End(xlDown).Offset(1) = ws1.Range("C2").Value
For j = 2 To 11
ws2.Cells(1, 1).End(xlDown).Offset(0, j - 1) = ws1.Cells(i, j - 1).Value
Next j
Next i
End Sub

How do you speed up VBA code with a named range?

I have written a program that analyzes a worksheet (with 8000 rows and 40 columns) and returns all of the relevant product ID's but my program is unbearably slow, it takes about 5 minutes to run, so In looking for a way to speed it up I came across some code to disable screenupdating, display status bar, calculation, and events. which doubled the programs run time (from 5 to 10 minutes) But i need the program to be able to run faster still. I kept searching and came across This This seems like it's exactly what i need but i don't exactly understand how to implement it.
Let me explain what my code needs to do and maybe you can help me find a better way. It might be helpful to tell you what the information is about. I work for a company that sells holsters, and we are trying to find a way to gather all of the product ID's for different types of holsters for 1 gun together. So in the first column we have the Gun names, in the 4th column we have the Holster Type and in the 12th column we have the Product ID #.
What I'm trying to do is to for any given line, make the program look throught the rest of the file and return the product ID's for the matching products (products with the exact same name) in lines 33-39 i.e column 33 will have the related concealment holster, 34 will have the related ankle holster etc.
I have already written a code to do this but how can i do it with this named DataRange Method?
Do
ActiveCell.Offset(1, 0).Activate
Location = ActiveCell.Address
GunName = ActiveCell.Value
X = 0
Range("A1").Activate
Do
If ActiveCell.Offset(X, 0).Value = GunName Then
PlaceHolder = ActiveCell.Address
If ActiveCell.Offset(X, 3).Value = "CA" Then
Range(Location).Offset(0, 34).Value = ActiveCell.Offset(X, 12).Value
ElseIf ActiveCell.Offset(X, 3).Value = "AA" Or ActiveCell.Offset(X, 3).Value = "AR" Then
If ActiveCell.Offset(X, 4).Value = "NA-LH" Or ActiveCell.Offset(X, 4).Value = "NA" Or ActiveCell.Offset(X, 4).Value = "11-LH" Or ActiveCell.Offset(X, 4).Value = "13-LH" Or ActiveCell.Offset(X, 4).Value = "12-A-LH" Or ActiveCell.Offset(X, 4).Value = "12-B-LH" Or ActiveCell.Offset(X, 4).Value = "12-C-LH" Or ActiveCell.Offset(X, 4).Value = "12-JB-LH" Or ActiveCell.Offset(X, 4).Value = "12-LS-LH" Or ActiveCell.Offset(X, 4).Value = "12-LS-b-LH" Or ActiveCell.Offset(X, 4).Value = "11-LS-LH" Or ActiveCell.Offset(X, 4).Value = "21L" Then
Else
Range(Location).Offset(0, 35).Value = ActiveCell.Offset(X, 12)
End If
ElseIf ActiveCell.Offset(X, 3).Value = "BA" Or ActiveCell.Offset(X, 3).Value = "BR" Then
Range(Location).Offset(0, 36).Value = ActiveCell.Offset(X, 12)
ElseIf ActiveCell.Offset(X, 3).Value = "HA" Or ActiveCell.Offset(X, 3).Value = "HR" Then
Range(Location).Offset(0, 37).Value = ActiveCell.Offset(X, 12)
ElseIf ActiveCell.Offset(X, 3).Value = "VA" Or ActiveCell.Offset(X, 3).Value = "VR" Then
Range(Location).Offset(0, 38).Value = ActiveCell.Offset(X, 12)
ElseIf ActiveCell.Offset(X, 3).Value = "TA" Or ActiveCell.Offset(X, 3).Value = "TR" Then
Range(Location).Offset(0, 39).Value = ActiveCell.Offset(X, 12)
End If
End If
X = X + 1
Loop Until IsEmpty(ActiveCell.Offset(X, 0).Value)
ActiveCell.Range(Location).Activate
Loop Until IsEmpty(ActiveCell.Value)
AA, BA CA etc are the holster types.
EDIT
After viewing the sample file and clarifying through the below comments, here is the updated code. I believe this should work for you:
Sub tgr()
Dim rngData As Range
Dim GunCell As Range
Dim rngFound As Range
Dim arrResults() As Variant
Dim ResultIndex As Long
Dim cIndex As Long
Dim strFirst As String
Dim strTemp As String
On Error Resume Next
With Range("DataRange")
.Sort .Resize(, 1), xlAscending, Header:=xlYes
Set rngData = .Resize(, 1)
End With
On Error GoTo 0
If rngData Is Nothing Then Exit Sub 'No data or no named range "DataRange"
With rngData
ReDim arrResults(1 To .Rows.Count, 1 To 6)
For Each GunCell In .Cells
If GunCell.Row > 1 Then
ResultIndex = ResultIndex + 1
If LCase(GunCell.Text) <> strTemp Then
strTemp = LCase(GunCell.Text)
Set rngFound = .Find(strTemp, .Cells(.Cells.Count), xlValues, xlWhole)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Do
If InStr(1, " CA BA HA VA TA ", " " & .Parent.Cells(rngFound.Row, "D").Text & " ", vbTextCompare) > 0 Then
Select Case UCase(.Parent.Cells(rngFound.Row, "D").Text)
Case "CA": cIndex = 1
Case "BA": cIndex = 3
Case "HA": cIndex = 4
Case "VA": cIndex = 5
Case "TA": cIndex = 6
End Select
arrResults(ResultIndex, cIndex) = .Parent.Cells(rngFound.Row, "M").Text
ElseIf InStr(1, " AA AR ", " " & .Parent.Cells(rngFound.Row, "D").Text & " ", vbTextCompare) > 0 _
And InStr(1, " NA-LH NA 11-LH 13-LH 12-A-LH 12-B-LH 12-C-LH 12-JB-LH 12-LS-LH 12-LS-b-LH 11-LS-LH 21L ", " " & .Parent.Cells(rngFound.Row, "E").Text & " ", vbTextCompare) = 0 Then
cIndex = 2
arrResults(ResultIndex, cIndex) = .Parent.Cells(rngFound.Row, "M").Text
End If
Set rngFound = .Find(strTemp, rngFound, xlValues, xlWhole)
Loop While rngFound.Address <> strFirst
End If
Else
For cIndex = 1 To UBound(arrResults, 2)
arrResults(ResultIndex, cIndex) = arrResults(ResultIndex - 1, cIndex)
Next cIndex
End If
End If
Next GunCell
End With
Range("AI2:AI" & Rows.Count).Resize(, UBound(arrResults, 2)).ClearContents
If ResultIndex > 0 Then Range("AI2").Resize(ResultIndex, UBound(arrResults, 2)).Value = arrResults
End Sub
Avoid .Activate, which is VERY slow and generally useless. Instead try something in this style:
Option Explicit
Sub sample()
Dim c As Range
For Each c In Range("a:a").SpecialCells(xlCellTypeConstants)
If c.Offset(x, 0).Value = GunName Then
'etc etc
End If
Next c
End Sub
Oh ! and make sure you use Option Explicit and you Dim your variables. It's not for speed, it is to avoid errors. And use comments ;-)