How can I hide zero rows in every worksheet in Excel - excel

I want to hide rows that have a zero values in columns B & C. My code works but only on the active worksheet. I want it to loop through all worksheets in the workbook. Any help is appreciated
Private Sub CommandButton1_Click()
Dim M As Long, LastRow As Long
Dim ws As worksheet
For Each ws In ActiveWorkbook.Worksheets
LastRow = ws.Range("E65536").End(xlUp).Row
For M = LastRow To 7 Step -1
If Range("B" & M).Value = 0 And Range("C" & M).Value = 0 Then
Range("B" & M).EntireRow.Hidden = True
End If
Next M
Next ws
End Sub

activate the sheet. The issue is that Range is working off the current active sheet. If you use ws. infromt of range or activate the worksheet such as below.
For Each ws In ActiveWorkbook.Worksheets
add
ws.Activate

I found this about the subject, I hope it helps: ExtendOffice
Sub Hide_rows()
Dim LastRow As Long
Dim Rng As Range
LastRow = Range("A65536").End(xlUp).Row '
Set Rng = Range("A1:A" & LastRow) '
Application.ScreenUpdating = False
For Each cell In Rng
If cell.Value = "0" Then
cell.EntireRow.Hidden = True
End If
Next cell
Application.ScreenUpdating = True
End Sub

You need to specify the Range as a member of the worksheet like so:
Private Sub CommandButton1_Click()
Dim M As Long, LastRow As Long
Dim ws As worksheet
For Each ws In ActiveWorkbook.Worksheets
LastRow = ws.Range("E65536").End(xlUp).Row
For M = LastRow To 7 Step -1
' Notice how 'ws' has been added before range
If ws.Range("B" & M).Value = 0 And ws.Range("C" & M).Value = 0 Then
ws.Range("B" & M).EntireRow.Hidden = True
End If
Next M
Next ws
End Sub

Related

copy row to new worksheet to next available row

How would you copy a row and paste it to the next available row to another worksheet. Currently the code below only pastes according to the entry row of the mastersheet.
Sub ShowMonth()
Dim k As Long
For k = 2 To 9999
Cells(k, 14).Value = Month(Cells(k, 1).Value)
Next k
End Sub
Private Sub Move()
Dim MonthNo As Range
Dim lastrow, j As Long
Set MonthNo = Worksheets("MasterSheet").Range("N2:N9999")
lastrow = Worksheets("MasterSheet").Cells(Rows.Count, "A").End(xlUp).Row
For j = 2 To 9999
If MonthNo(j) = 1 Then
lastrow = lastrow + 1
MonthNo(j).Rows.EntireRow.Copy Destination:=Worksheets("Jan").Range("A" & lastrow)
ElseIf MonthNo(j) = 2 Then
lastrow = lastrow + 1
MonthNo(j).Rows.EntireRow.Copy Destination:=Worksheets("Feb").Range("A" & lastrow)
End If
Next
End Sub
I have commented the code so that you should not have a problem understanding the code.
Option Explicit
Private Sub Move()
Dim wsLrow As Long, lastrow As Long, i As Long
Dim ws As Worksheet
Dim wsDest As String
On Error GoTo Whoa
'~~> Set your worksheet
Set ws = Worksheets("MasterSheet")
With ws
'~~> Find the last row in Col N
wsLrow = .Cells(.Rows.Count, "N").End(xlUp).Row
'~~> Loop through the cells in Col N
For i = 2 To wsLrow
Select Case .Range("N" & i).Value2
Case 1: wsDest = "Jan"
Case 2: wsDest = "Feb"
Case 3: wsDest = "Mar"
'
' And so on. Add more if applicable
'
End Select
If wsDest <> "" Then
With Worksheets(wsDest)
'~~> Find the row in the destination worksheet to copy
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
'~~> Copy the row from MasterSheet to relevant sheet
ws.Rows(i).Copy Destination:=.Rows(lastrow)
End With
wsDest = ""
End If
Next i
End With
LetsContinue:
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub

How to write Pythagoras formula in excel VBA, like I need to select all the values of column A and column B

