Lag in vba vlookup - vba

I'm running VBA code using vlookup, however, it take a few seconds to complete, despite the sheet that has the rows only has less than 150 rows.
The lag mainly appears during the generation of col 23.
The main sheet that contains this code has about 2300 rows.
Is the lag normal or is my coding inefficiency getting the best of me?
Private Sub Worksheet_Change(ByVal Target As Range)
thisrow = Target.Row
If Target.Column = 21 Then
' Generate the problem comments
' Declare some variables
Dim CodeString As String
Dim codeArr() As String
Dim isPI As Boolean
isPI = False
' Reset the impact, comment and origin cells
Cells(thisrow, 22).Value = ""
Cells(thisrow, 23).Value = ""
Cells(thisrow, 25).Value = ""
' For esthetics, remove spaces in the cell
Application.EnableEvents = False
Cells(thisrow, 21).Value = Replace(Cells(thisrow, 21).Value, " ", "")
Application.EnableEvents = True
' Get the code(s)
CodeString = Cells(thisrow, 21).Value
codeArr = Split(CodeString, Chr(59))
' Error code rows
ErrLastRow = Sheets("lookup error codes").Cells(Sheets("lookup error codes").Rows.Count, 1).End(xlUp).Row
' There's more than one code
If UBound(codeArr) > 0 Then
For i = 0 To UBound(codeArr)
If i < UBound(codeArr) Then
Cells(thisrow, 23).Value = Cells(thisrow, 23).Value & Application.WorksheetFunction.VLookup(CInt(codeArr(i)), Sheets("lookup error codes").Range("$A$2:$C$" & ErrLastRow).Value, 2, False) & "; "
Else
Cells(thisrow, 23).Value = Cells(thisrow, 23).Value & Application.WorksheetFunction.VLookup(CInt(codeArr(i)), Sheets("lookup error codes").Range("$A$2:$C$" & ErrLastRow).Value, 2, False)
End If
Next i
' Check to see if anything is pay impacting
For Each code In codeArr
If Application.WorksheetFunction.VLookup(CInt(code), Sheets("lookup error codes").Range("$A$2:$C$" & ErrLastRow).Value, 3, False) <> "" Then
isPI = True
' We only needed one
Exit For
End If
Next code
Else
' There's only one code
Cells(thisrow, 23).Value = Application.WorksheetFunction.VLookup(Cells(thisrow, 21).Value, Sheets("lookup error codes").Range("$A$2:$C$" & ErrLastRow).Value, 2, False)
If Application.WorksheetFunction.VLookup(Cells(thisrow, 21).Value, Sheets("lookup error codes").Range("$A$2:$C$" & ErrLastRow).Value, 3, False) <> "" Then
isPI = True
End If
End If
' There is a code that is pay impacting
If isPI = True Then
Cells(thisrow, 22).Value = "X"
End If
' Modify the origin of error with common origins
Dim Comment As Range, OrigErr As Range
Set Comment = Range(Cells(thisrow, 23).Address)
Set OrigErr = Range(Cells(thisrow, 25).Address)
OrigErr.Value = ""
If InStr(1, Comment.Value, "aaa", vbBinaryCompare) Or _
InStr(1, Comment.Value, "bbb", vbBinaryCompare) Or _
InStr(1, Comment.Value, "ccc", vbBinaryCompare) Then
OrigErr.Value = "ddd"
ElseIf InStr(1, Comment.Value, "eee", vbBinaryCompare) Then
OrigErr.Value = "fff"
End If
End If
End Sub

