using VBA to find the last row of a merged cell - excel

I am trying to use find the last row of a merged cell with text and hide all rows beside that
For example:
A1:A5 is a merged cell with text "A", A6:A10 is a merged cell with text "B", etc
I want to write a code that would find the last row of the merged cell with text "B", and would hide any rows above or below the merged cell.
At the moment I am defining the rows to hide manually, but these change frequently so my method is not very efficient.
Any suggestions on how to find the last row instead?
Sub FindLastRow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Test")
'Hide all rows above B
ws.Rows("1:5").EntireRow.Hidden = True
'Hide all rows below B
ws.Rows("11:80").EntireRow.Hidden = True
End Sub

I guess you could try the following, making use of Range.Find and Range.MergeArea:
Sub Test()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Test")
Dim fullRng As Range, fndRng As Range
Set fullRng = ws.Range("A1:A80")
Set fndRng = ws.Range("A1:A80").Find(What:="B", Lookat:=xlWhole)
If Not fndRng Is Nothing Then
fullRng.Rows.Hidden = True
fndRng.MergeArea.Rows.Hidden = False
End If
End Sub

Related

To stop pasting the formula if the last line in Column A ends

I am working on a vba small code which extract date from Column A into Column C. the current code puts the formulas to extract date from Cell C2 to C2500, However if the data in Column A ends at line A600 it still goes down till C2500. Is it possible if we amend the code to stop pasting the formula exactly at the last line of Column A. so that i do not need to manually delete those cells "#Value". e.g. see print shot.
Sub Formula_property()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheets("Sheet3")
wb.Activate
ws.Select
Range("C2:C2500").Formula = "=extractDate(A2:A2)"
End Sub
Assuming that all columns have the same number of rows (except column B which is empty) - we can use CurrentRegion to get the size of the target "for free"
Sub formula()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet3")
Dim rg As Range
Set rg = ws.Range("A1").CurrentRegion
Set rg = rg.Columns(1)
With rg
.Offset(1, 1).Resize(.Rows.Count - 1).formula = "=extractDate(A2:A2)"
End With
End Sub
BTW: activate/select is not necessary - I recommend reading How to avoid using select.

VBA help copy/paste in same sheet in next available row

I want to create a command button which copies a range of cells and pastes them into the next empty range.
I have found a code online which I tweaked to perform the function, but it does not work when I add conditional formatting.
The conditional formating being, blank cells = yellow.
The VBA im currently using is:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet1")
copySheet.Range("B11:J11").Copy
pasteSheet.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
If I enter a value into the blank cell the above VBA works fine, however if I leave the cell blank it does not paste into the next cell.
The aim was for the user to paste in as many rows as needed, and the yellow shading to indicate which cells to add a value in.
I hope this makes sense. I'm not particularly used to these functions in excel.
Try this code:
Private Sub CommandButton1_Click()
'Macro to copy in a new row.
'Turning off screen updating.
Application.ScreenUpdating = False
'Declarations.
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Dim targetRange As Range
'Setting variables.
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet1")
'Setting targetRange as the last cell in column B with value.
Set targetRange = pasteSheet.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
'Setting targetRange as the first cell in column B with no conditional formatting under the last cell in column B with no value.
Do Until targetRange.FormatConditions.Count = 0
Set targetRange = targetRange.Offset(1, 0)
Loop
'Copying range B11:J11.
copySheet.Range("B11:J11").Copy
'Pasting the copied range in targetRange.
targetRange.PasteSpecial xlPasteAll
'Turning off the cut-copy mode.
Application.CutCopyMode = False
'Turning on the screen updating.
Application.ScreenUpdating = True
End Sub
I've taken your code and added the variable targetRange. Said variable is then set as the last cell with value in column B (similar to what you have already done) and then i use a Do Loop cycle to set targetRange as the first cell with no conditional formatting under the last cell with value in column B. I've also added the proper comments to the whole code.
Extra code as requested in comments.
You can obtain a sum of the values of a range while counting any "outgoing" value as a 7 with this formula:
=SUM(B11:B15,COUNTIF(B11:B15,"ongoing")*7)
You can use the same formula in a macro like this:
Sub Macro1()
'A example of macro to return a range sum with any "ongoing" switched with 7.
'Declaration.
Dim rng As Range
'Setting the seed range.
Set rng = Range("B11")
'Expanding rng to the last cell with value of its column.
Set rng = Range(rng, Cells(Rows.Count, rng.Column).End(xlUp))
'Reporting in the immediate window the result.
Debug.Print Excel.WorksheetFunction.Sum(rng, Excel.WorksheetFunction.CountIf(rng, "ongoing") * 7)
'Reporting in the immediate window the result, this time using a With End With statement to make it more readable.
With Excel.WorksheetFunction
Debug.Print .Sum(rng, .CountIf(rng, "ongoing") * 7)
End With
End Sub

