Count the values for a string if the rows are hidden - excel

I want to count where:
column A contains "a"
the rows containing "a" are hidden.
For example:
If rows 2 & 5 (containing "a") are hidden the output should be 2 (excluding the visible "a" in row 3).

Using a healthy portion from here
=COUNTIF(A1:A5,"a")-SUMPRODUCT(--($A$1:$A$5="a"),SUBTOTAL(103,OFFSET(A1,ROW($A$1:$A$5)-ROW(A1),0)))

This will only count the hidden rows.
Sub SumInvisible()
For i = 1 To 10
If Range("A" & i).EntireRow.Hidden = True Then
If Range("A" & i).value = "a" then
Var = Var + 1
End if
End If
Next i
Range("B" & i).Value = Var
End Sub

In VBA, I have a custom function as follows:
Public Function IsCellHidden(vRange As Range) As Boolean
'Check if a cell is hidden
Dim vHidden As Boolean
If vRange.Rows(1).Hidden Or vRange.Columns(1).Hidden Then vHidden = True
IsCellHidden = vHidden
End Function
Then I put this into column B on the corresponding cell in column A, so B1 would for instance be =IsCellHidden(A1) and then in C6 I would have =COUNTIFS(A1:A10,"A",B1:B10,TRUE)

Easiest way:
1) Add a helper column
=SUBTOTAL(103, A2) next to column A, for A3, it becomes (103,A3) and so on
2) Then use
=COUNTIFS(A2:A5,"a",B2:B5,"=0")

Related

If the Cell value is blank then copy the cell value from another 2 cells using concatenate

I wrote the below VB Code but its not working, In the excel file
if Column BS = Blank then Take the Value from Column T and O, combine with _(underscore) and populate it in Column BS, If Column BS is not blank then no action required.
Similarly if Column BT =Blank then Take the Value from Column T and O, combine with _(underscore) and populate it in Column BT, If Column BT is not blank then no action required.
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
For Each rw In UsedRange.Rows
If rw.Columns("BS") = "" And rw.Columns("BT") = "" Then
rw.Columns("BS") = rw.Columns("T") & " _" & rw.Columns("O")
rw.Columns("BT") = rw.Columns("T") & " _" & rw.Columns("O")
End If
Next rw
End Sub

Vlookup In vba Code between two sheets is returning no values?

i have two worksheets, one is called sheet1, and the second is sheet2. what i want to do is take values from sheet1 and vlookup the values in sheet2, if the value exists in sheet2, return the 14 columns of sheet2 value.
i have a for loop that goes through all the values in sheet1, columns E and a vlookup function. but for some reason the vlookup is not returning a value even though the values are present in sheet2. here is my code.
Set myRang = shGL.Range("A13").CurrentRegion
For k = 1 To EndRowReport
If shReport.Range("D" & k).Value = "Bank Transaction Account" Then
On Error Resume Next
shReport.Range("F" & r).Offset(1, 0).Value = Application.WorksheetFunction.VLookup(shReport.Range("E" & r + 1).Value, myRang, 14, False)
totalAmount = totalAmount + shReport.Range("C" & r).Offset(1, 0).Value
totalAmountAbs = totalAmountAbs + shReport.Range("D" & r).Offset(1, 0).Value
r = r + 1
End If
Next k
any kind of help or suggestion is highly appreciated!

Remove row if value in one cell of the row is duplicated in the same column in the next row

I have a data sheet where column B is a list of data types. The only value I care about is if the value is SIS.
If row 2 has the value SIS in column B and row 3 has the value SIS in column B, then delete row 2. If row 3 contained instead a value of Topic, then keep row 2, ignore row 3, and look at row 4.
The attached image shows the sample data with a column called VBA Instructions. Any help is appreciated.
I think the below could work, this is testing if the cell in front has the same value as the current cell, if so then it would delete it. I would copy the dataset into another sheet before running a macro which deletes rows. The only problem with the below code is if you have two "Topic" cells right next to each other, for example if "Topic" tags were in rows 3 and 4 then row 3 would be deleted.
Sub test()
Columns("A:A").Insert Shift:=xlToRight
last = Cells(Rows.Count, 3).End(xlUp).Row
For Each Cell In Range("C2:C" & last)
If (Cell = Cells(Cell.Row + 1, 3)) = True Then
Range("A" & Cell.Row).Value = True
Else:
Range("A" & Cell.Row).Value = False
End If
Next Cell
For x = 2 To last:
If Cells(x, 1) = True Then
Rows(x).Delete
x = x - 1
End If
Next x
Columns("A:A").Delete
End Sub

Concatenate path and filename recursively