Changing a cell's value to "" is triggering a change event. Disable events before changing anything on the worksheet and disable calculation if the changed cells affect other formulas.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 21 Then
' Generate the problem comments
' Declare some variables
Dim CodeString As String, codeArr As Variant
Dim isPI As Boolean, thisRow As Long
On Error GoTo safe_exit
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
thisRow = Target.Row
isPI = False
' Reset the impact, comment and origin cells
Cells(thisRow, 22) = vbNullString
Cells(thisRow, 23).Value = vbNullString
Cells(thisRow, 25).Value = vbNullString
' For esthetics, remove spaces in the cell
Cells(thisRow, 21) = Replace(Cells(thisRow, 21).Value, " ", vbNullString)
' Get the code(s)
CodeString = Cells(thisRow, 21).Value
codeArr = Split(CodeString, Chr(59))
' Error code rows
ErrLastRow = Sheets("lookup error codes").Cells(Sheets("lookup error codes").Rows.Count, 1).End(xlUp).Row
' Doesn't matter if there is one code or many
For i = LBound(codeArr) To UBound(codeArr)
If i < UBound(codeArr) Then
Cells(thisRow, 23).Value = Cells(thisRow, 23).Value & Application.VLookup(CLng(codeArr(i)), Sheets("lookup error codes").Range("A:C"), 2, False) & "; "
Else
Cells(thisRow, 23).Value = Cells(thisRow, 23).Value & Application.VLookup(CLng(codeArr(i)), Sheets("lookup error codes").Range("A:C"), 2, False)
End If
Next i
' Check to see if anything is pay impacting
For Each code In codeArr
If Application.VLookup(CLng(code), Sheets("lookup error codes").Range("A:C"), 3, False) <> "" Then
' There is a code that is pay impacting
Cells(thisRow, 22).Value = "X"
' We only needed one
Exit For
End If
Next code
If isPI Then
End If
' Modify the origin of error with common origins
Dim Comment As Range, OrigErr As Range
Set Comment = Cells(thisRow, 23)
Set OrigErr = Cells(thisRow, 25)
OrigErr.Value = vbNullString
If InStr(1, Comment.Value, "aaa", vbBinaryCompare) Or _
InStr(1, Comment.Value, "bbb", vbBinaryCompare) Or _
InStr(1, Comment.Value, "ccc", vbBinaryCompare) Then
OrigErr.Value = "ddd"
ElseIf InStr(1, Comment.Value, "eee", vbBinaryCompare) Then
OrigErr.Value = "fff"
End If
End If
safe_exit:
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub

Add
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
To the start of your code and
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
to the end.

Related

Keep Receiving a 424 Debug Error w/ File Name

Please see the bold code below. The debugger says that it is what's causing the 424 "Object Required" error. Any insight/help on this would be greatly appreciated.
Sub GraywolfPlanful()
Dim sForecastFile, sUploadFile, sTab As String
Dim i As Integer
Application.ScreenUpdating = False
sForecastFile = GraywolfWorkbook.Name
Workbooks.Add
sUploadFile = GraywolfWorkbook.Name
Range("A1") = "Company"
Range("B1") = "Department"
Range("C1") = "Location"
Range("D1") = "Segment"
Range("E1") = "Account"
Range("F1") = "Year"
Range("G1") = "Month"
Range("H1") = "Amount"
Range("A2").Select
Windows(sForecastFile).Activate
sTab = Format(Sheets("00_No Department").Range("C1"), "yyyy-mm mmm") & "Project Forecast"
For i = 4 To Sheets.Count
Sheets(i).Select
Range("F6").Select
Do Until (Range("C") = "End" And Range("D") = "Subtotal Other G&A")
If Range("Z" & ActiveCell.Row) <> "" Then
Range("Z" & ActiveCell.Row, "AD" & ActiveCell.Row).Copy
Windows(sUploadFile).Activate
Range(ActiveCell, ActiveCell.Offset(12, 0)).PasteSpecial xlPasteValues
Range("B" & ActiveCell.Row, "B" & ActiveCell.Row + 12) = "Test"
Windows(sCurrentFile).Activate
End If
Loop
Next i
Windows(sUploadFile).Activate
GraywolfWorkbook.SaveAs CurDir & "\Test File Graywolf"
Application.ScreenUpdating = True
End Sub

Worksheet Codename Errors