Copy and Paste VBA - Skip Blanks, No header

I want to move data from one workbook to another for mapping. I don't need the header and want to skip blanks.
I can get it to skip blanks but then it copies the columns beside it.
I can also get it to copy and paste only the column but it doesn't skip the blanks. So I just need a way for it to do column A only, no header, and keep it blank on the new sheet.
I've tried Skip blanks in the Paste special and it doesn't work.
Sub NewCopyandPaste()
Dim wsSource As Worksheet
Dim weTarget As Worksheet
Dim y As Range
Set wsSource = Workbooks("Book1.csv").Worksheets("Book1")
Set weTarget = Workbooks("Book2.csv").Worksheets("Book2")
With wsSource
Set y = .Range(.Range("A2"), .Range("A2").End(xlDown))
y.SpecialCells(xlCellTypeConstants).Copy
End With
weTarget.Range("C2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Your issue is that
Set y = .Range(.Range("A2"), .Range("A2").End(xlDown))
sets y to the range from A2 down to the cell above first blank cell Note 1
hange that to use End(XlUp) from the bottom of the sheet
Set y = .Range(.Range("A2"), .Cells(.Rows.Count, 1).End(xlUp)) ' .Range("A2").End(xlDown))
Note 1. stritcly speaking, what is copied will depend on if cell A3 is blank.
I think the problem might be with your range creation. Try:
Dim wsSource As Worksheet
Dim weTarget As Worksheet
Dim y As Range
Set wsSource = Workbooks("Book1.csv").Worksheets("Book1")
Set weTarget = Workbooks("Book2.csv").Worksheets("Book2")
With wsSource
Set y = Range("A2").End(xlDown)
y.SpecialCells(xlCellTypeConstants).Copy
End With
weTarget.Range("C2").PasteSpecial xlPasteValues
Application.CutCopyMode = False

Hide rows based on cells contaning all zeroes or no values

I have a dataset where every row is a General Ledger (GL) account and in each column there is the value for the relevant period.
I would like to hide all GL accounts (rows) where no values (or zero values) are included for all periods (columns).
The code below seems to work for the "No values".
How do I hide all the rows with only zeroes (or all rows with zeroes or "no values"?
If one period has an amount, the row shouldn't be hidden.
Sub hide()
Dim c As Range
For Each c In Range("A1:F6")
If c.Value = "" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next c
End Sub
Furthermore once any amounts change in a row this code should also make the unhidden rows reappear. At this moment it hides a row that has no value, but once this changes, the hidden row doesn't reappear anymore.
See code below if you want to test for both all blanks or all zeros and hide row if either present. Starts with an unhide of all rows.
Sub hide()
Dim wb As Workbook
Dim ws As Worksheet
Dim c As Range
Dim targetRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet5")
Set targetRange = ws.Range("A1:F6")
targetRange.EntireRow.Hidden = False
For Each c In targetRange.Rows
If (WorksheetFunction.CountIf(c,"<>0") - WorksheetFunction.CountIf(c,"") = 0) And (WorksheetFunction.CountA(c) - WorksheetFunction.Count(c) = 0) Then
c.EntireRow.Hidden = True
End If
Next c
End Sub
You have to check every row completely before deciding if to hide it or not. Currently, the last cell of every row decided if a row is hidden.
Give the following code a try. It sets a range to all cells of a row and uses the function CountA to count number of cells that are not empty.
Sub hide()
Dim ws As Worksheet, row As Long
Set ws = ActiveSheet
With ws
For row = 1 To 6
Dim myRange As Range
Set myRange = .Range(.Cells(row, 1), .Cells(row, 6))
.Rows(row).EntireRow.Hidden = (Application.WorksheetFunction.CountA(myRange) = 0)
Next row
End With
End Sub

How can I un-hide hidden rows in a table in Excel?

I made a code to hide some rows using .hidden = True that I don't need to show to make some checks, but I after that I want to show all the data again, so I made this code:
Sub show_hidden_cells()
Dim line As Range
Dim rng As Range
Set rng = Range("Tb_Data[Date]")
For Each line In rng
If line.SpecialCells(xlCellTypeVisible) = False Then
line.EntireRow.Hidden = False
End If
Next line
End Sub
My data has 50.000 rows and my computer it's not really fast, so I don't want to check every cell if is visible, instead of that I want to select only the hidden cells.
EDIT.
Thanks to #Rory and #Flephal who helped me my code now is:
Sub show_hidden_cells()
Dim rng As Range
Set rng = Range("Tb_Data[Date]")
rng.EntireRow.Hidden = False
End Sub

Resources