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
Related
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
The following script selects a range of data on one sheet and transfers the selection to another sheet.
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
If Cells(i, 22) <> "" Then Cells(i, 23).Value = Now
If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
ActiveWorkbook.Save
End If
Next i
I would now like to introduce a script which will replace the row of data on the target sheet if the value in column A already exists, but i'm not sure how to achieve this, any help is much appreciated.
Thank you in advance.
Public Function IsIn(li, Val) As Boolean
IsIn = False
Dim c
For Each c In li
If c = Val Then
IsIn = True
Exit Function
End If
Next c
End Function
dim a: a= range(destWB.sheet(whatever)..range("A1"),destWB.Range("A" & destWB.sheet(whatever).Rows.Count).End(xlUp)).value
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
if isin(a, Cells(i, 1) ) then
do whatever you want
else
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
If Cells(i, 22) <> "" Then Cells(i, 23).Value = Now
If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
ActiveWorkbook.save
End If
End If
Next i
I suggest using a Dictionary-Object which is most likely a Hash-Map. The advantage is that you can use the built in method Dictionary.Exists(Key) to check if the Dictionary already holds the specified value (Key).
Also you should not save the Workbook in every step of the iteration. It would be better (and faster) to only save the workbook after completing the copying of your whole data.
Additionally your If-Tests after copy-paste are not neccessary, because you are already checking for Cells(i,1)<>"" before copying so you don't have to check this again as it does not change.
The following code shows how to get your desired result:
Set dict = CreateObject("Scripting.Dictionary")
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
If dict.Exists(Cells(i,1).Value) Then
'value already exists -> update row number
dict.Item(Cells(i,1).Value)=i
Else
'save value of column A and row number in dictionary
dict.Add Cells(i,1).Value, i
End If
Cells(i, 22).Value = "Yes"
Cells(i, 23).Value = Now
Cells(i, 24).Value = Environ("UserName")
End If
Next i
'finally copy over your data (only unique values)
For Each i In dict.Items
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
Next i
I have wrote the following code to merge cells in excel, the data is around 26000 rows, the code is running on core I7 CPU with 8 GB RAM, the problem that it still working since 4 days, the average rows per day is 3000 row!, any one know how to get the result, because its a report that should be delivered since three days!
Sub MergeCellss()
lastRow = Worksheets("A").Range("A65536").End(xlUp).Row
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
For i = 2 To lastRow
If Cells(i, 2).Value <> Cells(i - 1, 2).Value And Cells(i, 2).Value <> Cells(i + 1, 2).Value Then
intUpper = i
Debug.Print ("<> -1 and <> +1 " & intUpper)
End If
If Cells(i, 2).Value <> Cells(i - 1, 2).Value And Cells(i, 2).Value = Cells(i + 1, 2).Value Then
intUpper = i
Debug.Print ("<> -1 and = +1 " & intUpper & " UPPPER LIMIT")
End If
If Cells(i, 2).Value <> Cells(i + 1, 2).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value Then
Application.DisplayAlerts = False
Debug.Print ("<> +1 and = -1:" & i & "LOWER LIMIT")
DoEvents
For x = 1 To 8
Range(Cells(intUpper, x), Cells(i, x)).Merge
Next x
For j = 18 To 26
Range(Cells(intUpper, j), Cells(i, j)).Merge
Next j
Cells(intUpper, 14).Value = "=sumif(M" & CStr(intUpper) & ":M" & CStr(i) & ","">0"")"
Range(Cells(intUpper, 14), Cells(i, 14)).Merge
Range(Cells(i, 1), Cells(i, 26)).Borders(xlEdgeBottom).LineStyle = xlDouble
End If
If Cells(i, 2).Value <> Cells(i + 1, 2).Value And Cells(i, 2).Value <> Cells(i - 1, 2).Value Then
Debug.Print ("One Cells: " & i)
Range(Cells(i, 1), Cells(i, 26)).Borders(xlEdgeBottom).LineStyle = xlDouble
Cells(intUpper, 14).Value = Cells(intUpper, 13).Value
DoEvents
End If
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
the code above will merge the all cells containing repeated data like User Name, Date of Birth, .... into one cell, and leave the training courses and experiences as it is.
I wonder how can I run this code in less than 1 hour.
Here is some rewrite on your code. The two primary differences are the use of If ... ElseIf ... End If and the grouping of the first and fourth conditional operations (the conditions were the same).
Sub Merge_Cells()
Dim lastRow As Long, rw As Long
Dim intUpper As Long, x As Long
Dim vVALs As Variant
appTGGL bTGGL:=False
Debug.Print Timer
With Worksheets("A")
.Cells(1, 1) = Timer
lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For rw = 2 To lastRow
vVALs = Array(.Cells(rw - 1, 2).Value, .Cells(rw, 2).Value, .Cells(rw + 1, 2).Value)
If vVALs(1) <> vVALs(0) And vVALs(1) <> vVALs(2) Then
'the first and fourth conditions were the same so they are both here
'original first If condition
intUpper = rw
'Debug.Print ("<> -1 and <> +1 " & intUpper)
'original fourth If condition
'Debug.Print ("One Cells: " & rw)
.Range(.Cells(rw, 1), .Cells(rw, 26)).Borders(xlEdgeBottom).LineStyle = xlDouble
.Cells(intUpper, 14).Value = .Cells(intUpper, 13).Value
ElseIf vVALs(1) <> vVALs(0) And vVALs(1) = vVALs(2) Then
intUpper = rw
'Debug.Print ("<> -1 and = +1 " & intUpper & " UPPPER LIMIT")
ElseIf vVALs(1) = vVALs(0) And vVALs(1) <> vVALs(2) Then
'Debug.Print ("<> +1 and = -1:" & rw & "LOWER LIMIT")
For x = 1 To 26
If x < 9 Or x > 17 Then _
.Range(.Cells(intUpper, x), .Cells(rw, x)).Merge
Next x
.Cells(intUpper, 14).Value = "=sumif(M" & CStr(intUpper) & ":M" & CStr(rw) & ","">0"")"
.Range(.Cells(intUpper, 14), .Cells(rw, 14)).Merge
.Cells(rw, 1).Resize(1, 26).Borders(xlEdgeBottom).LineStyle = xlDouble
End If
Next rw
.Cells(1, 2) = Timer
End With
Debug.Print Timer
appTGGL
End Sub
Sub appTGGL(Optional bTGGL As Boolean = True)
Application.Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual)
Application.ScreenUpdating = bTGGL
Application.EnableEvents = bTGGL
Application.DisplayAlerts = bTGGL
End Sub
I've also read the three primary conditional values into a variant array to reduce repeated worksheet value reads.
Thanks for joining me, glad i am here
my problem is Subscript out of range when i am trying to copy and paste the data in individual tabs using with Offset option, i have given my code here
Private Sub CommandButton1_Click()
Call UnprotectSheets
Dim i As Long, a As Long, counter As Long
Dim lastrow As Long, c As Range
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
counter = 0
For i = 2 To Sheets.Count
If Sheets(i).Range("C6") = "" Then
a = 0
Else
a = Sheets(i).Range("C6", Sheets(i).Range("C6").End(xlDown)).Rows.Count
End If
counter = counter + a
Next i
If counter = Sheets("Dispatch Register").Range("C6", Sheets("Dispatch Register").Range("C6").End(xlDown)).Rows.Count Then MsgBox "No new entries!": Exit Sub
With Sheets("Dispatch Register")
lastrow = .Cells(.Rows.Count, 3).End(xlUp).Row
For Each c In Range("F6:F" & lastrow)
c.Offset(, -3).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(1)
c.Offset(, 1).Resize(, 3).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 2)
c.Offset(, 5).Resize(, 4).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 5)
c.Offset(, -4).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 10)
c.Offset(, 10).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 11)
Next c
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Call ProtectSheets
End Sub
when i press the debug button then i go to the below line
c.Offset(, -3).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(1)
kindly suggest me what is the mistake
Thanking you
here is the Final code which is changed but there is one problem that is it's copy only last row,
Private Sub CommandButton1_Click()
Call UnprotectSheets
Dim i As Long, a As Long, counter As Long
Dim lastrow As Long, c As Range
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Call UnprotectSheets
counter = 0
For i = 2 To Sheets.Count
With Sheets(i)
If .Range("C6") = "" Then
a = 0
ElseIf .Range("C7") = "" Then
a = 1
Else
a = .Range("C6", .Range("C6").End(xlDown)).Rows.Count
End If
counter = counter + a
End With
Next i
If counter = Sheets("Dispatch Register").Range("C6", Sheets("Dispatch Register").Range("C6").End(xlDown)).Rows.Count Then MsgBox "No new entries!": Exit Sub
With Sheets("Dispatch Register")
lastrow = .Cells(.Rows.Count, 3).End(xlUp).Row
For Each c In .Range("F" & (counter + 6) & ":F" & lastrow)
If c <> "" Then
If SheetExists(c.Text) Then
c.Offset(, -3).Resize(, 2).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(1)
c.Offset(, 1).Resize(, 3).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 2)
c.Offset(, 5).Resize(, 4).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 5)
c.Offset(, -4).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 10)
c.Offset(, 10).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 11)
Else
Debug.Print "Sheet: '" & c.Text & "' not found"
End If
End If
Next c
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Call ProtectSheets
End Sub
As per your instruction i change the code but i can't understand which to be remove when i run the code then i got the error code application is not defined
here is the latest code
Private Sub CommandButton1_Click()
Call UnprotectSheets
Dim i As Long, a As Long, counter As Long
Dim lastrow As Long, c As Range
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Call UnprotectSheets
counter = 0
For i = 2 To Sheets.Count
With Sheets(i)
If .Range("C6") = "" Then
a = 0
ElseIf .Range("C7") = "" Then
a = 1
Else
a = .Range("C6", .Range("C6").End(xlDown)).Rows.Count
End If
counter = counter + a
End With
Next i
' If counter = Sheets("Dispatch Register").Range("C6", Sheets("Dispatch Register").Range("C6").End(xlDown)).Rows.Count Then MsgBox "No new entries!": Exit Sub
lastCell = Sheets("Dispatch Register").Range("C6").End(xlDown)
counter = Sheets("Dispatch Register").Range("C6", lastCell).Rows.Count
If Count = 0 Then
MsgBox "No new entries!"
Exit Sub
End If
With Sheets("Dispatch Register")
lastrow = .Cells(.Rows.Count, 3).End(xlUp).Row
For Each c In .Range("F" & (counter + 6) & ":F" & lastrow)
If c <> "" Then
If SheetExists(c.Text) Then
c.Offset(, -3).Resize(, 2).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(1)
c.Offset(, 1).Resize(, 3).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 2)
c.Offset(, 5).Resize(, 4).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 5)
c.Offset(, -4).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 10)
c.Offset(, 10).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 11)
Else
Debug.Print "Sheet: '" & c.Text & "' not found"
End If
End If
Next c
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Call ProtectSheets
End Sub
if possible please give me the full code, what is my aim with above code is i have entered data in Dispatch Register with party wise and i have the different tabs as per the parties in the Dispatch Register when i run the code then the data will copy to their individual tabs without duplicate data
if any information you need then please ask me sir
Thanking You
With Regards
I'd add some code to handle possible error conditions and put in some debugging messages to work out what going on (or just examine some more variables in the debugger).
How about the following to get started.
lastrow = .Cells(.Rows.Count, 3).End(xlUp).Row
Debug.Print "lastrow: " & lastrow
For Each c In Range("F6:F" & lastrow)
If SheetExists(c.Text) Then
c.Offset(, -3).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(.Rows.Count, "B").End(xlUp).Offset(1)
c.Offset(, 1).Resize(, 3).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 2)
c.Offset(, 5).Resize(, 4).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 5)
c.Offset(, -4).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 10)
c.Offset(, 10).Resize(, 1).Copy ThisWorkbook.Sheets(c.Text).Cells(Rows.Count, "B").End(xlUp).Offset(0, 11)
Else
Debug.Print "Sheet: '" & c.Text & "' not found"
End If
Next c
Function SheetExists(sheetName As String) As Boolean
SheetExists = False
For Each ws In Worksheets
If sheetName = ws.Name Then
SheetExists = True
Exit Function
End If
Next ws
End Function
If I run this on a blank workbook (with a sheet named "Dispatch Register" I get the following in the "Immediate" debug window
lastrow: 1
Sheet: '' not found
Sheet: '' not found
Sheet: '' not found
Sheet: '' not found
Sheet: '' not found
Sheet: '' not found
In general, if something isn't working, it's best to expand out the code until it's easy to debug. For example,
If counter = Sheets("Dispatch Register").Range("C6", Sheets("Dispatch Register").Range("C6").End(xlDown)).Rows.Count Then MsgBox "No new entries!": Exit Sub
would be easier to read and debug as
lastCell = Sheets("Dispatch Register").Range("C6").End(xlDown)
counter = Sheets("Dispatch Register").Range("C6", lastCell).Rows.Count
If Count = 0 Then
MsgBox "No new entries!"
Exit Sub
End If
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 ;-)