Why does my subtotal code counts 1 when there's no values? - excel

so I'm running a code that filters my data and then takes the count of the data and inserts it into an excel file. I'm using this code to count the data:
lastrow = Cells(Rows.count, "A").End(xlUp).Row
count = Application.WorksheetFunction.Subtotal(3, Range("A2:A" & lastrow))
For some reason, when I've filtered my data so that no value is visible aside from my top row, which is located in row 1, It gives me a value of 1 rather than 0. Any ideas why it's doing so?
Thanks

This is actually a funny thing I, too, discovered a while ago. What's happening is that, with a blank (or just filtered to appear blank) column A, lastrow = 1, which means the range you're Subtotaling is Range("A2:A1").
For example, test:
Debug.Print Application.WorksheetFunction.Subtotal(3, Range("A2:A1"))

Related

How to add serial number in "different size merged cells" which are positioned "alternatively" in excel using VBA?

As shown in image, I am trying to fill numbers in increasing order in alternate merged cells, which are different in size. So, I can't use autofill function of excel. But I want a macro so I can do it every time just hitting button once.
Note that I want numbers till the used range only.
I tried a lot to do it my self, but I am stuck now...Plz help the beginner, it's my third day in VBA.
This should do what you are looking for.
It will ignore non merged cells, I didn't see any in your screenshot that needed a number and were not merged so that shouldn't be an issue.
It uses column B to figure out the last row of your data.
Dim i As Long
Dim lr As Long
Dim counter As Long
counter = 1
With Sheet1 'Change to whatever your sheets code name is
lr = .Cells(.Rows.Count, 2).End(xlUp).Row 'If you want to use something other than column B, change the 2 to the right column index
For i = 2 To lr
If .Cells(i, 1).MergeCells = True Then
If .Cells(i, 1).MergeArea.Item(1).Address = .Cells(i, 1).Address Then
.Cells(i, 1) = counter
counter = counter + 1
End If
End If
Next i
End With

Replicating values

Need a little help here.
In the "Data" Tab I want to copy values in column "c2:c1000" and paste in column "a1" of another Tab.
This is what i have so far,
Dim x As Long
Dim lastRow As Long
lastRow = Worksheet("Data").Cells(3, Columns.Count).End(xlUp).Column
For x = 1 To lastRow
If Worksheets("Sheet2").Cells(2, "A") = "" Then
Worksheets("Data").Range("c2:c1000").Copy Destination:=Worksheets("Sheet2").Range(1, "A")
Sheets("Sheet2").Range("A1").Value = Format(Now, "mm/dd/yyyy HH:mm:ss")
Else
Worksheets("Data").Range("c2:c1000").Copy Destination:=Worksheets("Sheet2").Cells(2,
Columns.Count).End(xlToLeft).Offset(, 1)
'Sheets("Sheet2").Range("A1").Value = Format(Now, "mm/dd/yyyy HH:mm:ss") --> can't figure how to increment this as this will need to be on the subsequent empty column
End If
Next
End Sub
Your help will be greatly appreciated!
Thank you.
Pasting values first into range A1 and down and then next time to cell B1 and so on, leaves no space for the timestamp to A1, B1 etc. So, I assume that you would like to paste the random values to row 2. So cells A1, B1, ... are left for the timestamp.
Inside the With statements we can refer to properties of the wsAudit so we can replace the "Worksheets("Audit")." reference with just "."
The column.count expression just checks the amount of columns in the worksheet.
The expression .Cells(2, Columns.Count) just points to last cell in the row 2.
The .End(xlToLeft).Column then looks from this column to left and is supposed to find the last not empty cell on this row. It's basically the same idea that in Excel's sheet you would go to cell XDF2 and hit CTRL+Arrow Left from keyboard.
But instead of activating the cell we just want to get the columns index number and then add 1 (the new column) and save it into variable. Now the new column is known.
The expression Range(.Cells(2, newColAudit), .Cells(1000, newColAudit)).Value is really the same as e.g. Range("B2:B1000"), but with this we can use the row and column index numbers instead. This is useful as the column number varies.
And as Samuel pointed out the copy paste operation can be avoided by setting the areas equal.
Dim wsAudit As Worksheet
Dim newColAudit As Long
Set wsAudit = Worksheets("Audit")
With wsAudit
newColAudit = .Cells(2, Columns.Count).End(xlToLeft).Column + 1
Range(.Cells(2, newColAudit), .Cells(1000, newColAudit)).Value = Worksheets("Data").Range("C2:C1000").Value
.Cells(1, newColAudit).Value = Format(Now, "mm/dd/yyyy HH:mm:ss")
End With
Much like your LastRow* variable for your source sheet, create a LastColumn variable for your destination sheet, which will find the last used column the same way you are finding your last used row.
Like so:
Dim LastColumn As Long
LastColumn = Sheets("Audit").Cells(1, Columns.Count).End(xlToLeft).Column
Then use the variable like so:
Destination:= Worksheets("Audit").Cells(1, LastColumn)
It seems that your code contradicts your question too, in your question you explained the data will be written to the Audit sheet in row 1, using the next column each time but your code looks for values in row 2 in your If statement:
If Worksheets("Audit").Cells(2, "A") = "" Then is the same as If Worksheets("Audit").Range("A2") = "" Then.
If you mean to check the first row, change the 2 to 1.
To help improve your codes efficiency:
(Also see the link to 'how to avoid select' in that question):
You can achieve 'copy/paste' without actually using the 'copy' and 'paste' methods by assigning the value of one range to the other, as example, like so:
Worksheets("Audit").Cells(1, LastColumn).Resize(999, 1) = Worksheets("Data").Range("c2:c1000").Value
Note: Change the Resize Property rows to suit the source range (in this case you are wanting to move values from C2:C1000).
*The LastRow variable is a bit confusing, as it is looking for the last used column in row 3.
If it's meant to find a column, consider renaming it to avoid confusion later on in debugging.
If it's meant to find the last row, try like this:
LastRow = Worksheet("Data").Cells(Rows.Count, 1).End(xlUp).Row

