I have a question. When
I ran the code below to autofill to a range (sheets will have different lengths of rows). In my example, the length I am filling down to is only 6 and only the first column (Column U) fills correctly. The rest go to the bottom of the sheet at 10000.
Sub MacroFill ()
Dim LastRow As Long LastRow = Range("T4").CurrentRegion.Rows.Count + 1
Range("U4:U" & LastRow).FillDown
Range("V4:U" & LastRow).FillDown
...
Range("AZ4:AZ" & LastRow).FillDown
End Sub()
The columns I am trying to autofill down are from U:AZ. Is there a more efficient way to get them autuofill based on how many number of rows have data in a specified column( in my case column T4:T)?
Thank you
I tried to create multiple LastRow variables and label them for each row but its not efficient. I tried to combine in the Range of the length of my rows.
I think it's because you referenced Col U twice.
Either that or your lastrow reference.
Try this:
Sub MacroFill()
Dim LastRow As Long
LastRow = Range("T" & Rows.Count).End(xlUp).Row
Range("U4:V" & LastRow).FillDown
'...
Range("AZ4:AZ" & LastRow).FillDown
End Sub
Related
Looking for simple, elegant solutions using the range in its current form to append under another Worksheet's last row.
Range:
Sheets("conf_9").Columns(5).Copy Destination:=Sheets("Rec_9").Columns(9)
Sheets("conf_9").Columns(6).Copy Destination:=Sheets("Rec_9").Columns(10)
Sheets("conf_9").Columns(7).Copy Destination:=Sheets("Rec_9").Columns(7)
Sheets("conf_9").Columns(8).Copy Destination:=Sheets("Rec_9").Columns(8)
Sheets("conf_9").Columns(12).Copy Destination:=Sheets("Rec_9").Columns(6)
Several responses to a similar question seem overly complicated and incompatible to the Columns usage - yes, I am new to VBA. For example, inserting the expression Columns(number) & last row syntax is rejected.
Many thanks.
Try the next code, please. It is not necessary to copy whole columns. The code calculates the last filled row of the sheet to copy the column and the first empty row of the target sheet column:
Sub AppendToOtherSheet()
Dim shC As Worksheet, wsR As Worksheet, lastRowC As Long, lastRowR As Long
Set shC = Sheets("conf_9")
Set wsR = Sheets("Rec_9")
lastRowC = shC.Range("E" & Rows.Count).End(xlUp).row
lastRowR = wsR.Range("I" & Rows.Count).End(xlUp).row + 1
shC.Range("E2:E" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 9)
shC.Range("F2:F" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 10)
shC.Range("G2:G" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 7)
shC.Range("H2:H" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 8)
shC.Range("L2:L" & lastRowC).Copy Destination:=wsR.Cells(lastRowR, 6)
End Sub
The above code assumes that all columns have the same number of rows, and the sheet where from the columns are copied has column headers.
If the columns in discussion do not have the same number of rows, lastRowC and lastRowR must be calculated for each column to be copied. If no column headers, the range to be copied will start (for instance) from "E1" instead of "E2" in the first copying code line.
I'm looking to create a macro that will essentially identify the total number of rows on a sheet. Then take the values in a range on another sheet, copy that range, and paste it until it reaches the total number of rows that was identified on the first sheet. I am NOT looking to take a set of values and copy it to the last row of another sheet.
This is what I have so far. I'm having trouble trying to find the next step to take to accomplish my goal. I think I'm close, but my knowledge of variables is very limited.
Public Sub Delegation()
Dim lastrow As String
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Worksheets("delegating").Range("A1:A13").Copy _
End Sub
You can try something like this:
Public Sub Delegation()
Dim lastrow As Long
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
With Worksheets("delegating")
.Range("A1:A13").AutoFill Destination:=.Range("A1:A" & lastrow), _
Type:=xlFillCopy
End With
End Sub
I have a table from A to E. I don't know the row count of A column so it might be 15 or 450 or any number. What I need is when I run the macros I want it to add fixed number "7" to the column J1 till the last row of column A. I don't know how to make this. I tried many things but did not succeed.
Let us assume that we are using Sheet1. You may change from sheet1 to:
Then try:
Sub test()
Dim LR As Long
Dim i As Long
With Worksheets("Sheet1")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Range("J1:J" & LR).Value = "7"
End With
End Sub
Try this formula in J1 and drag it or copy it to the end of your data row:
Formula in J1: =if(A1="","",7)
I have solved the issue with this code block:
Dim LastRow As Long
LastRow = y.Sheets("csv").Cells(Rows.Count, "A").End(xlUp).Row
y.Sheets("csv").Range("J1:J" & LastRow).Value = "7"
Thank you All.
I have looked through the forum, as well as Google, and I wasn't able to find an answer to what I'm trying to do.
One of the users [here][1] helped me get a code to copy data in a specific row (upon clicking a button) to the last row of a table that is found further down in the same sheet.
However, I'd like the sheet to scroll down to the last non-empty row of the table instead of scrolling manually. I understand that this can be accomplished through a combination of CTRL+SHIFT+"DOWN ARROW". However; the excel users are not very good with computers, so I am trying to make it as simple as possible to them.
Is there a VBA code that can do the job?
Thanks
So far, I am using Erin's code, which takes me to the last row of the spread sheet, instead of the last Non-Blank row. This could be because column A has formulas in all its cells, all the way down to the last cell. However, column A cells formulas are set to give blank unless there's data entered in their adjacent cells in column E.
Here's the code that I am using, which is pasted in the module.
Option Explicit
Sub copyRow()
Dim ws As Worksheet
Dim lRow As Long
' define which worksheet to work on, i.e. replace Sheet1 with the name of
your sheet
Set ws = ActiveWorkbook.Sheets("1. Clients Details")
' determine the last row with content in column E and add one
lRow = ws.Cells(Rows.count, "E").End(xlUp).Row + 1
' copy some cells into their ranges
ws.Range("E3:G3").Copy ws.Range("E" & lRow)
ws.[E1].Select
' combine H3, I3, J3, K3 and L3 and copy into column E, next empty row
ws.Range("H" & lRow) = ws.[H3] & " " & ws.[I3] & " " & ws.[J3] & ", " & ws.
[K3] & " " & ws.[L3]
' copy the other cells into their ranges
ws.Range("M3:Q3").Copy ws.Range("M" & lRow)
ws.[M1].Select
' combine H3 & I3
ws.Range("AA" & lRow) = ws.[H3] & " " & ws.[I3]
' combine J3, K3 & L3
ws.Range("AB" & lRow) = ws.[J3] & " " & ws.[K3] & " " & ws.[L3]
' copy Q3 into column Q only, if F3 = "Company"
If Worksheets("1. Clients Details").Range("F3").Value = "Company" Then
ws.Range("Q3").Copy ws.Range("Q" & lRow)
End If
Call scrollToEnd
End Sub
Sub scrollToEnd()
Dim lastrow As Long
Dim numRows As Long
numRows = Range("E1:E1000").Rows.count
lastrow = Range("E1:E1000").Columns(5).Rows(numRows).Row
ActiveSheet.Range("E" & lastrow).Activate
End Sub
Dim lastrow as long
Dim numRows as long
numRows = Range ("TableName").Rows.Count
lastRow = Range ("TableName").Columns (1).Rows(numRows).Row
Activesheet.Range("A" & lastRow).Activate
I can't test it right now, but I believe this will work. If you know the offset for your table, you can just do numRows + offset (mine are usually in A1, so I just add 1 for the header - numRows is data rows) to get your row number for the .Activate. :-)
Or to reach the same row as CTRL+SHIFT+"DOWN ARROW", regardless of the table:
With Activesheet
.Range("A" & .UsedRange.Rows(.UsedRange.Rows.Count).Row).Activate
End With
EDITED: I was thinking CTRL+END in the above code. To simulate CTRL+"DOWN ARROW" (adding SHIFT selects everything in its path...), you would actually use:
Range("A1").End(xlDown).Activate
You could simply paste this at the end of your sub since it is one line, or keep it as its own little sub if you are wanting to call it from a button-click. If it is column E that you want selected, you would simply replace "A1" with "E1".
This does assume that there are no blank cells in column E between "E1" and the last non-blank row. Otherwise, you will need to use the same logic as in your copyRow sub to find the last non-blank row:
ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Activate
This will scroll down till the last row's cell is at the top left of the screen.
Sub test()
Application.Goto Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp), True 'where 'A' is the column of the last row
End Sub
You can paste the code at the bottom of your current procedure or assign it to a button.
EDIT:
Alternatively, you can try this. This will find the last row of any column.
Sub test()
Dim lastrow As Range
Set lastrow = Sheets("Sheet1").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious)
If Not lastrow Is Nothing Then Application.Goto lastrow, True
End Sub
This shouldn't be complicated code, but I am new to Excel VBA. I've tried many different methods resulting in bugs, infinite loops, and wrong selections.
I developed a little code which finds the duplicates in the column A and deletes one of the duplicates.
Sub Statistique()
Dim LastRow As Long
Set x = ActiveWorkbook.Sheets("COPYRIGHT")
LastRow = Range("A65536").End(xlUp).Row
For x = LastRow To 3 Step -1
If Application.WorksheetFunction.CountIf(Range("A3:A" & x), Range("A" & x).Text) > 1 Then
Range("A" & x).EntireRow.Delete
End If
Next x
Now I want to improve the code, by copying the non-blank cells of the row I want to delete and paste them in the row which will remain. It means that I want the non-blank cells to be copied upper, on the first duplicate row, in the same column they were before.
Any idea?
Try this:
Sub Statistique()
Dim rngColA As Range
Set rngColA = Worksheets("COPYRIGHT").Range("A1").EntireColumn
rngColA.RemoveDuplicates 1, xlGuess
End Sub
or
Sub Statistique2()
Dim rngColA As Range
Set rngColA = Worksheets("COPYRIGHT").Cells
rngColA.RemoveDuplicates 1, xlGuess
End Sub