How to error handle a Next without For Error? - excel

When trying to run the below code, a compile error of for without next or next without for is experienced. The error message keeps appearing in loops (once a for without next error, the next time a next without for error), making it difficult to spot where the error is.
How to check if an "end if" is missing or if there is an indentation error?
Please help!
Sub DataCleaning()
Dim ws As Worksheet
Dim myValue As Variant
Dim StringToFind As String
Dim f, cell, cell1 As Range
Dim LastCol, LastCol1 As Long
Dim i, j, k, l As Integer
Application.DisplayAlerts = False 'Optional
For Each ws In Worksheets
Select Case ws.Name
'Include sheet names to keep on next line (with comma between)
Case "VIE", "CA", "UK", "EU", "CHN", "JP", "AU", "NZ", "KR", "PH", "TH", "ID"
ws.Cells.ClearFormats
Case Else
ws.Delete
End Select
Next ws
Application.DisplayAlerts = True
StringToFind = Application.InputBox("Input Batch Number:")
For Each ws In Worksheets
'myValue = InputBox("Input Batch Number:", ws, 1)
ws.Activate
ActiveSheet.Rows(4).Select
Set cell = Selection.Find(what:="Batch " & StringToFind, After:=ActiveCell, _
LookIn:=xlFormulas, lookat:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
MsgBox "No Order"
Else
cell.Offset(0, -1).Select
ColumnLetter = Split(Cells(1, ActiveCell.Column).Address, "$")(1)
Range(Columns("B"), Columns(ColumnLetter)).EntireColumn.Delete
LastCol = Cells(5, Columns.Count).End(xlToLeft).Column
ws.Activate
ActiveSheet.Rows(5).Select
Set cell1 = Selection.Find(what:="<", After:=ActiveCell, _
LookIn:=xlFormulas, lookat:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
cell1.Select
ColumnLetter1 = Split(Cells(1, ActiveCell.Column).Address, "$")(1)
Range(Cells(1, ColumnLetter1), Cells(1, LastCol)).EntireColumn.Delete
Rows(1).EntireRow.Delete
Range("A1") = "Batch"
Range("A2") = "City"
Range("A3") = "Number"
Range("A4") = "Shipment"
LastCol1 = Cells(4, Columns.Count).End(xlToLeft).Column
With Range("B1")
For j = 2 To LastCol1
Cells(1, j) = StringToFind
Next j
End With
With Range("B2")
For k = 2 To LastCol1
Cells(2, k) = ws.Name
Next k
End With
With Range("B3")
For l = 2 To LastCol1
Cells(3, l) = ""
Next l
End With
Cells(4, LastCol1 + 1) = "Price"
i = 1
Do While Not IsEmpty(Cells(i, 1))
SKUColumn = Cells(i, 1)
If SKUColumn Like "2018" Then
ws.Rows([i]).EntireRow.Delete
Deleted = True
ElseIf SKUColumn Like "2020" Then
ws.Rows([i]).EntireRow.Delete
Deleted = True
ElseIf SKUColumn Like "Accessories" Then
ws.Rows([i]).EntireRow.Delete
Deleted = True
End If
i = i + 1
Loop
Application.ScreenUpdating = True
ws.Copy
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\xxx\Desktop\" & ws.Name & ".csv" _
, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close SaveChanges:=True
Application.ScreenUpdating = False
Next ws
End If 'this line should be in front of `Next ws`

Related

VBA loop selection.find

I want to loop or find multiple value in another sheets. My code doesn't work even after I do..loop the code.
For i = 1 To lastrowBAU
Worksheets(fname).Range("A1:A" & lastrowsheet).Select
Do Until Cell Is Nothing
Set Cell = Selection.find(What:=ThisWorkbook.Worksheets("BAU").Range("A" & i).Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False)
If Not Cell Is Nothing Then
Cell.Activate
ActiveCell.Copy
ActiveCell.Insert Shift:=xlShiftDown
ActiveCell.Offset(1, 0).Select
Selection.Replace What:=ThisWorkbook.Worksheets("BAU").Range("A" & i).Value, _
replacement:=ThisWorkbook.Worksheets("BAU").Range("B" & i).Value, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
Set Cell = Worksheets(fname).Range("A1:A" & lastrowsheet).FindNext(Cell)
End If
Loop
Next i
You need to set the cell before entering the loop
Set cell = rngSrc.Find(sA, LookIn:=xlFormulas, LookAt:=xlPart, _
After:=rngSrc.Cells(rngSrc.Cells.Count), SearchOrder:=xlByRows, MatchCase:=False)
If Not cell Is Nothing Then
however you also need to avoid an endless loop by checking if the search has returned to the first one found.
Option Explicit
Sub macro1()
Dim ws As Worksheet, wsBAU As Worksheet
Dim cell As Range, rngSrc As Range
Dim fname As String, lastrow As Long, lastrowBAU As Long
Dim i As Long, n As Long, first As String
Dim sA As String, sB As String
fname = "Sheet1"
With ThisWorkbook
Set ws = .Sheets(fname)
Set wsBAU = .Sheets("BAU")
End With
With ws
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rngSrc = .Range("A1:A" & lastrow)
End With
With wsBAU
lastrowBAU = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With ws
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rngSrc = .Range("A1:A" & lastrow)
End With
' search and replace
Application.ScreenUpdating = False
For i = 1 To lastrowBAU
sA = wsBAU.Cells(i, "A")
sB = wsBAU.Cells(i, "B")
Set cell = rngSrc.Find(sA, LookIn:=xlFormulas, LookAt:=xlPart, _
After:=rngSrc.Cells(rngSrc.Cells.Count), SearchOrder:=xlByRows, MatchCase:=False)
If Not cell Is Nothing Then
first = cell.Address
Do
' insert cell above
cell.Insert xlDown
cell.Offset(-1).Value2 = cell.Value2
cell.Value2 = Replace(cell.Value2, sA, sB)
' expand search range
n = n + 1
Set rngSrc = ws.Range("A1:A" & lastrow + n)
' find next
Set cell = rngSrc.FindNext(cell)
Loop While cell.Address <> first
End If
Next
Application.ScreenUpdating = True
MsgBox n & " replacements", vbInformation
End Sub

Method ‘Range’ of object’ ‘_worksheet’ failed

I am getting error while pasting the data . I do activate the workbook and sheet and like fully qualified. but not sure what I am missing here.
I am copying the data from 1 workbook to another. Sometimes it is working fine and sometimes I am getting the error. When the data is not copied , row number is set to zero. I am not sure why the data is not copied.
Sub PasteFormattedRange(rgFrom As Range, rgTo As Range)
Dim S As String
Dim rgStart As Range
Dim i As Long, CF_Format As Long
Dim SaveDisplayAlerts As Boolean, SaveScreenUpdating As Boolean
Dim HTMLInClipBoard As Boolean
Dim Handle As Long, Ptr As Long, FileName As String
Dim Rownum, Rownum1, Rownum2, Rownum3 As Integer
Application.DisplayAlerts = False
With Workbooks("Template.xlsm").Worksheets("Sheet1").Columns(1)
Set Rowfind = .Find(What:="CASH FLOW", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False, SearchFormat:=False)
If Rowfind Is Nothing Then
Rownum = 0
Else
Rownum = Rowfind.Row
End If
Set Rowfind = .Find(What:="VARIANCE $000'S", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False, SearchFormat:=False)
If Rowfind Is Nothing Then
Rownum1 = 0
Else
Rownum1 = Rowfind.Row + 1
End If
End With
Set rgStart = Selection
rgFrom.Copy
DoEvents
'Enumerate the clipboard formats
If OpenClipboard(0) Then
CF_Format = EnumClipboardFormats(0&)
Do While CF_Format <> 0
S = String(255, vbNullChar)
i = GetClipboardFormatName(CF_Format, S, 255)
S = Left(S, i)
HTMLInClipBoard = InStr(1, S, "HTML Format", vbTextCompare) > 0
If HTMLInClipBoard Then
Application.CutCopyMode = False
Application.GoTo rgTo
DoEvents
ActiveSheet.PasteSpecial Format:="HTML"
Application.GoTo rgStart
Exit Do
End If
CF_Format = EnumClipboardFormats(CF_Format)
Loop
CloseClipboard
End If
With Newsheet.Columns(1)
Set Rowfind = .Find(What:="CASH FLOW", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False, SearchFormat:=False)
If Rowfind Is Nothing Then
Rownum2 = 0
Else
Rownum2 = Rowfind.Row
End If
Set Rowfind = .Find(What:="VARIANCE $000'S", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False, SearchFormat:=False)
If Rowfind Is Nothing Then
Rownum3 = 0
Else
Rownum3 = Rowfind.Row + 1
End If
End With
Workbooks("Template.xlsm").Worksheets("Sheet1").Range("B" & Rownum & ":BL" & Rownum).Copy
Workbooks(newBook).Activate
Newsheet.Activate
Newsheet.Range("B" & Rownum2 & ":BL" & Rownum2).PasteSpecial Paste:=xlPasteValues
Newsheet.Range("B" & Rownum2 & ":BL" & Rownum2).PasteSpecial Paste:=xlPasteFormats
Workbooks("Template.xlsm").Worksheets("Sheet1").Range("B" & Rownum1 & ":BL" & Rownum1).Copy
Workbooks(newBook).Activate
Newsheet.Activate
Newsheet.Range("B" & Rownum3 & ":BL" & Rownum3).PasteSpecial Paste:=xlPasteValues
Newsheet.Range("B" & Rownum3 & ":BL" & Rownum3).PasteSpecial Paste:=xlPasteFormats

Deleting rows make script go in a loop

I use this piece of code to delete any blank rows in my excel file and then adjust the structure so there won't be a blank hole in the file.
But I found out that this part of the code put my script in an infinite loop.
Does somebody know what I can change to stop this piece of code let my script go in an infinite loop or is there maybe a better way to delete blank rows?
Dim LastRowIndex As Integer
Dim RowIndex As Integer
Dim UsedRng As Range
Set UsedRng = ActiveSheet.UsedRange
LastRowIndex = UsedRng.Row - 1 + UsedRng.Rows.Count
Application.ScreenUpdating = False
For RowIndex = LastRowIndex To 1 Step -1
If Application.CountA(Rows(RowIndex)) = 0 Then
Rows(RowIndex).Delete
End If
Next RowIndex
Application.ScreenUpdating = False
Dim n As Long
The hole code looks like:
Dim cell As Range
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
For Each cell In ActiveSheet.Range("C2:C" & lastRow)
S = vbNullString
If cell.Value <> vbNullString Then
v = Split(cell.Value, " ")
For Each W In v
S = S & Left$(W, 1) & "."
Next W
cell.Offset(ColumnOffset:=-1).Value = S
End If
Next cell
Application.Range("B1").Value = "tesing"
Worksheets("sheet1").Range("B1").Font.Bold = True
Columns("D").Replace What:="vander", _
Replacement:="van der", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("D").Replace What:="vanden", _
Replacement:="van den", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("B").Replace What:="..", _
Replacement:=".", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
'Beta code'
Dim r As Range
For Each r In ActiveSheet.UsedRange
If Not IsError(r.Value) Then
v = r.Value
If v <> vbNullString Then
If Not r.HasFormula Then
r.Value = Trim(v)
End If
End If
End If
Next r
'NIEUW NIEUW NIEUW NIEUW NIEUW NIEUW NIEUW NIEUW '
ActiveWorkbook.Worksheets("sheet1").Range("A2:Z5000").Font.Bold = False
ThisWorkbook.ActiveSheet.Cells.Range("A2:Z5000").ClearFormats
Range("A1:Z5000").Font.Color = vbBlack
Range("G2:G5000,A2:A5000,H2:H5000").Clear
Worksheets("sheet1").Columns("A:M").AutoFit
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const RolesList As String = "Testing"
Const FirstCellAddress As String = "L2"
Const Delimiter As String = "||"
Dim rng As Range
With Range(FirstCellAddress)
Set rng = Intersect(.Resize(.Worksheet.Rows.Count - .Row + 1), Target)
End With
If rng Is Nothing Then
Exit Sub
End If
Dim Roles() As String: Roles = Split(RolesList, ",")
Dim dRng As Range
Dim aRng As Range
Dim cel As Range
Dim Curr() As String
Dim cMatch As Variant
Dim n As Long
Dim isFound As Boolean
For Each aRng In rng.Areas
For Each cel In aRng.Cells
If Not IsError(cel) Then
Curr = Split(cel.Value, Delimiter)
For n = 0 To UBound(Curr)
cMatch = Application.Match(Curr(n), Roles, 0)
If IsError(cMatch) Then
isFound = True
Exit For
Else
' Remove this block if you don't need case-sensitivity.
If StrComp(Curr(n), Roles(cMatch - 1), _
vbBinaryCompare) <> 0 Then
isFound = True
Exit For
End If
End If
Next n
If isFound Then
isFound = False
If dRng Is Nothing Then
Set dRng = cel
Else
Set dRng = Union(dRng, cel)
End If
End If
End If
Next cel
Next aRng
Application.ScreenUpdating = False
rng.Interior.Color = xlNone
If Not dRng Is Nothing Then
dRng.Interior.Color = vbRed
End If
Application.ScreenUpdating = True
End Sub
I replaced the code above with, it works now:
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Dim i As Long
Dim DelRange As Range
On Error GoTo Whoa
Application.ScreenUpdating = False
For i = 1 To 50
If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "Z" & i)) = 0 Then
If DelRange Is Nothing Then
Set DelRange = Range("A" & i & ":" & "Z" & i)
Else
Set DelRange = Union(DelRange, Range("A" & i & ":" & "Z" & i))
End If
End If
Next i
If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp
LetsContinue:
Application.ScreenUpdating = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue

copy rows between header and the last row

I found and modified a code that works pretty well but I am struggling with Set CopyRng = sh.Range("A11:AI15") . What I want to do is to create a dynamic range that will copy values from first table from each worksheet between header called Language and last row in table called Total. Some cells are merged in the table (default template) and there are empty columns in the table (so .CurrentRegion doesn't work).
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set DestSh = ActiveWorkbook.Worksheets("test")
' Loop through all worksheets and copy the data to the
' summary worksheet.
For Each sh In ActiveWorkbook.Worksheets
If sh.Name Like "test*" Then
Last = LastRow(DestSh)
' Specify the range to place the data.
Set CopyRng = sh.Range("A11:AI15")
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial
Application.CutCopyMode = False
End With
DestSh.Cells(Last + 1, "A").Resize(CopyRng.Rows.Count).Value = sh.Range("F8")
DestSh.Cells(Last + 1, "AK").Resize(CopyRng.Rows.Count).Formula = "=AG10*3%"
DestSh.Cells(Last + 1, "AL").Resize(CopyRng.Rows.Count).Formula = "=AG10+AK10"
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Regards,
Here is a function that will return you the range. Parameters explained:
oW = The worksheet you want to get the range from
sStartColHeader = Holds the name of the header column you want to start the range from (i.e. in your example this would be "Language")
Function GetRange(ByVal oW As Worksheet, ByVal sStartColHeader As String) As Range
Dim oTotRng As Range: Set oTotRng = oW.Cells.Find("total", oW.Cells(1, 1), xlValues, xlPart, xlByRows, xlNext, False, , False)
Dim oLan As Range
Set oLan = oW.Cells.Find(sStartColHeader, oW.Cells(1, 1), xlValues, xlPart, xlByRows, xlNext, False, , False)
If oLan Is Nothing Then
Set GetRange = Nothing
Else
Set GetRange = Range(oLan.Offset(1, 0), oTotRng.Offset(0, 1))
End If
End Function
How to use this function:
In CopyRangeFromMultiWorksheets function, change Set CopyRng = sh.Range("A11:AI15") to Set CopyRng = GetRange(sh, "Language"). Then have an If condition to check if a range was returned. For example:
Set CopyRng = GetRange(sh, "Language")
If CopyRng Is Nothing Then
' your exception code here as range was not returned
Else
' rest of your code here as a range was returned
End If
NOTE: Presumption is that the actual total amount in the sheet is in the cell to the right of the cell that contains the text Total. So if "H10" has text Total, actual total is held in cell "I10"
I have found a solution that works great. See the code below:
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
Dim findrow As Long, findrow2 As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set DestSh = ThisWorkbook.Worksheets("Summary")
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
Last = LastRow(DestSh)
'this method doesn't work with merged cells thhat is why I have to unmerge them first.
sh.Range("B10:B200").UnMerge
findrow = sh.Range("B:B").Find("Language Pair", sh.Range("B1")).Row
findrow2 = sh.Range("B:B").Find("Total", sh.Range("B" & findrow)).Row
Set CopyRng = sh.Range("A" & findrow + 1 & ":AJ" & findrow2 - 1)
CopyRng.Copy
With DestSh.Cells(Last + 1, "B")
.PasteSpecial
Application.CutCopyMode = False
End With
DestSh.Cells(Last + 1, "A").Resize(CopyRng.Rows.Count).Value = sh.Range("F8")
DestSh.Cells(Last + 1, "AK").Resize(CopyRng.Rows.Count).Formula = "=AG10*3%"
DestSh.Cells(Last + 1, "AL").Resize(CopyRng.Rows.Count).Formula = "=AG10+AK10"
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function

Find cell with value, offset and copy range then paste basing data's date, then loop to findnext

With th following Excel Sheet.
I'm trying to do the following:
Find the cell with Value, let's say "Sam", in range("B17:B25")
Offset(0,5).resize(,8).copy
Find the Date value of the Data row, and paste Data to range("B4:M4") according to the data's Date.
Loop to find next.
Here is what I got so far, don't know how to loop:
Sub getDat()
Dim myFind As Range
Dim pasteLoc As Range
Dim payee, pasteMon As String
Range("B5:M12").ClearContents
With Sheet3.Cells
payee = Range("B2").Text
Set myFind = .Find(What:=payee, After:=Range("B16"), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If Not myFind Is Nothing Then
myFind.Offset(0, 3).Resize(, 8).Copy
pasteMon = myFind.Offset(0, 1).Text
With Range("B4:M4")
Set pasteLoc = .Find(What:=pasteMon, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If Not pasteLoc Is Nothing Then
pasteLoc.Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True
End If
End With
End If
End With
End Sub
Here is simplified version (not tested)
Sub getDat()
Range("B5:M12").ClearContents
Dim c As Range, r As Range
For Each c in Range("B16").CurrentRegion.Columns(1).Cells
If c = Range("B2") Then
Set r = Range("B4:M4").Find(c(, 2))
If Not r Is Nothing Then
r(2).Resize(8) = Application.Transpose(c(, 4).Resize(, 8))
End If
End If
Next
End Sub
Something like this For loop would work as well:
Sub getDat()
Dim payee As String
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
payee = Range("B2").Value
Range("B5:M12").ClearContents
For x = 17 To lastrow
If Cells(x, 2).Value = payee Then
For y = 2 To 13
If Cells(4, y).Value = Cells(x, 3).Value Then
Range("E" & x & ":L" & x).Copy
ActiveSheet.Range(Cells(5, y), Cells(12, y)).PasteSpecial Transpose:=True
Exit For
End If
Next y
End If
Next x
End Sub

Resources