Sub MS()
Data = Sheets("Tabelle1").Select
Rows("1:1").Select
Rows("11409:11409").Select
Dim bilder As Long
Dim n As Long
Dim d As Long
Dim t As Long
bilder = 64
n = 1
d = 0
t = 0
'Dim i As Long
'For i = 1 To lastrow
Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Range("b1:b" & Cells(Rows.Count, 1).End(xlUp).Row).Select
'Range("a1").Select
'Range("b1").Select
Range("a1,b1").Select
Do While ActiveCell.Value <> ""
Radius = Sqr(Range("A1").Value * Range("A1").Value + Range("B1").Value * Range("B1").Value)
ActiveCell.Offset(1, 1).Select
Loop
End Sub
I'm not sure why you'd want to do it this way (given that it can be done with a simple formula in-cell), but looking at the remnants of code in your question we can see what you're trying to achieve. Here's how I'd do it:
Sub MS()
Dim sht As Worksheet, StartRow As Long, LastRow As Long, OutputColumn As Long
Dim SideA As Double, SideB As Double, SideC As Double
With Worksheets("Tabelle1")
'Set StartRow to the first row of your data ignoring headers
StartRow = 2
'Locate LastRow as last occupied cell in column A
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Set OutputColumn to 3
OutputColumn = 3
'Start loop
For r = StartRow To LastRow
SideA = .Cells(r, 1).Value
SideB = .Cells(r, 2).Value
SideC = Sqr(SideA * SideA + SideB * SideB)
.Cells(r, OutputColumn).Value = SideC
Next
End With
End Sub
Output:
You do not need to select the range to work with it. You may want to see How to avoid using Select in Excel VBA
In your code you are not writing the output to any cell. Here are two ways that will help you achieve what you want.
NON VBA - WAY 1
Put the formula =SQRT(A1*A1+B1*B1) or =SQRT(A1^2+B1^2) in C1 and drag it down
VBA - WAY 2 (Without Looping)
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Set ws = Sheets("Tabelle1")
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("C1:C" & lRow)
.Formula = "=SQRT(A1*A1+B1*B1)"
.Value = .Value
End With
End With
End Sub
VBA - WAY 3 (Without Looping) Slightly complicated way of doing this. Explanation can be seen HERE
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Set ws = Sheets("Tabelle1")
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("C1:C" & lRow)
.Value = Evaluate("index(SQRT((A1:A" & lRow & _
")^2+(B1:B" & lRow & _
")^2),)")
End With
End With
End Sub

Copy & paste each unique value from one sheet to another