When using the code below to reference a worksheet by codename, I get file explorer opening up for each instance of the "Sheet34". How can I prevent this?
Private Sub CheckBox4_Click()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim sheetName As String
If CheckBox4.Value = True Then
ActiveWorkbook.Unprotect
Worksheets("SUMMARY").Unprotect
sheetName = Sheets("Control").Cells(16, "I")
If sheetName = "" Then
MsgBox "You must enter a valid Allowance descriptor. No entry was detected."
CheckBox4.Value = False
Exit Sub
End If
If Len(sheetName) > 31 Then
MsgBox "Worksheet tab names cannot be greater than 31 characters in length."
Application.EnableEvents = False
Sheets("Control").Cells(16, "I").ClearContents
Application.EnableEvents = True
CheckBox4.Value = False
Exit Sub
End If
Dim IllegalCharacter(1 To 7) As String, i As Integer
IllegalCharacter(1) = "/"
IllegalCharacter(2) = "\"
IllegalCharacter(3) = "["
IllegalCharacter(4) = "]"
IllegalCharacter(5) = "*"
IllegalCharacter(6) = "?"
IllegalCharacter(7) = ":"
For i = 1 To 7
If InStr(sheetName, (IllegalCharacter(i))) > 0 Then
MsgBox "You used a character that violates sheet naming rules. Please refrain from the following characters: / \ [ ] * ? : "
Application.EnableEvents = False
Sheets("Control").Cells(16, "I").ClearContents
Application.EnableEvents = True
CheckBox4.Value = False
Exit Sub
End If
Next i
If Sheets("Control").Range("I16") = Sheets("Control").Range("I17") Then
MsgBox "There is already an Allowance with this name. Please choose a different name."
Application.EnableEvents = False
Sheets("Control").Cells(16, "I").ClearContents
Application.EnableEvents = True
CheckBox4.Value = False
Exit Sub
End If
If Sheets("Control").Range("I16") = Sheets("Control").Range("I18") Then
MsgBox "There is already an Allowance with this name. Please choose a different name."
Application.EnableEvents = False
Sheets("Control").Cells(16, "I").ClearContents
Application.EnableEvents = True
CheckBox4.Value = False
Exit Sub
End If
If Sheets("Control").Range("I16") = Sheets("Control").Range("I21") Then
MsgBox "There is already an Other Item with this name. Please choose a different name."
Application.EnableEvents = False
Sheets("Control").Cells(16, "I").ClearContents
Application.EnableEvents = True
CheckBox4.Value = False
Exit Sub
End If
If Sheets("Control").Range("I16") = Sheets("Control").Range("I22") Then
MsgBox "There is already an Other Item with this name. Please choose a different name."
Application.EnableEvents = False
Sheets("Control").Cells(16, "I").ClearContents
Application.EnableEvents = True
CheckBox4.Value = False
Exit Sub
End If
If Sheets("Control").Range("I16") = Sheets("Control").Range("I23") Then
MsgBox "There is already an Other Item with this name. Please choose a different name."
Application.EnableEvents = False
Sheets("Control").Cells(16, "I").ClearContents
Application.EnableEvents = True
CheckBox4.Value = False
Exit Sub
End If
If WorksheetExists(sheetName) Then
Worksheets(sheetName).Visible = -1
Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = False
Worksheets("SUMMARY").Cells(47, 2).Value = "ALL 1:"
Worksheets("SUMMARY").Cells(47, 3).Value = "='Control'!I16"
Worksheets("SUMMARY").Cells(47, 3).NumberFormat = "General"
Worksheets("SUMMARY").Cells(47, 4).Value = "='Control'!K16"
Worksheets("SUMMARY").Cells(47, 5).Value = "='Control'!L16"
Worksheets("SUMMARY").Cells(47, 6).Value = "=" & sheetName & "!$H$69"
Worksheets("SUMMARY").Cells(47, 7).Value = "=" & sheetName & "!$J$69"
Worksheets("SUMMARY").Cells(47, 8).Value = "=" & sheetName & "!$N$69"
Worksheets("SUMMARY").Cells(47, 9).Value = "=" & sheetName & "!$P$69"
Worksheets("SUMMARY").Cells(47, 10).Value = "=SUM(F47:I47)/D47"
Worksheets("SUMMARY").Cells(47, 11).Value = "=L47/F3"
Worksheets("SUMMARY").Cells(47, 12).Value = "=" & sheetName & "!$U$69"
Worksheets("SUMMARY").Cells(47, 13).Value = "=L47/$K$57"
ActiveWorkbook.Protect
Sheets(sheetName).Protect
Sheets("SUMMARY").Protect
Exit Sub
Else
Set ws = ActiveWorkbook.Sheet34
ws.Name = sheetName
ws.Protect
ws.EnableSelection = xlUnlockedCells
Application.CutCopyMode = False
Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = False
Worksheets("SUMMARY").Cells(47, 2).Value = "ALL 1:"
Worksheets("SUMMARY").Cells(47, 3).Value = "='Control'!I16"
Worksheets("SUMMARY").Cells(47, 3).NumberFormat = "General"
Worksheets("SUMMARY").Cells(47, 4).Value = "='Control'!K16"
Worksheets("SUMMARY").Cells(47, 5).Value = "='Control'!L16"
Worksheets("SUMMARY").Cells(47, 6).Value = "=" & ws.Name & "!$H$69"
Worksheets("SUMMARY").Cells(47, 7).Value = "=" & ws.Name & "!$J$69"
Worksheets("SUMMARY").Cells(47, 8).Value = "=" & ws.Name & "!$N$69"
Worksheets("SUMMARY").Cells(47, 9).Value = "=" & ws.Name & "!$P$69"
Worksheets("SUMMARY").Cells(47, 10).Value = "=SUM(F47:I47)/D47"
Worksheets("SUMMARY").Cells(47, 11).Value = "=L47/F3"
Worksheets("SUMMARY").Cells(47, 12).Value = "=" & ws.Name & "!$U$69"
Worksheets("SUMMARY").Cells(47, 13).Value = "=L47/$K$57"
ActiveWorkbook.Protect
Sheets(sheetName).Protect
Sheets("SUMMARY").Protect
Worksheets("Control").Activate
End If
End If
If CheckBox4.Value = False Then
ActiveWorkbook.Unprotect
Worksheets("SUMMARY").Unprotect
sheetName = Sheets("Control").Cells(16, "I")
If WorksheetExists(sheetName) Then
Worksheets(sheetName).Visible = 2
Worksheets("SUMMARY").Rows("47").EntireRow.ClearContents
Worksheets("SUMMARY").Rows("47").EntireRow.Hidden = True
ActiveWorkbook.Protect
Sheets(sheetName).Protect
Sheets("SUMMARY").Protect
End If
End If
Application.ScreenUpdating = True
End Sub
Follow up question: This code is taking an existing sheet and renaming it. The concern I have is if the user names the sheet in Cell I16, then renames it, and unchecks or checks the box, I get a row on the Summary Worksheet with poor references. Also, the code will not allow me to enter the code below
Worksheets("SUMMARY").Cells(47, 6).Value = "=" & sheetName & "!$H$69"
as
Worksheets("SUMMARY").Cells(47, 6).Value = "="' & sheetName & '"!$H$69"
as it turns the & sheetName & into a comment. How can I fix this so that the worksheet is properly referenced in the code? Do I need to keep with the codename worksheet?
My thoughts on how to stop the renaming issue was to protect the named cell so long as the checkbox value is positive, and allow the user to edit the cell if the checkbox value is false; maybe adding a line of code that checks if the cell is being changed and asking the user if they are sure this is the cell they want to modify, which would clear the data, but not the formatting, of the existing spreadsheet.
You are trying to set the value of a cell to a formula...Try out the following:
Create a direct link to a cell in another sheet within the same workbook
Open another workbook.
Create a direct link to a cell in a sheet from the other workbook
You will find that the following would be correct use for a worksheet reference:
Worksheets("SUMMARY").Cells(47, 6).Formula = "='" & sheetName & "'!$H$69"
And the below for a workbook reference:
Worksheets("SUMMARY").Cells(47, 6).Formula = "='[" & Worboookpath & "] & sheetName & "'!$H$69"

