I apologize for the poor question name, if anybody has better suggestions at the end of this, let me know.
I have a range, say A1:M4 with the months in row 1 and a total for each row in column M. For each cell, there are values except for the months that have not yet happened. So if in November, everything up to November is filled in and December, column L, is empty and column M has the total from Jan to Nov (col A to col L). Another example, if in May, Jan, Feb, March, April and May and filled in, but not the other months, and column M has the total.
I want to create code that copies all the values up to the current month including the total for each row and paste into a powerpoint (I know how to copy and paste into a ppt). How do I copy this?
Current code:
rng=Range("a1",Range("a1").End(xldown).End(xlright))
Copies the entire range. How do I delete the columns without values in it. I thought of creating a count to count the values from column A to L, but having trouble incorporating how to remove the difference between total columns and filled in columns
This procedure can help you to find empty columns.
Sub FindBlankColumns()
'Step1: Declare your variables.
Dim MyRange As Range
Dim iCounter As Long
'Step 2: Define the target Range. Change to fit your needs
Set MyRange = ActiveSheet.UsedRange
'Step 3: Start reverse looping through the range.
'You can modify this according to your needs.
For iCounter = MyRange.Columns.Count To 1 Step -1
'Step 4: If entire column is empty then do some stuff.
If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
' Insert your code here to do what you want with empty column.
End If
Next
End Sub
Related
I need to remove duplicates from an Excel worksheet based on the values in 2 columns while taking case into account.
In the example below, Rows 1 and 2 are duplicates (Row 2 should be removed). Row 3, 4, and 5 are unique.
Row
Column A
Column B
1
Abc
Def
2
Abc
Def
3
ABC
DEF
4
ABC
DeF
5
Abc
DeF
I've done this with other datasets using Data > Remove duplicates, but since it is case-insensitive, it won't work for this.
I also found this question, which is very similar, but only identifies duplicates based on 1 column.
(How to remove duplicates that are case SENSITIVE in Excel (for 100k records or more)?)
Try this code:
Sub SubRemoveDuplicates()
'Declarations.
Dim RngData As Range
Dim RngDataToBeCompared
Dim RngCell As Range
Dim RngRow As Range
'Settings.
Set RngData = Range("A1:C6")
Set RngDataToBeCompared = Range("B2:C6")
'Covering each row of the data to be compared.
For Each RngRow In RngDataToBeCompared.Rows
CP_Rerun_For:
'Covering each cell of the given row.
For Each RngCell In RngRow.Cells
'Checking if any cell is different from the one under it.
If RngCell.Value <> RngCell.Offset(1, 0).Value Then
'If said cell has been found, skip to the next row.
GoTo CP_Next_Row
End If
Next
'Checking if the range to be targeted is within RngData.
If Not Intersect(RngRow.Offset(1, 0).EntireRow, RngData) Is Nothing Then
'Deleting the row of duplicates.
Intersect(RngRow.Offset(1, 0).EntireRow, RngData).Delete (xlShiftUp)
'Rerunning this cycle for the given row in order to catch duplicates that comes in more than 2.
GoTo CP_Rerun_For
End If
CP_Next_Row:
Next
End Sub
Note: if you are going to cover an entire column with presumably many empty cells, the macro will cover (and eventually delete) all those empty cells too. The macro can be modified so it will stop when it encounters and empty row, or to dynamically determinate the appropriate range to be covered. Otherwise it might take more time than necessary.
I don't like using macros until it's last hope.
For your situation, I would suggest adding new columns, and with function
=lower(Column A), etc. get values of column A in lower case. Then I would add one more new column and do the same for Column B.
And after that, I would use Data/Remove Duplicates (converting range to Table format first). And then I would delete unnecessary columns which were added for converting everything to lowercase.
Use this frmula to manually delete. It combines two columns on one row and compares them with the column above.
=B2&C2=B1&C1
You can then edit or filter on Col D and delete.
sorry for being a total noob in excel!
I have two sheets, sheet 1 named "Stocks" and sheet 2 named "Stocks search".
In "Stocks" I have from A1 to B700 values. In A column I have the stocks symbols and in B column I have the stocks' issuers symbols, so every entry in A column is unique, yet there can be repeating entries in column B.
So in sheet "Stocks search", if I enter in A1 an issuer's symbol, I want for the formula to go search in sheet "Stocks" and fetch all stocks that this issuer has in new rows.
How can this be done in a formula? Thanks in advance!
This is a VBA solution to the question. IMHO, this is more appropriate than a formula (in this case). The formula approach is OK, but there are drawbacks - you have to remember the CSE rule, and then copy the formulas down the right number of rows (which you don't know in advance), etc, etc.
This code uses the same assumptions as the formula approach.
1 - sheets = Stocks and Stock report
2 - Data in Sheets, columns A and B (header in row 1)
3 - lookup code is on Stock report
4 - Output is on Stock report
One of the advantages is that if new data is added to the Stocks sheet (i.e. the bottom row > 700), the vba automatically adjusts.
The code is self-documented. But the essence is that it creates an autofilter on "Stocks" using the lookup value as the criterion; copies the rows that meet the criteria; and pastes the result to an output range on "Stock reports". The output range is cleared before the copy/paste takes place so that there are no left-overs from any previous lookup.
I think there's something to be said for creating a dropdown list for the lookup cell. No doubt that could be automated too by getting the codes from Column A, getting the unique values, and then apply them to the lookup cell. Just a thought;)
Sub so_52537740()
' CREDITS
'
' Refer: https://stackoverflow.com/questions/17531128/copy-paste-calculate-visible-cells-from-one-column-of-a-filtered-table
' Date: 8 July 2013
' Submitted by: Jon Crowell (https://stackoverflow.com/users/138938/jon-crowell)
Dim src As Worksheet, tgt As Worksheet
Dim filterRange As Range, copyRange As Range
Dim lastRow As Long
Dim stocks As String, stockreport As String
' set values for sheet names
stocks = "Stocks"
stockreport = "Stock report"
' set values for Sheet variables
Set src = ThisWorkbook.Sheets(stocks)
Set tgt = ThisWorkbook.Sheets(stockreport)
' clear the exist target data
tgt.Range("A4:B" & Rows.Count).ClearContents
' turn off any autofilters that are already set
If src.AutoFilterMode Then src.AutoFilter.ShowAllData
' find the last row in the Stocks sheet with data in column A
lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
' the range that we are auto-filtering (all columns)
Set filterRange = src.Range("A1:B" & lastRow)
' the range we want to copy (only columns we want to copy)
' in this case we are copying both columns A and B
' we set the range to start in row 2 to prevent copying the header
Set copyRange = src.Range("A2:B" & lastRow)
' filter range based on column A being equal the the value in Cell A1 of the stockreport
' consider making this a dropdown list so that there are no errors
filterRange.AutoFilter field:=1, Criteria1:=Format(Sheets(stockreport).Range("a1").Value)
' copy the visible cells to our target range
' note that you can easily find the last populated row on this sheet
' if you don't want to over-write your previous results
copyRange.SpecialCells(xlCellTypeVisible).copy tgt.Range("A4")
' turn off any autofilters that are already set
If src.AutoFilterMode Then src.AutoFilter.ShowAllData
End Sub
Giving due credit: There is, as they say, nothing new under the sun. I have based this answer on an excellent piece of work by Jon Crowell on a question in StackOverflow "Copy/Paste/Calculate Visible Cells from One Column of a Filtered Table" in July 2013. Just goes to show what a bit of Googling and perseverance can achieve.
I believe I have an answer for you.
Try
=IFERROR(INDEX('Stocks Search'!$A$1:$A$700,SMALL(IF('Stocks Search'!$B$1:$B$700=$A$1,ROW('Stocks Search'!$A$1:$A$700)-MIN(ROW('Stocks Search'!$A$1:$A$700))+1),COLUMNS($A$1:A1))),"")
This is a CSE formula. What that means is once you enter it into cell B1, you will need to press Control+Shift+Enter. Once you do this, these brackets will appear around your formula {}
Click the fill button in the bottom right of the cell and drag the formula to the right (you will need to do this for as many cells as it is possible for answers). So if Company A has 40 possible answers, you will need to have this formula go at least 40 cells to the right.
The application of CSE formulas can be tricky. Essentially you need to go to the end of the formula in the formula bar, and then use Control+Shift+Enter.
I hope this helps.
I am trying to expand my annual planning document to give me more information. Currently however I am stuck.
My current Sheet is laid out as follows:
I need to create a second sheet that concatenates the client name with each column heading there is a "yes" value in, as its own separate line.
Example 1 in the new sheet would become:
Example 1 - Annuals
Example 1 - Xero Fee
I had tried a copy and paste Macro, based on a quantity column counting the rows that include text. This gave the desired amount of client names in a new sheet but I was unable to work out how to include the "concatenate" part of this problem to it.
Public Sub CopyData()
' This routing will copy rows based on the quantity to a new sheet.
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer
' Set this for the range where the Quantity column exists. This works only if there are no empty cells
Set rngQuantityCells = Range("G1", Range("G1").End(xlDown))
For Each rngSinglecell In rngQuantityCells
' Check if this cell actually contains a number
If IsNumeric(rngSinglecell.Value) Then
' Check if the number is greater than 0
If rngSinglecell.Value > 0 Then
' Copy this row as many times as .value
For intCount = 1 To rngSinglecell.Value
' Copy the row into the next emtpy row in sheet2
Range(rngSinglecell.Address).EntireRow.Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
' The above line finds the next empty row.
Next
End If
End If
Next
End Sub
If your goal is to get from this:
...to this (or that):
...then what you want to do is called an Unpivot.
How to Unpivot "crosstab-style" data:
You can find the steps written out on a question I answered a week ago, and there's a more detailed explanation and steps over here.
Let me know if you have any questions!
Yeah, maybe I used this as an excuse to practice using Screen2Gif, but it sure helps demo a point! :-)
🇨🇦
I have the following Excel Table:
Create Date Last Active Date Age
4/12/2017 5:54 4/17/2020 8:54 5 Days
4/19/2017 7:43 #N/A
4/12/2017 20:43 #N/A
4/1/2017 23:20 4/3/2017 6:54 10 Days
4/15/2017 22:20 #N/A
What I want to do is to filter the Age Column by #N/A, and then copy each Last Active Date value to the same row in Create Date. Seems easy enough, but I keep running into issues. I am using the SpecialCells(xlCellTypeVisible) property to then do a for each on each Area in there(non-contiguous rows), but when I go to copy the rows, it either copies the rows starting at Row 1 of the Create Date column, meaning the values get all out of whack OR it throws an error saying the ranges don't match. Here is the code I have so far that I pulled from another page that talked about how to do this, but it doesn't seem to work for me.
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8, Criteria1:= _
"#N/A"
Dim lngrow As Long
Dim FinalDest As Range
Dim Rng As Range
lngrow = Sheets("Sheet1").UsedRange.Rows.Count
Set FinalDest = Sheets("Sheet1").Range("C2:C" & lngrow)
Range("F2:F" & lngrow).Select
For Each Rng In Cells.SpecialCells(xlCellTypeVisible).Areas
Set FinalDest = FinalDest.Offset(Rng.Rows.Count)
Rng.Copy Destination:=FinalDest
Next Rng
Application.CutCopyMode = False
How can I accomplish this? I want to filter it by #N/A, and then for each filtered row that remains, copy the value in Last Active Date to Create Date, (which will always be blank for these rows) and make sure they get copied to the proper rows, ie if Row 3 is the first filtered row, the value gets copied to rows 3 instead of row 2.
You don't need to filter your table. You have two feasible solutions:
Solution 1 (without VBA):
Assuming that Create Date is at A1:
-Insert a new column between Create Date and Last Active Date.
-Insert this function to B2--> =IF(A2="",IFNA(D2,C2),A2)
-Copy paste B2 till end of your column
-Copy all B column and paste them as values.
-Delete Column A
Solution 2 (with VBA):
You can loop through each row and check if the Age cell is #N/A, if true(#N/A), then you do the copy/paste from Last Active Date cell to Create Date Cell.
Solution 1 is much faster and easy. If you are interested in VBA solution let us know.
Edit: You can always change the following code for different conditions but since you said --> Create date value (which will always be blank for these rows):
Dim i As Long
For i = 2 To Range("C1").End(xlDown).Row
If IsEmpty(Cells(i, 1).Value) Then
Cells(i, 1) = Cells(i, 2)
End If
Next i
my problem is:
I have a matrix in excel then a function (repeated N times) that extract from this matrix a random number.
This happen everytime i hit F9 or everytime something change in the sheet.
After this i have another cell with a sum of all then numbers extracted. I would like to keep track of the change of this last cell in a linear chart to see how it goes on.
Thank you so much in advance
Solution 1: formulas
if you want to repeat T times, just instead of only 1 series of N number create a matrix of N by T. Then ad 1 last row (or column) under (or next to) the N functions for all T which contains the sum.
Now relate a chart to this last row (or column).
Solution 2: VBA
Create a piece of code that loops T times and copies the value of the cell with the sum into the next empty cell in a designate part of your excel workbook.
Sub aaa
dim i as Long
dim T as Long
T = 1000
For i = 1 to T
Range("rSum").Copy
Cells(rows.count,1).offset(xlUp)).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Next i
End Sub