I may have up to 8 unique values in column D. I am looking for a code that will copy & paste each row with unique value to a new sheet.
So I may have up to 8 new sheets.
Could you help me to build the code that will do that?
This is what I have so far:
Option Explicit
Sub AddInstructorSheets()
Dim LastRow As Long, r As Long, iName As String
Dim wb As Workbook, ws As Worksheet, ts As Worksheet, nws As Worksheet
Dim i As Integer
Dim m As Integer
'set objects
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Set ts = Sheets("Master")
'set last row of instructor names
LastRow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'add instructor sheets
On Error GoTo err
Application.ScreenUpdating = False
For r = 17 To LastRow 'assumes there is a header
iName = ws.Cells(r, 4).Value
With wb 'add new sheet
ts.Copy After:=.Sheets(.Sheets.Count) 'add template
Set nws = .Sheets(.Sheets.Count)
nws.Name = iName
Worksheets(iName).Rows("17:22").Delete
Worksheets("Master").Activate
Range(Cells(r, 2), Cells(r, 16)).Select
Selection.Copy
m = Worksheets(iName).Range("A15").End(xlDown).Row
Worksheets(iName).Cells(m + 1, 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
Next r
err:
ws.Activate
Application.ScreenUpdating = True
End Sub
The thing is that this macro is creating new sheets, which is not necessary. I only want to make following.
If you find a unique value in column D (which will have exact name as other sheet), find this sheet and paste whole row in there.
Sub CopyFromColumnD()
Dim key As Variant
Dim obj As Object
Dim i As Integer, lng As Long, j As Long
Dim sht As Worksheet, mainsht As Worksheet
Set obj = CreateObject("System.Collections.ArrayList")
Set mainsht = ActiveSheet
With mainsht
lng = .Range("D" & .Rows.Count).End(xlUp).Row
With .Range("D1", .Range("D" & lng))
For Each key In .Value
If Not obj.Contains(key) Then obj.Add key
Next
End With
End With
For i = 0 To obj.Count - 1
Set sht = Sheets.Add(After:=Sheets(Sheets.Count))
sht.Name = obj(i)
For j = 1 To lng
If mainsht.Cells(j, 4).Value = obj(i) Then
mainsht.Rows(j).EntireRow.Copy Destination:=Range("A1")
Exit For
End If
Next
Next
End Sub
Ok, I did the workaround. I have created a list of unique values in a separate sheet.
Sub copypaste()
Dim i As Integer
Dim j As Integer
LastRow = Worksheets("Master").Range("D17").End(xlDown).Row
For i = 17 To LastRow
For j = 2 To 10
Workstream = Worksheets("Database").Cells(j, 5).Value
Worksheets("Master").Activate
If Cells(i, 4) = Worksheets("Database").Cells(j, 5).Value Then
Range(Cells(i, 2), Cells(i, 16)).Select
Selection.Copy
Worksheets(Workstream).Cells(1, 1).PasteSpecial Paste:=xlPasteValues
Else
End If
Next j
Next i
End Sub
Thank you everyone for help and your time!

How to delete the values which are not followed by second immediate cell?

How to delete the values which are not followed by second immediate cell?
Following might help.
Sub Demo()
Dim ws As Worksheet
Dim lastRow As Long
Dim cel As Range
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet5 to your data sheet
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Each cel In .Range("B1:B" & lastRow)
If IsEmpty(cel) Then 'or use If Len(cel) = 0 Then
cel.Offset(0, -1).ClearContents
End If
Next cel
End With
End Sub
If you want to delete the rows where Column B is empty then try this
Sub Demo()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet5 to your data sheet
ws.Range("B1:B100").SpecialCells(xlCellTypeBlanks).EntireRow.Delete 'change range as per your data
End Sub
Use this macro. Enter it in regular module (eq Module 1). it will remove all values in column A in case there is no value in adjacent cell in column B.
Sub delete()
For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If Range("B" & x).Value <> "" Then
Else
Range("A" & x).ClearContents
End If
Next x
End Sub
or if you want to delete those rows.
Sub deleteRows()
For x = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
If Range("B" & x).Value <> "" Then
Else
Range("A" & x).EntireRow.delete
End If
Next x
End Sub

Copying rows from one sheet to another

The following script seems like it should work, but I'm getting an "Object defined" error on the lines marked below. I can't find what's causing this at all...
Sub MailMerge()
Sheets.Add.Name = "MailMerge"
Dim MailMerge As Worksheet
Set MailMerge = Sheets("MailMerge")
Dim Rng As Range
Dim i, index, lastrow As Long
Dim Abstracts As Worksheet
Set Abstracts = Sheets("Abstracts")
lastrow = Abstracts.Cells(Rows.Count, 1).End(xlUp).row
For i = 1 To lastrow
Set Rng = Abstracts.Range("O" & i)
If WorksheetFunction.CountA(Rng) >= 1 Then
Abstracts.Range("A" & i).Resize(0, 14).Copy _
Destination:=MailMerge.Range("A" & i).Resize(0, 14)
'this is where the error is occuring
End If
Next
End Sub
Any suggestions?
Resize is not like OFFSET. It will set the size of the range to the size dictated. So you are setting the range size to 0 rows. It should be 1:
Sub MailMerge()
Sheets.Add.Name = "MailMerge"
Dim MailMerge As Worksheet
Set MailMerge = Sheets("MailMerge")
Dim Rng As Range
Dim i, index, lastrow As Long
Dim Abstracts As Worksheet
Set Abstracts = Sheets("Abstracts")
lastrow = Abstracts.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastrow
Set Rng = Abstracts.Range("O" & i)
If WorksheetFunction.CountA(Rng) >= 1 Then
Abstracts.Range("A" & i).Resize(1, 14).Copy _
Destination:=MailMerge.Range("A" & i).Resize(1, 14)
'this is where the error is occuring
End If
Next
End Sub

Resources