I have an output exported to Excel which lists paths and filenames.
The paths and filenames are on separate rows however. If the path is consistent the filename is simply listed on the next row. Then the next path is on the next line followed by filenames ect.
C:\
file1.doc
C:\Windows\
file2.doc
file3.doc
file4.doc
C:\Windows\Folder\
file5.doc
I need to concatenate all the paths with the filenames. All paths begin with c:\ (or other drive letters which can be defined). For the example above the following output is required:
C:\file1.doc
C:\Windows\file2.doc
C:\Windows\file3.doc
C:\Windows\file4.doc
C:\Windows\Folder\file2.doc
Happy to have white spaces as these can be filtered out in Excel.
Thanks,
Jono
VBA approach:
Sub test()
Dim ws As Worksheet
Dim iRow As Long
Dim i As Integer
Dim strFirstValue, strScndValue, strNewValue, strValue As String
Dim startFlag, endFlag As Boolean
Set ws = Sheets(1)
iRow = ws.Range("A1048576").End(xlUp).Row
strFirstValue = ws.Range("A2:A2")
strFirstValue = "": strScndValue = ""
startFlag = False
strFirstValue = ws.Range("A2:A2")
For i = 2 To iRow 'Assuming you have header, otherwise change 2 to 1
If endFlag Then
strFirstValue = ws.Range("A" & i & ":A" & i)
End If
strValue = strFirstValue
strScndValue = ws.Range("A" & i + 1 & ":A" & i + 1)
If InStr(strValue, ":") > 0 Then
startFlag = True
If Not strScndValue = "" Then
If Not InStr(strScndValue, ":") > 0 Then
strNewValue = strFirstValue & strScndValue
ws.Range("B" & i + 1 & ":B" & i + 1) = strNewValue
endFlag = False
Else
endFlag = True
End If
End If
End If
Next i
'To remove the row with drive info
For i = 2 To iRow
strValue = ws.Range("B" & i & ":B" & i)
If strValue = "" Then
ws.Range("B" & i & ":B" & i).EntireRow.Delete
End If
Next i
Set ws = Nothing
End Sub
Before:
After:
With data in column A, this macro will put the results in column B:
Sub dural()
Dim s As String, J As Long, r As Range
J = 1
For Each r In Intersect(ActiveSheet.UsedRange, Range("A:A"))
s = r.Text
If s = "" Then Exit Sub
If Right(s, 1) = "\" Then
pref = s
Else
Cells(J, 2).Value = pref & s
J = J + 1
End If
Next r
End Sub
Non-VBA solution, which will require some helper columns:
Assuming your data is in column A, and you don't care about sorting the results / having blanks within the results:
Put this in cell B2 and copy down [I recommend you have a header row for row 1]
=if(mid(A2,2,2)=":/",A2,B1)
This puts the new file path in column B, and if it's not a new file path (doesn't start with "x:/"), it takes the file path from column the previous cell.
In cell C2, copied down:
=if(mid(A2,2,2)=":/","",B2&A2)
This checks if the line you're on is a file path or a file name. If it's a file name, it adds the file name to the file path and displays as a single string. If it's a filepath, it returns a blank.
Alternatively you could save a tiny bit of processing time by using columns B-D instead of B-C. Some calculation is wasted here because we are doing the same check ("does cell A2 include ':/'?") twice, so excel needs to calculate it twice. Like so:
Put this in Cell B2:
=mid(a2,2,2)=":/"
Returns TRUE if the cell in column A is a filepath; returns FALSE if the cell in column A is a filename. Then put this in cell C2 and copy down:
=if(B2,A2,B1)
Works same as above, but uses the test already defined in cell B2. Put this in cell D2 and copy down:
=if(B2,"",B2&A2)
If you do want to sort your results column, there's just 2 extra steps (this is kind of unnecessary, but if you want to present / print your data in some format, you will need to either do this or manually copy + paste values):
Add an extra column to the right of the final column from my above response. Here, you want to check to see whether your current row is a new filepath + filename, or if it is blank (meaning column A was a filepath). I will assume you used my second option above, using columns B-D.
In column E, starting at E2 and copied down:
=if(B2,B1,B1+1)
If B2 is TRUE, the row is a filepath, and doesn't create a new filename + filepath. Therefore, we can keep the last counter. Otherwise, add a new counter.
In column F, starting at F2 and copied down:
=if(row()-1>max(E:E),"",index(D:D,match(row()-1,E:E,0)))
This looks at your results column, column D, which is unsorted and contains blanks. If the current row number in column F (minus 1 for the header row) is no bigger than the biggest counter in column D, it returns the matching item for that row number from column D.
Hope this helps you in similar situations in the future.

Excel Macro - pull cell function until the end of the table

I will use an example to illustrate my question:
I have many tables which their lines quantity is different.
I want to pull down the function until the end of the table.
For example:
A B
1 =1*2 // <- this is the function that I want to pull
2
3
4
The output should be:
A B
1 =1*2
2 =2*2
3 =3*2
4 =4*2
It is important that the pull length is determined by the last cell at column A (in this case it is 4)
Please also note that the function may be changed either, this should work for any function.
Thank you,
Doron
Here is an example of a macro that will autofill the value from cell B1 to the end of the column to the left of it (in this case column A).
Sub AutoFill()
Dim FillFrom As Range
Set FillFrom = ActiveSheet.Range("B1")
FillFrom.AutoFill Destination:=Range(FillFrom.Address, FillFrom.Offset(0, -1).End(xlDown).Offset(0, 1).Address)
End Sub
Try This:
Public Sub DoWhatIWantYouToDo()
Dim lr As Integer, i As Integer
lr = Sheets("Sheet1").UsedRange.Rows.Count
For i = 2 To lr
Sheets("Sheet1").Range("B" & i).Formula = "=" & " A" & i & "*2"
Next
End Sub

Resources