Error with Checkbox and Duplicate Sheets

I have designed a code that creates a new worksheet based on a checkbox, and the name is derived from a User Defined Variable. However, if someone unchecks and checks the box, it runs the code again and generates an error due to multiple worksheets having the same name. I understand that this is just the code functioning as it is supposed to, but I want to create an IF:THEN statement where the code checks to see if the name exists. If the sheet exists, the code will do stop itself; if the sheet does not exist, it will run as normal.
How can I do this?
Code below.
Private Sub CheckBox4_Click()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim Protocol As Range
If CheckBox4.Value = True Then
ActiveWorkbook.Unprotect
Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
ws.Name = Sheets("Control").Cells(16, "I")
Set Protocol = Sheets("The Hidden Works").Columns("W:AQ").EntireColumn
Protocol.Copy
ws.Paste
ws.Protect
ws.EnableSelection = xlUnlockedCells
Application.CutCopyMode = False
Worksheets("SUMMARY").Rows("44").EntireRow.Hidden = False
Worksheets("SUMMARY").Cells(44, 3).Value = "='Control'!I16"
Worksheets("SUMMARY").Cells(44, 3).NumberFormat = "General"
Worksheets("SUMMARY").Cells(44, 4).Value = "='Control'!K16"
Worksheets("SUMMARY").Cells(44, 5).Value = "='Control'!L16"
Worksheets("SUMMARY").Cells(44, 6).Value = "=" & ws.Name & "!$H$69"
Worksheets("SUMMARY").Cells(44, 7).Value = "=" & ws.Name & "!$J$69"
Worksheets("SUMMARY").Cells(44, 8).Value = "=" & ws.Name & "!$N$69"
Worksheets("SUMMARY").Cells(44, 9).Value = "=" & ws.Name & "!$P$69"
Worksheets("SUMMARY").Cells(44, 10).Value = "=SUM(F44:I44)/D44"
Worksheets("SUMMARY").Cells(44, 11).Value = "=M44/F3"
Worksheets("SUMMARY").Cells(44, 12).Value = "=" & ws.Name & "!$U$69"
Worksheets("SUMMARY").Cells(44, 13).Value = "=M44/$K$57"
Worksheets("Control").Activate
End If
Application.ScreenUpdating = True
End Sub
You can evaluate if a cell exists on the specifically named sheet to check:
Application.DisplayAlerts = False
If IsError(Evaluate("SHEETNAME!A1")) Then Sheets.Add(After:=Sheets(Sheets.Count)).Name = "SHEETNAME"
Application.DisplayAlerts = True
You can use the below function to check if a Sheet exists:
Function WorksheetExists(sheetName As String) As Boolean
WorksheetExists = Evaluate("ISREF('" & sheetName & "'!A1)")
End Function
Use it like,
If WorksheetExists("Sheet10") Then
Exit Sub
Else
'Your Code
End If
Your code adapted to use the solution:
Private Sub CheckBox4_Click()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim Protocol As Range
Dim sheetName As String
If CheckBox4.Value = True Then
ActiveWorkbook.Unprotect
sheetName = Sheets("Control").Cells(16, "I")
If WorksheetExists(sheetName) Then
Exit Sub
Else
Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
ws.Name = sheetName
Set Protocol = Sheets("The Hidden Works").Columns("W:AQ").EntireColumn
Protocol.Copy
ws.Paste
ws.Protect
ws.EnableSelection = xlUnlockedCells
Application.CutCopyMode = False
Worksheets("SUMMARY").Rows("44").EntireRow.Hidden = False
Worksheets("SUMMARY").Cells(44, 3).Value = "='Control'!I16"
Worksheets("SUMMARY").Cells(44, 3).NumberFormat = "General"
Worksheets("SUMMARY").Cells(44, 4).Value = "='Control'!K16"
Worksheets("SUMMARY").Cells(44, 5).Value = "='Control'!L16"
Worksheets("SUMMARY").Cells(44, 6).Value = "=" & ws.Name & "!$H$69"
Worksheets("SUMMARY").Cells(44, 7).Value = "=" & ws.Name & "!$J$69"
Worksheets("SUMMARY").Cells(44, 8).Value = "=" & ws.Name & "!$N$69"
Worksheets("SUMMARY").Cells(44, 9).Value = "=" & ws.Name & "!$P$69"
Worksheets("SUMMARY").Cells(44, 10).Value = "=SUM(F44:I44)/D44"
Worksheets("SUMMARY").Cells(44, 11).Value = "=M44/F3"
Worksheets("SUMMARY").Cells(44, 12).Value = "=" & ws.Name & "!$U$69"
Worksheets("SUMMARY").Cells(44, 13).Value = "=M44/$K$57"
Worksheets("Control").Activate
End If
End If
Application.ScreenUpdating = True
End Sub
Function WorksheetExists(sheetName As String) As Boolean
WorksheetExists = Evaluate("ISREF('" & sheetName & "'!A1)")
End Function

