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
Related
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.
I am trying to create an excel macro which finds the last column of a sheet and then selects the entire column. However, this column will always be different- some days it will be column 'H', other days will be column 'GX' as the data in the sheet is constantly updated. So far I have seen how you can find the last column and then delete it, but it specifically refers to that certain column once the macro runs again. I need it to always refer to the last column, no matter what column that may be. Thanks!
Here is the code. I am new to VBA, etc. and this was created through the macro recorder and other things I found online so bear with me!
`Sub Macro11()
Sheets("Sheet25").Cells(1, 1).Activate
ActiveCell.SpecialCells(xlLastCell).Select
lastCol = ActiveCell.Column
Columns("W:W").Select
Selection.Delete Shift:=xlToLeft
End Sub`
Here is the sample code
Avoid using Select /Activate in your code. To know why refer this link
Sub Macro11()
Dim LastCol As Long
With ThisWorkbook.Sheets("Sheet25")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Columns(LastCol).Delete
End With
End Sub
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")
I'm looking for a way to have extra cells added to a table, in a sheet with multiple tables. Table A (Column B to F) should get an extra row, under the selected cell. Table B, (Column H to J) should be Fixed: When i add extra cells to row 8 in Table A, Table B should not change. I can only find the EntireRow.Insert, but not for specific cells.
My code so far:
Sub Button1_Click()
Dim anyR As Range
Set anyR = Selection.SpecialCells(xlVisible)
Selection.SpecialCells(xlVisible).Offset(1, 0).EntireRow.Insert
anyR.Offset(1, 0).Select
End Sub
But it adds cells to Table B as well. Thanx for having a look into this!
The following will add a cell beneath the current selection:
Selection.Offset(1, 0).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.SpecialCells(xlVisible).Offset(1, 0).EntireRow.Insert
You're inserting a row in the worksheet - that's like right-clicking the row heading and then selecting "insert".
If you have two tables on the same sheet, with rows at the insertion point, then yeah, both tables will earn an extra row.
If you want to add rows to one table, then work with the table instead of Selection (you hardly ever really need to work with Selection anyway):
Dim table1 As ListObject
Set table1 = Worksheeets("MyAwesomeSheet").ListObjects(1)
table1.ListRows.Add Position:=5, AlwaysInsert:=True
That will insert a new row in the first table of MyAwesomeSheet, making a blank 5th row in that table.
Note that this assumes "table" in the OP is referring to actual tables in the spreadsheet, i.e. that the target sheet has ListObject objects to play with.
I found a way:
Range("A" & ActiveCell.Row & ":L" & ActiveCell.Row).Select
Selection.Copy
Selection.Offset(1, 0).Insert Shift:=xlDown
Range("A" & ActiveCell.Row & ":L" & ActiveCell.Row).Offset(1, 0).ClearContents
ActiveCell.Offset(1, 0).Select
In this case, column A to L are selected.
Thanks for helping!
KR, freek
For i = 3 To 50
If lngRow = Range("A" & i) Then
Range("A1:EN3").Rows(i).Copy
Range("A1:EN3").Columns(strCol).Offset(, 1).PasteSpecial Transpose:=True
Range("A1:EN3").Rows(1).Copy
Range("A1:EN3").Columns(strCol).PasteSpecial Transpose:=True
Exit For
End If
Next i
Hello I have written code for selecting the row and paste it into column wise
it is working correctly but my problem is here i am giving range as Range("a1:en3") but every time it may be more the values than this so is it possible to copy the row data before blank cell like how we can copy the column before blank cell i,e Range(rng, rng.End(xlDown)).Copy.
Function firstblank()
i=1
While (worksheets("WORKSHEETNAMEHERE").cells(i,1).value<>"")
i=i+1
wend
return i
End function
Not totaly sure I understand your question but pasting the transposed values should be as easy as:
Range("T5").Select
Selection.PasteSpecial Transpose:=True
(i would not define a range bigger than one cell to write the values; if you give more than one cell excel will require your target to have the exact dimensions of what you are trying to write)