Using formula in a new column of a table - excel

I want to write a macro that will insert a new column into a table, enter a formula in the first cell after the header, and paste it to all cells in that column to the bottom of the table.
My issue is defining the bottom of the table. I can find the last row with data by using the following.
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
And the formula portion will always start in this column and works for the first cell:
Range("E2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]=R[-1]C[-1]"
Range("E2").Select
Here is the issue; ideally I'd want the range to start at "E1" (or Selection since its already there) and end at the variable # of rows the table ends at, NOT E10.
ActiveCell.Select
Selection.Copy
Range(Selection, "E10").Select
ActiveSheet.Paste
I've tried replacing "E10" with just the LastRow variable but I see that wont work since it's just a number. Any help would be appreciated!
(I've also tried this range, but since its a new column there's no way Excel knows where the bottom of the table is with ctrl shift down; maybe it can be tweaked/altered to work?):
Range(Selection, Selection.End(xlDown)).Select

Range("E2").Resize(LastRow - 1).FormulaR1C1 = "=RC[-1]=R[-1]C[-1]"
You don't have to Select - just change the FormulaR1C1 property of the whole range. The -1 part of the Resize argument is because you are in row 2. If you started in row 10, it would be LastRow - 9.

Related

Excel VBA to Select Rows Down That Include Blanks

I'm trying to create a macro to copy cells down an entire column of a table in hopes of acting as a 'refresh' in case those formulas were altered or replaced.
I have multiple tables in the same sheet so I can't select the table name because they constantly change.
What I'm thinking of is having a bottom row with a keyword that VBA can select down until they hit the keyword and select those cells to copy the formulas down.
The thing is that I have multiple tables and they would all have the bottom row of keywords.
When I recorded a macro, I have to Control+Shift+Down multiple times to account for missing rows which I imagine wouldn't always be the case. This is what scares me for this macro since sometimes a table would have no missing data so the xlDown function would select more data than it should.
Here is what I recorded:
Sub Macro9()
'
' Macro9 Macro
'
'
ActiveCell.Offset(3, 2).Range("A1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
End Sub
Here is an example of the column I am trying to restore formulas on:
I want the formula below "Total Price" to fill down until it hits the word "Total". Note that formulas are hidden if there is no data elsewhere in the sheet.
There are multiple tables on this sheet so this would have to work in different sections of te same sheet.
If you have actual Tables/Listobjects then you could do something like this:
Sub FillDownFormulas()
Dim lo As ListObject, col As Range
For Each lo In ActiveSheet.ListObjects
For Each col In lo.DataBodyRange.Columns
If col.Cells(1).HasFormula Then 'first cell in column has a formula?
col.Formula = col.Cells(1).Formula 'fill formula to rest of column
End If
Next col 'next column
Next lo 'next table
End Sub
One of the most simple ways (for people that usually don't use VBA) is to get the number of the last row in your table. You can do that by counting values in table or with your own code by using a column that is always filled, like:
last_row = Range("B2").end(xldown).row
With last_row value you can fill your formula in ranges, like:
Range("C2").value = 'Your Formula here
Range("C2").AutoFill Destination:=Range("C2:C" & last_row)
You can do that to every column that you want.

Google Sheets/Excel: Is there a way to pull information from different columns depending on the value of another column?

I'm trying to create a summary of data (responses) in a separate sheet that pulls a value from a calculated column based on the value in another column.
For example, my data output has Item Type in column C and columns D:L are item descriptions based on type. Every row (response) has an item type in column C (e.g., office supplies), then depending on that item type there is a description (e.g., pens) in the appropriate column (e.g., Office supplies item description). All other description cells in that row are blank.
What I want is to summarize the data from D:L into one column (e.g., Item description). In other words, I want to summarize all of the non-blank cells in D:L in one column.
I must add: I'm using Google Sheets with the data output from a Google Form. Every time a new response is added to the sheet, a new row is inserted below the previous row, so using reference cells (e.g., C2) doesn't seem to work.
My first time posting here so please let me know if I need to provide more information.
Example:
Response data
End goal/summary
You can use the below macro to achieve this result :
Sub Macro1()
'
' Macro1 Macro
'
Sheets("Response Data").Select
Range("C1").Select
Selection.End(xlDown).Select
LR = ActiveCell.Row
Sheets("Summary").Select
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Response Data'!R1C4:R" & LR & "C12,ROW(Summary!RC[-1]),MATCH(Summary!RC[-1] & "" item description"",'Response Data'!R1C4:R1C12,0))"
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range("B2", "B" & ActiveCell.Row).Select
Selection.FillDown
End Sub
What I was able to do is create a FILTER of the Google Forms output in a separate sheet (which created a copy of the responses that would NOT insert a new row for each entry), then use TEXTJOIN (referencing columns D:L in the new sheet) to get the info in one column on the summary sheet.
Thank you to BigBen for reminding me in the comments of the TEXTJOIN function :)

Unable to copy all data in column to another sheet - excel VBA

I am unable to copy all data in a column. I would only want to copy data that does not include the first row, (headers), and paste it to another sheet. My current code gives me a partial result as there are some blanks in between the data.
Please help me out as I am new to VBA. Thanks!
Below are two codes that I have tried. One is through the xldown method and the other is the lastrow. I found that using the lastrow, I am not even able to copy anything at all.
This is the xldown method, which gives me partial data (wsRawT and wsDetI are my defined worksheets):
wsRawT.Select
range("AI1").Select
ActiveCell.Offset(1, 0).range("A1").Select
range(Selection, Selection.End(xlDown)).Select
Selection.copy
wsDetI.Select
range("B1").Select
ActiveCell.Offset(1, -1).range("B1").Select
ActiveSheet.Paste
This is the lastrow method, which does not even allow me to copy anything:
Dim lastRowTD As Long
lastRowTD = wsRawT.Cells(Rows.Count, "A").End(xlUp).Row
wsRawT.range("AU2" & lastRowRD).copy
wsDetI.range("A2").PasteSpecial xlPasteValues
wsDetS.range("A2").PasteSpecial xlPasteValues
There are only small mistakes in your code, try this:
Dim lastRowTD As Long
lastRowTD = wsRawT.Cells(Rows.Count, "AU").End(xlUp).Row
wsRawT.Range("AU2:AU" & lastRowTD).Copy
wsDetI.Range("A2").PasteSpecial xlPasteValues
To explain: This code checks how large the dataset in column A of your sheet wsRawT is. Then it copys everything from cell A2 down to the last row with data. Then all the values are pasted to column A of sheet wsDetI. If you don't specifically need the values only, you could also use this simpler version of .Copy:
wsRawT.Range("AU2:AU" & lastRowTD).Copy wsDetI.Range("A2")

how to value to integer VBA

I’m trying to create VBA that allows me to copy the one of a cell and paste over until end of the row in the same column.
But I’m unable to figure out how this works, I have a total of 109 rows with 20 columns and in column “BD” that is an empty column and I want to put today date and fill up rest of the same column until end of row.
Please see the code at the bottom
Sub CopyInsert()
Range("BD2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("BD2").Select
Selection.Copy
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
ActiveSheet.Paste
End Sub
This code is working fine but I’m not getting the right result what these codes do is copy and paste over pass 109 rows mean is paste over up to around 2000 rows
My question is how can I copy and paste over until the end of the row like till row 109! If we have over 200 rows, how to copy and paste until end of the row, row 200 in one column! But not using (BD2:BD), I have tried (BD2:BD) and is paste over 109 rows is not stopped at the end of row.
many thanks.
So you need to find the lastRow based on the column where the actual data is...
Try something like this:
With worksheets("mySheet")
Dim lastRow as Long
lastRow = .Cells(.Rows.Count,1).End(xlUp).Row 'this uses column A for example
.Range("BD2:BD" & lastRow).Formula = "=TODAY()"
End With

Is there a VBA string to select copied cells that used to have a blank formula?

I have an excel file where everything is controlled via a macro. At one point I am moving files from one sheet to another where this data is stored as a backlog.
I then try to select blank cells and remove the row if true. But cells appear blank but are not.
So I am moving it from Sheet A to Sheet B. The data in Sheet A is being moved and pasted as values in Sheet B. The data being moved has two columns: Column A holds Item ID and column B holds a date when the item cease to exist (Cease Date).
In Sheet A, Cease Date is populated through a simple formula (=+IF(O5<>"";O5;N5)) where if there is no new Cease Date input, fetch from backlog.
Now, if there neither is no new Cease Date input nor is there any backlog, the cell is blank.
When the macro copy and paste (as values) the data from Sheet A into Sheet B, column B is populated by blank cells (as intended) but there is something invisible, for lack of better word. Almost like there would be a formatting or like when you can encounter hidden characters not seen other than by ANSI.
IF I select any of the empty cells and press delete, then run "Go to special..." and blanks, the cell gets selected by the function.
I'm using this line to remove blanks:
Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
This is the segment of my code that copy/paste and handle the Cease Date section:
Sheets("Dashboard").Select
Range("B3:Q400").Select
Selection.Copy
Sheets("CeaseDate").Select
Range("F1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("G:T").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("F1:G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("F:G").Select
Selection.Delete Shift:=xlToLeft
Columns("B").SpecialCells(xlBlanks).EntireRow.Delete 'Remove rows that does not contain a Cease Date <--- This does not work since it wont treat the blank cells as blank
'Range converting to date format
Columns("B:B").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("A:B").Select
ActiveSheet.Range("A:B").RemoveDuplicates Columns:=Array(1, 2), _
Header:=xlYes
Selection.End(xlUp).Select
So I found a solution to this, I cannot explain why the problem occurs but apparently it has been something occurring in Excel since at least version 2003.
Problem: When I copy cells and use paste-special-value, the cells that appeared blank will fail the test of =ISBLANK() and returning a FALSE value. Yet, there is nothing to copy from the cell or anything to mark. If I select the apparently blank cell and press delete or Backspace, =ISBLANK() will now return a TRUE value.
Solution:
I select the area with the apparently blank cells
Open Find/Replace function and leave the Find What: blank (no spaces or anything)
and then in the Replace With: type in a string or word that you
KNOW does not appear anywhere else in the spreadsheet.
Click Replace All
All apparently blank cells will be replaced with the word
Now take the word and Find/Replace it with nothing
The replaced cells will now be truly blank and pass the =ISBLANK() test
I originally found this (quite obscure) solution here
If you have a cell that contains a formula, but the Value of that Formula is equivalent to ="", then it will display as Blank, and the COUNTBLANK function in Excel (or WorksheetFunction.CountBlank function in VBA) will call it Blank, but SpecialCells(xlBlanks) will not - because the Range.Formula property is not blank.
Here is a Function to retrieve cells with a Blank Value in a range:
Private Function GetNullValues(ByVal Target As Range) As Range
Dim TestingArea As Range, TestingCell As Range
For Each TestingArea In Target.Areas 'Loop through Areas in Target
For Each TestingCell In TestingArea.Cells 'Loop through Cells in Area
If TestingCell.Value = "" Then 'If Cell looks Blank
If GetNullValues Is Nothing Then 'If first blank found
Set GetNullValues = TestingCell 'Start list
Else 'If not first blank
Set GetNullValues = Union(GetNullValues, TestingCell) 'Add to list
End If
End If
Next TestingCell, TestingArea 'This is the same as doing 2 Next lines
End Function
Use it like this: Set BlankCells = GetNullValues(Sheet1.Columns(2))
What you could try is also removing formatting. I have encountered this several times and it worked for me with this code. with this line all formats in blank cells are removed.
Columns("B").SpecialCells(xlBlanks).EntireRow.ClearFormats
what you could also try is adding the .clearformats function to every delete part.
so if you have
sheet1.column("B").clearcontent
you could add
sheet1.column("B").clearcontent
sheet1.column("B").clearformats

Resources