How to Optimize Excel VBA Formula

A little background: Been working on a file which is accessible by 80 users (concurrent would probably be 10 at a time). Say the sales team leaders need to activate a button to activate codes below to read from another file (A) with 3 sheets of 20000 records per sheet (A.1, A.2, A.3), to read line by line to match the copy and paste into the current file based on the names of each sales person based on criteria.
It seemed to take a long time as each leader has 20 sales staff and the code seemed to jam excel though ;(
If the file it's reading from consists of about 1000 lines or something, it works pretty smooth though.
Hope someone could enlighten me.
Option Explicit
Sub T1CopyDataFromAnotherFileIfSearchTextIsFound()
'Clear Existing Content
Sheets("4").Cells.ClearContents
Sheets("5").Cells.ClearContents
Sheets("6").Cells.ClearContents
Sheets("7").Cells.ClearContents
Sheets("8").Cells.ClearContents
Sheets("9").Cells.ClearContents
Sheets("10").Cells.ClearContents
Sheets("11").Cells.ClearContents
Sheets("12").Cells.ClearContents
Sheets("13").Cells.ClearContents
Sheets("14").Cells.ClearContents
Sheets("15").Cells.ClearContents
Sheets("16").Cells.ClearContents
Sheets("17").Cells.ClearContents
Sheets("18").Cells.ClearContents
Sheets("19").Cells.ClearContents
Sheets("20").Cells.ClearContents
Sheets("21").Cells.ClearContents
Sheets("22").Cells.ClearContents
Sheets("23").Cells.ClearContents
'Team 1 Content Copy >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim Name1, Name4, Name5, Name6, Name7, Name8, Name9, Name10, Name11, Name12, Name13, Name14, Name15, Name16, Name17, Name18, Name19, Name20, Name21, Name22, Name23 As String
Dim strPath As String
Dim wbkImportFile As Workbook
Dim shtThisSheet As Worksheet
Dim shtImportSheet1 As Worksheet
Dim shtImportSheet2 As Worksheet
Dim shtImportSheet3 As Worksheet
Dim lngrow As Long
Dim strSearchString As String
Dim strImportFile As String
Name1 = Sheets("UserAccessAcc").Range("B3").Value
Name4 = Sheets("UserAccessAcc").Range("B6").Value
Name5 = Sheets("UserAccessAcc").Range("B7").Value
Name6 = Sheets("UserAccessAcc").Range("B8").Value
Name7 = Sheets("UserAccessAcc").Range("B9").Value
Name8 = Sheets("UserAccessAcc").Range("B10").Value
Name9 = Sheets("UserAccessAcc").Range("B11").Value
Name10 = Sheets("UserAccessAcc").Range("B12").Value
Name11 = Sheets("UserAccessAcc").Range("B13").Value
Name12 = Sheets("UserAccessAcc").Range("B14").Value
Name13 = Sheets("UserAccessAcc").Range("B15").Value
Name14 = Sheets("UserAccessAcc").Range("B16").Value
Name15 = Sheets("UserAccessAcc").Range("B17").Value
Name16 = Sheets("UserAccessAcc").Range("B18").Value
Name17 = Sheets("UserAccessAcc").Range("B19").Value
Name18 = Sheets("UserAccessAcc").Range("B20").Value
Name19 = Sheets("UserAccessAcc").Range("B21").Value
Name20 = Sheets("UserAccessAcc").Range("B22").Value
Name21 = Sheets("UserAccessAcc").Range("B23").Value
Name22 = Sheets("UserAccessAcc").Range("B24").Value
Name23 = Sheets("UserAccessAcc").Range("B25").Value
strPath = ThisWorkbook.Path
strImportFile = "Book1.xlsx"
On Error GoTo Errorhandler
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set wbkImportFile = Workbooks.Open(Filename:=strPath & "\" & strImportFile, ReadOnly:=True, UpdateLinks:=False)
'Account1>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'strSearchString = Name1
'Set shtThisSheet = ThisWorkbook.Worksheets("1")
Set shtImportSheet1 = wbkImportFile.Worksheets("6-9 Months")
Set shtImportSheet2 = wbkImportFile.Worksheets("10-24 Months")
Set shtImportSheet3 = wbkImportFile.Worksheets("25-36 Months")
With shtImportSheet1
.Columns("L").Insert
.Columns("L").Insert
End With
'Account4>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strSearchString = Name4
Set shtThisSheet = ThisWorkbook.Worksheets("4")
With shtThisSheet.Range("A1")
.Offset(0, 0).Value = "memberid"
.Offset(0, 1).Value = "firstname"
.Offset(0, 2).Value = "lastname"
.Offset(0, 3).Value = "country"
.Offset(0, 4).Value = "ADT"
.Offset(0, 5).Value = "Team"
.Offset(0, 6).Value = "Lastgamingdt"
.Offset(0, 7).Value = "Type"
.Offset(0, 8).Value = "predom"
.Offset(0, 9).Value = "playStatus"
.Offset(0, 10).Value = "HostName"
.Offset(0, 11).Value = "HostLogin"
.Offset(0, 12).Value = "Campaign"
.Offset(0, 13).Value = "GamingOfferType"
.Offset(0, 14).Value = "OfferAmount"
.Offset(0, 15).Value = "Tagcode"
.Offset(0, 16).Value = "TagcodeDescription"
.Offset(0, 17).Value = "Comments"
End With
For lngrow = 2 To shtImportSheet1.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet1.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
'With shtImportSheet1
''.Columns("L").Insert
''.Columns("L").Insert
'End With
shtImportSheet1.Range(shtImportSheet1.Cells(lngrow, 1), shtImportSheet1.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
For lngrow = 2 To shtImportSheet2.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet2.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
shtImportSheet2.Range(shtImportSheet2.Cells(lngrow, 1), shtImportSheet2.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
For lngrow = 2 To shtImportSheet3.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet3.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
shtImportSheet3.Range(shtImportSheet3.Cells(lngrow, 1), shtImportSheet3.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
'Account5>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strSearchString = Name5
Set shtThisSheet = ThisWorkbook.Worksheets("5")
With shtThisSheet.Range("A1")
.Offset(0, 0).Value = "memberid"
.Offset(0, 1).Value = "firstname"
.Offset(0, 2).Value = "lastname"
.Offset(0, 3).Value = "country"
.Offset(0, 4).Value = "ADT"
.Offset(0, 5).Value = "Team"
.Offset(0, 6).Value = "Lastgamingdt"
.Offset(0, 7).Value = "Type"
.Offset(0, 8).Value = "predom"
.Offset(0, 9).Value = "playStatus"
.Offset(0, 10).Value = "HostName"
.Offset(0, 11).Value = "HostLogin"
.Offset(0, 12).Value = "Campaign"
.Offset(0, 13).Value = "GamingOfferType"
.Offset(0, 14).Value = "OfferAmount"
.Offset(0, 15).Value = "Tagcode"
.Offset(0, 16).Value = "TagcodeDescription"
.Offset(0, 17).Value = "Comments"
End With
For lngrow = 2 To shtImportSheet1.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet1.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
With shtImportSheet1
''.Columns("L").Insert
''.Columns("L").Insert
End With
shtImportSheet1.Range(shtImportSheet1.Cells(lngrow, 1), shtImportSheet1.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
For lngrow = 2 To shtImportSheet2.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet2.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
shtImportSheet2.Range(shtImportSheet2.Cells(lngrow, 1), shtImportSheet2.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
For lngrow = 2 To shtImportSheet3.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet3.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
shtImportSheet3.Range(shtImportSheet3.Cells(lngrow, 1), shtImportSheet3.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
'Account6>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strSearchString = Name6
Set shtThisSheet = ThisWorkbook.Worksheets("6")
With shtThisSheet.Range("A1")
.Offset(0, 0).Value = "memberid"
.Offset(0, 1).Value = "firstname"
.Offset(0, 2).Value = "lastname"
.Offset(0, 3).Value = "country"
.Offset(0, 4).Value = "ADT"
.Offset(0, 5).Value = "Team"
.Offset(0, 6).Value = "Lastgamingdt"
.Offset(0, 7).Value = "Type"
.Offset(0, 8).Value = "predom"
.Offset(0, 9).Value = "playStatus"
.Offset(0, 10).Value = "HostName"
.Offset(0, 11).Value = "HostLogin"
.Offset(0, 12).Value = "Campaign"
.Offset(0, 13).Value = "GamingOfferType"
.Offset(0, 14).Value = "OfferAmount"
.Offset(0, 15).Value = "Tagcode"
.Offset(0, 16).Value = "TagcodeDescription"
.Offset(0, 17).Value = "Comments"
End With
For lngrow = 2 To shtImportSheet1.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet1.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
With shtImportSheet1
''.Columns("L").Insert
''.Columns("L").Insert
End With
shtImportSheet1.Range(shtImportSheet1.Cells(lngrow, 1), shtImportSheet1.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
For lngrow = 2 To shtImportSheet2.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet2.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
shtImportSheet2.Range(shtImportSheet2.Cells(lngrow, 1), shtImportSheet2.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
For lngrow = 2 To shtImportSheet3.Cells(shtImportSheet1.Rows.Count, "K").End(xlUp).Row
If InStr(1, shtImportSheet3.Cells(lngrow, "K").Value2, strSearchString, vbTextCompare) > 0 Then
shtImportSheet3.Range(shtImportSheet3.Cells(lngrow, 1), shtImportSheet3.Cells(lngrow, 18)).Copy
shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
End If
Next lngrow
wbkImportFile.Close SaveChanges:=False
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
Sheets("Summary Report View").Select
MsgBox ("Team 1 Cold Call Data Refresh Completed")
End Sub
''>>>>>>>>Account4 onwards to repeat same codes for account 5 - 20..
I'd go retrieving import workbook data sheets data into arrays, thus minimizing import data workbook opening time, and releasing it as soon as possible.
moreover your code has a lot of repetitions and other possible improvements
here follows a possible refactoring of your code to cope with the "data to array" issue and avoiding repetitions:
Sub T1CopyDataFromAnotherFileIfSearchTextIsFound()
Dim Names As Variant ' <--| array that will hold all the "names"
Dim Months6_9 As Variant, Months10_24 As Variant, Months25_36 As Variant ' <--| arrays that will store ImportFile worksheets data
Dim strPath As String, strImportFile As String, strSearchString As String
ClearSheets '<--|'Clear Existing Content
SetNames Names '<--| set the "names"
'Team 1 Content Copy >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strPath = ThisWorkbook.Path
strImportFile = "Book1.xlsx"
On Error GoTo Errorhandler '<---| where is the label???
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
' here try and read data from import workbook to arrays Months6_9, Months10_24, and Months25_36
If Not ReadImportData(strPath & "\" & strImportFile, Months6_9, Months10_24, Months25_36) Then Exit Sub '<--| exit if reading data unsuccessfully
'Account1>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
' what was here has been shifted to
'Account4>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strSearchString = Names(4)
Account Months6_9, Months10_24, Months25_36, ThisWorkbook.Worksheets("4"), strSearchString
'Account5>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strSearchString = Names(5)
Account Months6_9, Months10_24, Months25_36, ThisWorkbook.Worksheets("5"), strSearchString
'Account6>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strSearchString = Names(6)
Account Months6_9, Months10_24, Months25_36, ThisWorkbook.Worksheets("6"), strSearchString
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
Sheets("Summary Report View").Select
MsgBox ("Team 1 Cold Call Data Refresh Completed")
End Sub
which relies on the following helper subs/functions:
The function that reads import workbook worksheets data and stores them into arrays
Function ReadImportData(wbFullName As String, Months6_9 As Variant, Months10_24 As Variant, Months25_36 As Variant) As Boolean
Dim wbkImportFile As Workbook
If Dir(wbFullName) = "" Then Exit Function '<--| exit if there's no such file
On Error Resume Next
Set wbkImportFile = Workbooks.Open(Filename:=wbFullName, ReadOnly:=True, UpdateLinks:=False)
On Error GoTo 0
If wbkImportFile Is Nothing Then Exit Function '<--| exit if you couldn't open the workbook
With wbkImportFile
With .Worksheets("6-9 Months")
.Columns("L:M").Insert
Months6_9 = .Range("A2:R" & .Cells(.Rows.Count, "K").End(xlUp).Row).Value
End With
With .Worksheets("10-24 Months")
Months10_24 = .Range("A2:R" & .Cells(.Rows.Count, "K").End(xlUp).Row).Value
End With
With .Worksheets("25-36 Months")
Months25_36 = .Range("A2:R" & .Cells(.Rows.Count, "K").End(xlUp).Row).Value
End With
End With
wbkImportFile.Close SaveChanges:=False
ReadImportData = True
End Function
the sub the process the single Account
Sub Account(Months6_9 As Variant, Months10_24 As Variant, Months25_36 As Variant, shtThisSheet As Worksheet, strSearchString As String)
PutHeaders shtThisSheet '<--| put headers in passed sheet
ProcessMonths Months6_9, shtThisSheet, strSearchString '<-- process Months6_9 arrayfor passed strSearchString
ProcessMonths Months10_24, shtThisSheet, strSearchString '<-- process Months10_24 array for passed strSearchString
ProcessMonths Months25_36, shtThisSheet, strSearchString '<-- process Months25_36 array for passed strSearchString
End Sub
which on is turn demands the processing of single months-interval to:
Sub ProcessMonths(Months As Variant, shtThisSheet As Worksheet, strSearchString As String)
Dim nRows As Long, nCols As Long, iRow As Long, jCol As Long
nRows = UBound(Months, 1)
nCols = UBound(Months, 2)
ReDim tempArr(1 To nCols) As Variant
With shtThisSheet
For iRow = 1 To nRows
If InStr(1, Months(iRow, 11), strSearchString, vbTextCompare) > 0 Then
For jCol = 1 To nCols
tempArr(jCol) = Months(iRow, jCol)
Next jCol
.Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row + 1).Resize(, nCols).Value = tempArr
End If
Next iRow
End With
End Sub
and then the last ones
Sub PutHeaders(shtThisSheet As Worksheet)
shtThisSheet.Range("A1:R1") = Array("memberid", "firstname", "lastname", "country", "ADT", "Team", _
"Lastgamingdt", "Type", "predom", "playStatus", "HostName", "HostLogin", _
"Campaign", "GamingOfferType", "OfferAmount", "Tagcode", "TagcodeDescription", "Comments")
End Sub
Sub ClearSheets()
Dim i As Long
With ThisWorkbook
For i = 4 To 23
.Sheets(CStr(i)).Cells.ClearContents
Next i
End With
End Sub
Sub SetNames(Names As Variant)
With ThisWorkbook.Sheets("UserAccessAcc")
Names = Application.Transpose(.Range("B5:B25").Value)
Names(1) = .Range("B3").Value
End With
End Sub

Subscript out of Range VBA

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