How to program macro to compare column in two Excel worksheets and add matches to third sheet

I have looked and attempted to fit whatever code I have run across on this site (since this morning) into a working application with non-functional or poor results.
I need to be able to match if id numbers in the "A" column of sheet 1 match in the respective "A" column of sheet 2, and just paste the matches to column "A" in sheet 3. The column range in sheet 1 is not the same range as sheet 2, and sheet 3 needs to have the matches pasted in each row as they are found (i.e. no blank rows in between). I do not need to do anything about numbers that do not match.
I have attempted to modify code from a different thread; it catches some matches, but not all. I guess the ranges are incorrect but I don't really know how to code that in VBA. Plus there are spaces in the column where some of the other duplicates should be posted. The "if found is nothing then x = 0" is unnecessary (and may even be causing the aforementioned error), but if I take that out I get a type mismatch error.
Sub matchPAs()
Dim x, i, total, fRow As Integer
Dim found As Range
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To total
match1 = Worksheets(1).Range("A" & i).Value
Set found = Sheets(2).Columns("A:A").Find(what:=match1)
If found Is Nothing Then
x = 0
Else
fRow = Sheets(2).Columns("A:A").Find(what:=match1).Row
Worksheets(3).Range("A" & i).Value = Worksheets(1).Range("A" & fRow).Value
End If
Next i
End Sub

Delete specific rows in Excel sheet

I have a table with multiple columns. I would like to delete specific rows within the table. The logic to delete is the following:
If in column B one cell contains a specific value, let's stick to "example" for this case, I would like to delete the following two rows after the row(s) which matched the criteria.
It is important to note that the criteria might appear several times within the table and that the table might have different lengths.
My idea was the following:
1. Identify all rows which contain "example" in column B
2. Store the row numbers in a variable
3. Go through the variable and create a new one which has twice the length of the first one and write the two following rows into the 2nd variable
4. Use the 2nd variable to delete the rows with that numbers.
Unfortunately, I am totally new to VBA and was not able to code it. I also tried to copy code together but I couldn't find a solution for my specific topic.
This is a very slight mod to your approach
starting from the bottom of column B, work upwards.
if we encounter "example", delete the two rows below
So if row#7 contains "example", delete row#7 and row#8
Before:
The code:
Sub RowKiller()
Dim N As Long, i As Long, t As String
t = "example"
N = Cells(Rows.Count, "B").End(xlUp).Row
For i = N To 1 Step -1
If Cells(i, "B") = t Then
Range(Cells(i + 1, "B"), Cells(i + 2, "B")).EntireRow.Delete
End If
Next i
End Sub
and after:
I think, instead, the best way to handle this is:
Loop through all of the populated rows from the last to the first. (this insures we don't pull the rug out from under us when deleting rows).
If "Example" is found in column B of that row, delete the two rows after it (we've already traversed those rows so deleting shouldn't be any big deal
Thats it.
Sub deleteRows()
Dim lastRow as Long
'get the last row
lastRow = Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row
'Now work backwards
Dim i As Long
For i = lastRow to 1 Step -1 'change that 1 to whatever your first row is
If Sheet1.Cells(i, 2).value = "Example" Then
Sheet1.Rows(i + 1 & ":" & i + 2).Delete
End If
Next i
End Sub
I haven't tested that, but it looks right. You may have to tweak some things in there, but it will definitely get you in the ballpark.

How to get the row count in EXCEL VBA

I am developing a dashboard in excel. And I am looking for calculating row count. (How many records are present) ..
Since there are some blank cells I thought to go from bottom to up. I use the following
Range("A1048576").Select
Selection.End(xlUp).Select
After this execution the active cell is at A113 which means the row count is 113.
My question is how to get this number 113 from the active cell?
You can use this:
Dim lastrow as Long
lastrow = Cells(Rows.Count,"A").End(xlUp).Row
lastrow will contain number of last empty row in column A, in your case 113
Here is what I usually use for that:
lastrow = WorksheetFunction.CountA(Columns("A:A"))
This will return the number of non-empty cells in Column "A" which is what I think you're after. Hope this helps.
The best way to get the count of rows/records (in most cases) is to use .UsedRange.Rows.Count. You can assign the return value to a variable like this:
lastRow = Sheets(1).UsedRange.Rows.Count
If you use a function that includes a column (such as column A) as shown in other examples, that will only get you the count of rows in that column, which may or may not be what you're going for. One caveat: if you have formatted rows below your last row with a value then it will return that row number.
If there is a slight chance that the last row of the worksheet is not empty, you should add an IsEmpty() check to #simoco 's solution. Therefore; following is a function that returns the last used row and check if the last row of the worksheet is empty:
Function lastRow(WS As Worksheet, iColumn As String) As Long
If Not IsEmpty(WS.Range(iColumn & WS.Rows.Count)) Then
lastRow = WS.Rows.Count
Else
lastRow = WS.Range(iColumn & WS.Rows.Count).End(xlUp).Row
End If
End Function

Resources