I have a table that I want to completely hide or hide/show rows within the table, depending on whether a cell value is 0 or above.
It looks for a value of 0 within cell D26; if 0 it hides rows 24-51, if not 0 it hides/shows rows depending on whether there is a value in the C column between rows 34 and 49.
The macro below is too slow to be a viable option. Can anyone suggest an alternative way of doing this, that might work in a few seconds rather than a few minutes? I think it's because I'm running the For/If/Else loop.
Sub HideManifolds()
'
' HideManifolds Macro
'
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
ChkCol = 3
Manifold1BeginTableRow = 34
Manifold1EndTableRow = 49
Manifold1BeginRow = 24
Manifold1EndRow = 51
For Manifold1RowCnt = Manifold1BeginRow To Manifold1EndRow
If Cells(26, 4).Value = 0 Then
Cells(Manifold1RowCnt, 1).EntireRow.Hidden = True
Else
For Manifold1TableRowCnt = Manifold1BeginTableRow To Manifold1EndTableRow
If Cells(Manifold1TableRowCnt, ChkCol).Value = 0 Then
Cells(Manifold1TableRowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(Manifold1TableRowCnt, ChkCol).EntireRow.Hidden = False
End If
Next Manifold1TableRowCnt
End If
Next Manifold1RowCnt
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
'
End Sub
I think you don't need this loop For Manifold1RowCnt = Manifold1BeginRow To Manifold1EndRow
code:
Sub HideManifolds()
'
' HideManifolds Macro
'
Dim hRng As Range, vRng As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
ChkCol = 3
Manifold1BeginTableRow = 34
Manifold1EndTableRow = 49
Manifold1BeginRow = 24
Manifold1EndRow = 51
If Cells(26, 4).Value = 0 Then
Rows(Manifold1BeginRow & ":" & Manifold1EndRow).Hidden = True
Else
For Manifold1TableRowCnt = Manifold1BeginTableRow To Manifold1EndTableRow
If Cells(Manifold1TableRowCnt, ChkCol).Value = 0 Then
If hRng Is Nothing Then
Set hRng = Cells(Manifold1TableRowCnt, ChkCol)
Else
Set hRng = Union(hRng, Cells(Manifold1TableRowCnt, ChkCol))
End If
Else
If vRng Is Nothing Then
Set vRng = Cells(Manifold1TableRowCnt, ChkCol)
Else
Set vRng = Union(vRng, Cells(Manifold1TableRowCnt, ChkCol))
End If
End If
Next Manifold1TableRowCnt
If Not hRng Is Nothing Then hRng.EntireRow.Hidden = True
If Not vRng Is Nothing Then vRng.EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
'
End Sub
Related
Sub Demo()
Application.ScreenUpdating = False
Dim r as Long
Dim C As Range
With ActiveDocument.Tables(1)
For r = 2 To .Rows.Count
With .Rows(r)
If .Cells.Count < 5 Then .Cells(2).Delete
If .Cells.Count > 4 Then .Cells(3).Delete
End WIth
Next
ActiveDocument.Tables(1).Cell(1,2).Delete
End With
Application.ScreenUpdating = True
End Sub
As I indicated in your other thread, the process has to be done at the cell level. Thus:
With .Rows(r)
If .Cells.Count < 3 Then
.Cells(2).Width = InchesToPoints(4)
.Cells(3).Width = InchesToPoints(2)
ElseIf .Cells.Count > 4 Then
.Cells(2).Width = InchesToPoints(4)
.Cells(3).Width = InchesToPoints(2)
.Cells(4).Width = InchesToPoints(3)
End If
End With
i am checking column P (starting from row 8)if it contains "Incomplete" then i want to remove the entire row , else do nothing , but it give me back run time error 6 OVERFLOW
This is my code :
Private Sub CommandButton3_Click()
Dim i As Integer
Dim Pvalue As String
i = 8
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
lastRow = 0
lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
MSG1 = MsgBox("are you sure you want to remove the Incomplete rows?", vbYesNo, "Microsoft Excel")
If MSG1 = vbYes Then
'clearing the body table
For i = 8 To lastRow
Pvalue = Range("P" & i).value
If Pvalue = "Incomplete" Then
Worksheets("Sheet4").Range("A" & i & ":" + "P" & i).ClearContents
Worksheets("Sheet4").Range("A" & i & ":" + "P" & i).Interior.ColorIndex = 0
Else
End If
Next
Else
End If
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
End Sub
thanks every body for supporting,
actually after reading about this error (run time error 6) one of its reasons that you are using a wrong data type for a variable,
i used
dim i as long instead of
dim i as integer
then it work fine.
thanks again everybody.
So i have the following macro
Private Sub Worksheet_Change(ByVal Target As Range)
BeginRow = 178
EndRow = 178
ChkCol = 8
For RowCnt = BeginRow To EndRow
If IsError(Sheet1.Cells(RowCnt, ChkCol).Value) Then
Sheet1.Cells(169, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(170, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(171, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(172, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(173, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(174, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(175, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(176, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(177, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(178, ChkCol).EntireRow.Hidden = True
Sheet1.Cells(179, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
For RowCnt = BeginRow To EndRow
If Not IsError(Sheet1.Cells(RowCnt, ChkCol).Value) Then
Sheet1.Cells(169, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(170, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(171, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(172, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(173, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(174, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(175, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(176, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(177, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(178, ChkCol).EntireRow.Hidden = False
Sheet1.Cells(179, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End Sub
You could probably do this a lot prettier, but i am quite new to VBA.
The formula H178 has the following input:
`=(H170+H171+H172+H173+H174+H175+H176+H177)/7`
However these cells H170, H171 etc. get their data from Sheet2
So i.e. when H170 is an error, H178 gives a #REF! which should automatically mean that the macro is runned, but it doesn't, unless if i double-click on the cell H178 and hit enter.
But if i i.e. change the cell H170 directly on sheet1, to =5/0 (which gives error) then the macro is runned.
What am i doing wrong?
You should place this code in the Worksheet_Calculate event in order to have it recalculate when the error is passed
I have the following basic script that merges cells with the same value in Column R
Sub MergeCells()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim rngMerge As Range, cell As Range
Set rngMerge = Range("R1:R1000")
MergeAgain:
For Each cell In rngMerge
If cell.Value = cell.Offset(1, 0).Value And IsEmpty(cell) = False Then
Range(cell, cell.Offset(1, 0)).Merge
GoTo MergeAgain
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
What I would like to do is repeat this in columns A:Q and S:T but, I would like these columns to be merged in the same merged cell ranges as column R, i.e. if R2:R23 is merged then A2:A23, B2:B23, C2:C23 etc. will also be merge.
Columns A:Q do not contain values, column S:T have values but, these will be the same values throughout the range.
Any ideas
Apols for the earlier edit - this now deals with more than one duplicate in col R.
Note that this approach will work on the current (active) sheet.
Sub MergeCells()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim cval As Variant
Dim currcell As Range
Dim mergeRowStart As Long, mergeRowEnd As Long, mergeCol As Long
mergeRowStart = 1
mergeRowEnd = 1000
mergeCol = 18 'Col R
For c = mergeRowStart To mergeRowEnd
Set currcell = Cells(c, mergeCol)
If currcell.Value = currcell.Offset(1, 0).Value And IsEmpty(currcell) = False Then
cval = currcell.Value
strow = currcell.Row
endrow = strow + 1
Do While cval = currcell.Offset(endrow - strow, 0).Value And Not IsEmpty(currcell)
endrow = endrow + 1
c = c + 1
Loop
If endrow > strow+1 Then
Call mergeOtherCells(strow, endrow)
End If
End If
Next c
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub mergeOtherCells(strw, enrw)
'Cols A to T
For col = 1 To 20
Range(Cells(strw, col), Cells(enrw, col)).Merge
Next col
End Sub
You can try the below code as well. It would require you to put a 'No' after the last line in column R (R1001) so as to end the while loop.
Sub Macro1()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
flag = False
k = 1
While ActiveSheet.Cells(k, 18).Value <> "No"
i = 1
j = 0
While i < 1000
rowid = k
If Cells(rowid, 18).Value = Cells(rowid + i, 18).Value Then
j = j + 1
flag = True
Else
i = 1000
End If
i = i + 1
Wend
If flag = True Then
x = 1
While x < 21
Range(Cells(rowid, x), Cells(rowid + j, x)).Merge
x = x + 1
Wend
flag = False
k = k + j
End If
k = k + 1
Wend
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
For some reason this macro freezes EXCEL when I set the for home = X to XX loop to more than 10 iterations.
This code downloads a webpage into excel, extracts cells that contain either 'overall' or 'carried' and copy them into another sheet in the same workbook.
Thank you
Sub Macro1()
'
' Macro1 Macro
'
'
Dim home As Integer
Dim Calc_sheet As Worksheet
Dim score_count As Integer
Dim inspection_count As Integer
Dim output_rows As Integer
Dim output_columns As Integer
Dim date_columns As Integer
'Counting variables
score_count = 3
inspection_count = 8
'Output rows and columns starting values
output_rows = 3
output_columns = 3
date_columns = 8
For home = 20 To 23
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.XXXXXXXX.org.uk/directory/" & Sheets("Output").Cells(home, 1), Destination:=Range("$A$1") _
)
'.CommandType = 0
.Name = "Homes"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
For x = 20 To 250
Select Case Left(Cells(x, 1), 7)
'Is it a score?
Case Is = "Overall"
Cells(x, 1).Copy
Sheets("Output").Select
Cells(output_rows, output_columns).Select
ActiveSheet.Paste
output_columns = output_columns + 1
'Is it a date?
Case Is = "Carried"
Cells(x, 1).Copy
Sheets("Output").Select
Cells(output_rows, date_columns).Select
ActiveSheet.Paste
date_columns = date_columns + 1
Case Else
End Select
Sheets("Calc_sheet").Activate
Cells(x, 1).Activate
Next x
'Clean sheet
ActiveSheet.Cells.Delete
'Go back to top
Range("A1").Select
'Reset column count
output_columns = 3
date_columns = 8
output_rows = output_rows + 1
Next home
End Sub
I updated the code, try it again!
Try replacing your inner-loop with this one :
Dim wsC As Worksheet
Dim wsO As Worksheet
Set wsC = Worksheets("Calc_sheet")
Set wsO = Worksheets("Output")
For x = 20 To 250
yourContent = wsC.Cells(x, 1)
yourCase = Left(yourContent, 7)
Select Case yourCase
'Is it a score?
Case Is = "Overall"
wsO.Cells(output_rows, output_columns) = yourContent
output_columns = output_columns + 1
'Is it a date?
Case Is = "Carried"
wsO.Cells(output_rows, date_columns) = yourContent
date_columns = date_columns + 1
Case Else
End Select
Next x