I have two vba Code and i want to combine as single process. Need someone help please.
1st Code:
Sub DelAllZeros()
Application.Calculation = xlCalculationManual
Dim ws As Worksheet
For Each ws In Worksheets
On Error Resume Next
Set frange = ws.Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not frange Is Nothing Then
For Each c In frange
If c.Value = 0 Then
c.Formula = ClearContents
End If
Next c
End If
Set frange = Nothing
Next ws
End Sub
2nd Code:
Sub DelAllZeros1()
Dim ws As Worksheet
For Each ws In Worksheets
On Error Resume Next
ws.Select
Cells.Replace What:="0", Replacement:="", LookAt:=xlWhole, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next ws
End Sub
1st code will clear the "0" from formula cells and seconds code will clear non formula cells.
Try the code below (modifications inside the code comments)
Option Explicit
Sub DelAllZerosCombined()
Application.Calculation = xlCalculationManual
Dim ws As Worksheet, c As Range, Rng As Range
For Each ws In Worksheets
' set range to occupied range in worksheet (save time in loop)
Set Rng = ws.Range("A1:" & ws.Cells.SpecialCells(xlCellTypeLastCell).Address)
If Not Rng Is Nothing Then
For Each c In Rng
If c.Value = 0 Then
' unmerge "merged" cells
If c.MergeCells Then c.UnMerge
c.ClearContents
End If
Next c
End If
Set Rng = Nothing
Next ws
' resume setting
Application.Calculation = xlCalculationAutomatic
End Sub
Try this code. This will clear the 0s from the used range in a sheet without changing the format.
Updated:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Replace what:=0, Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next ws
Use the setting in File > Options > Advanced and untick the "Show zeros ..." setting. This setting is on a per sheet basis, so, to automate it for the whole workbook, put this code into the ThisWorkbook module
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
ActiveWindow.DisplayZeros = False
End Sub
Now zeros will not display on any sheet.
Related
I'm adding functionality to a report where some columns will be output with a cell containing 'Delete Me'.
I'm looking to loop a find function in the macro to find 'Delete Me', then delete the entire column.
At the moment, there will be a maximum of three instances of 'Delete Me', so I'm looping three times.
When there are three instances of 'Delete Me', the below Sub works fine, but when there are fewer than three, I get an error message: "Run-time error '91': Object variable or With block variable not set"
Sub Test()
Dim i As Integer
For i = 1 to 3
Cells.Find(What:="Delete Me", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.EntireColumn.Delete
Next i
End Sub
What I'd like to do is loop until the find function doesn't find a 'Delete Me', then exit the Sub.
Delete Columns Containing a Criteria
Usually, a combination of Find and FindNext is used in such a case.
Combining the found cells into a range and then deleting the entire columns of the range in one go will probably also be more efficient.
Note that xlPart means it will also delete aaa delete me aaa opposed to xlWhole where this wouldn't be deleted.
Option Explicit
Sub DeleteColumns()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim rg As Range: Set rg = ws.UsedRange
Dim fCell As Range
Set fCell = rg.Find(What:="Delete Me", _
After:=rg.Cells(rg.Rows.Count, rg.Columns.Count), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Dim drg As Range
If Not fCell Is Nothing Then
Dim FirstCellAddress As String: FirstCellAddress = fCell.Address
Do
' Combine found cells into the Delete range.
If drg Is Nothing Then
Set drg = fCell
Else
Set drg = Union(drg, fCell)
End If
Set fCell = rg.FindNext(After:=fCell)
Loop Until fCell.Address = FirstCellAddress
End If
If drg Is Nothing Then
MsgBox "Nothing deleted.", vbExclamation
Else
drg.EntireColumn.Delete
MsgBox "Columns deleted.", vbInformation
End If
End Sub
I have a workbook that is used as a template to fill in data. The data is cleared and the workbook is reused.
The workbook has multiple worksheets and the range that needs to be cleared is different in every worksheet.
Let's say I want to clear the data in the range A10:Y50, I put value "Start" in the cell Z10, as a starting point to clear data. "Start" is located in different cell in every worksheet.
The code is clearing data based on the "Start" located in the first worksheet and not independently for each worksheet.
Sub TestReset()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
If sht.Name <> "Sheet1" And sht.Name <> "Sheet2" Then '
Dim iRow As Long, iMax As Long
iRow = Cells.Find(What:="start", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
iMax = Cells(iRow, "A").End(xlDown).Row
sht.Range("A" & iRow & ":AY" & iMax).ClearContents
End If
Next sht
End Sub
As #Tony Dallimore mentioned in his comment you need to specify in what sheet you are looking for specific cells (if you dont specify it it assumes you are looking in ActiveSheet). So it is always best to specify with what sheet you work. It is good to use With statement for that. When you use With then it is enough to use only dot "."
Sub TestReset()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
With sht
If .Name <> "Sheet1" And .Name <> "Sheet2" Then '
Dim iRow As Long, iMax As Long
iRow = .Cells.Find(What:="start", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
iMax = .Cells(iRow, "A").End(xlDown).Row
.Range("A" & iRow & ":AY" & iMax).ClearContents
End If
End with
Next sht
End Sub
Also use Dim iRow as Long before for each loop... But here is added example of how you can manage sheets to skip (Create sheet "Setup" and in cell A1 add name to skip, for example Sheet1, in cell A2 add Sheet2 and it should do the trick.
Sub WorkInUnSpecifiedSheets()
Dim xRng As Range
'sheet "Setup" must exist and Range A1 contains name of sheet to skip, current region might not work on some PCs. But it is simple
Set xRng = ThisWorkbook.Sheets("Setup").Range("A1").CurrentRegion
'you can also use another method to specify range... Named ranges for example or using last row and last column...
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets 'i would recommend using ThisWorkbook or Workbook variable instead of Active
If xRng.Find(What:=sht.Name, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) Is Nothing Then
'your code to work in unspecified sheets
End If
Next sht
End Sub
My code below works but it is very slow… This code in fact consists to convert the date in column C and D of my sheet (called "Test") from format day.month.year to format day/month/year (For example please see the picture below, the lines 1-2-3-4-5 have been already converted whereas the other lines from line 1183 have not been converted yet).
I am looking for a solution to improve the speed of this macro because if I have a lot of lines to convert in column C and D, the macro is really really slow…
If by chance someone know how to improve the speed of this macro, that would be really fantastic.
Sub convertdatrighteuropeanformat()
Dim cell As Range
Call selectallmylinesctrlshiftdown
Application.ScreenUpdating = False
For Each cell In Selection
With cell
.NumberFormat = "#"
.Value = Format(.Value, "dd/mm/yyyy")
End With
Next cell
Selection.Replace What:="/", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.ScreenUpdating = True
End Sub
Sub selectallmylinesctrlshiftdown()
With Sheets("Test")
.Range(.Range("D2"), .Range("E2").End(xlDown)).Select
End With
End Sub
Instead of a loop, refer to the entire Range (previously Selection) at once inside the With block. This is combined into one sub, although there is nothing wrong with your decision to declare the range with a stand alone procedure.
Option Explicit
Sub convert()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Test")
Dim LRow As Long, MyCell As Range, MyRange As Range
LRow = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
Set MyRange = ws.Range("D2:E" & LRow)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With MyRange
.Value = Format(.Value, "dd/mm/yyyy")
.Replace "/", ".", xlPart, xlByRows
.NumberFormat = "#"
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I want to clear the contents if cell value is zero ((Either its in formula or value) The below code is working by selection of cells, but i want to do this for entire workbook please help me to change the code.
Sub DelZeros()
Dim c As Range
For Each c In Selection
If c.Value = 0 Then c.ClearContents
Next c
End Sub
Instead of Selection you could use ActiveSheet.UsedRange And if you need to do it for all sheets in a workbook you could do sth like that
Sub DelAllZeros()
Cells.Replace What:="0", Replacement:="", LookAt:=xlWhole, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
Sub DelFormulaZeros()
Dim rg As Range, sngCell As Range
Dim sh As Worksheet
Dim result As Long
For Each sh In Worksheets
On Error Resume Next
Set rg = sh.Cells.SpecialCells(xlCellTypeFormulas, 1)
result = Err.Number
On Error GoTo 0
If result = 0 Then
For Each sngCell In rg
If sngCell.Value = 0 Then
sngCell.ClearContents
End If
Next
End If
Next
End Sub
Sub DelAllZeros()
Application.Calculation = xlCalculationManual
Dim ws As Worksheet
For Each ws In Worksheets
On Error Resume Next
ws.Cells.Replace What:="0", Replacement:="", LookAt:=xlWhole, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Set frange = ws.Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not frange Is Nothing Then
For Each c In frange
If c.Value = 0 Then
c.Formula = ClearContents
End If
Next c
End If
Set frange = Nothing
Next ws
Application.Calculation = xlCalculationAutomatic
End Sub
Goal:
Search Sheet1 for keywords in column 18 (keywords:case,etc.)
Once keyword is found, offset (0,-11)
enter the given value C1008 in respective cell.
and repeat the same till it completes full column (about 1500 rows)
I am not able to perform 4th step.
And I need multiple keywords to search and perform the same steps.
Program:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Columns(18).Find(What:="case", LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
End With
End Sub
If you want just to repeat your code use a loop statement like this:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
DIM isTimetoExit As Boolean
Dim currentRow As Long 'Counter of Rows
Dim KeyWord as String
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
isTimetoExit = False
currentRow = 1 'If you start from row 1
KeyWord = "case"
DO
'
Set aCell = .Columns(18).Find(What:=KeyWord, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
'
' Whit a condition go out of loop
isTimetoExit = NOT (Trim(.Cells(currentRow, 1).Value & "") = "")
If (isTimetoExit) THEN EXIT DO
'Go for next row
currentRow = currentRow + 1
' And set the keyword to your new value;
KeyWord = "next case" 'But I don't know where are those KeyWords stroes !?
LOOP
End With
End Sub