I am trying to create a macro.
LR = Range("Y3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("Y3:Y" & LR)
LR = Range("C3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("R3:R" & LR)
LR = Range("C3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("B3:B" & LR)
LR = Range("C3333333").End(x1Up).Row
Range("C3").AutoFill Destination:=Range("A3:A" & LR)
My number of rows varies each time I run this. These (4) columns have nothing in rows 1 or 2. Row three is a formula that I want to copy to the last column.
Column C is the only column that will always have information in it all all times for all lines.
use:
Dim LR As Long
With Worksheets("Sheet1") 'Change to your Worksheet name
LR = .Cells(.Rows.Count, 3).End(xlUp).row
.Range("Y3:Y" & LR).FillDown
.Range("R3:R" & LR).FillDown
.Range("B3:B" & LR).FillDown
.Range("A3:A" & LR).FillDown
End With
An alternative (more common) way of writing it is:
Dim LR As Long
LR = Activesheet.Range("Y" & Rows.Count).End(xlUp).Row
Range("C3").AutoFill Destination:=Range("Y3:Y" & LR)
This would work in any sheet (also compatibility mode), and you do not need to autofill anymore
Related
I am trying to copy cell data from one sheet to another based on a match.
I have a workbook with 3 sheets. "Place", "Coke" and "HMS".
On Active sheet "Place", If column C14 has the word "Coke" - I want D14-H14 copied to the sheet "Coke".
Similarly if C15 contains "HMS" - I want only "D15-H15" Copied to the sheet "HMS"
I have a macro that copies the entire row while I want specific cells copied - which is from the specific C:H.
Sub As_Of_Analysis_Sorting()
Dim lr As Long, lr2 As Long, r As Long
lr = Sheets("Place").Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("Coke").Cells(Rows.Count, "A").End(xlUp).Row
lr3 = Sheets("HMS").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
If Range("C" & r).Value = "Coke" Then
Rows(r).Copy Destination:=Sheets("Coke").Range("A" & lr2 + 1)
lr2 = Sheets("Coke").Cells(Rows.Count, "A").End(xlUp).Row
End If
If Range("C" & r).Value = "HMS" Then
Rows(r).Copy Destination:=Sheets("HMS").Range("A" & lr3 + 1)
lr3 = Sheets("HMS").Cells(Rows.Count, "A").End(xlUp).Row
End If
Range("A140").Select
Next r
End Sub
Hos do I achieve this?
Firstly a "good practice" suggestion: always tell which sheet the range is in:
Range("C" & r).Value
'becomes
Range("Place!C" & r).Value
'or
sheets("Place").Range("C" & r).Value
Now the answer: the code just does what you told it to do:
Rows(r).Copy Destination:=Sheets("Coke").Range("A" & lr2 + 1)
==> you copy the whole row and place it in A. That's replacing the whole row
If you do NOT need format, you should use this:
range("Coke!A" & lr2 +1 & ":F" & lr2 +1 ) = range("Place!C" & r & ":H" & r).value2
if you need format (slower code) :
range("Place!C" & r & ":H" & r).copy Destination:=Sheets("Coke").Range("A" & lr2 + 1)
I am trying to have vba create a formula for two ranges within the same data set, as input into variables lastRow and lastRow2. However, when I try to create and calculate the formula, instead of getting the value of the last cell I get a zero. Offending code below:
Dim lastRow As Long
Dim lastRow2 As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
lastRow2 = Range("M" & Rows.Count).End(xlUp).Row
...
'Calculate ATRT 2014 at cell B4
Range("B6").Formula = "=(SUM(J11:J" & lastRow & ")/ SUM(I11:I" & lastRow & "))"
Range("E6").Formula = "=(SUM(U11:U" & lastRow2 & ")/ SUM(T11:T" & lastRow2 & "))"
Range("H6").Formula = "=E6-B6"
Running this gets two formulas: =(SUM(J11:J0)/ sum(I11:I0)) and =(SUM(U11:U0)/ SUM(T11:T0)). Why is the end of the range a zero???
I'm pretty sure what is happening if you are not defining which worksheet the range() and row() objects are on so VBA is guessing and probably guessing incorrectly. Try adding a worksheet object and then defining all the ranges and rows to use be on that worksheet.
Dim lastRow As Long
Dim lastRow2 As Long
Dim ws As Worksheet
Set ws = ActiveSheet
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
lastRow2 = ws.Range("M" & ws.Rows.Count).End(xlUp).Row
'...
'Calculate ATRT 2014 at cell B4
ws.Range("B6").Formula = "=(SUM(J11:J" & lastRow & ")/ SUM(I11:I" & lastRow & "))"
ws.Range("E6").Formula = "=(SUM(U11:U" & lastRow2 & ")/ SUM(T11:T" & lastRow2 & "))"
ws.Range("H6").Formula = "=E6-B6"
I have made two functions that makes a calculation from two different linest (UpperCalc and LowerCalc). This works fine.
Then I will need to fill in formulas for an unknown number of cells (depending on the input on another sheet). I have been able to fill in formulas for the correct number of cells. But when I try to include an "IF"-formula in the VBA programming together with the function names, it does not work? My VBA code to fill in the formula looks like this now;
Dim lastRow As Long, i As Long, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
lastRow = ws1.Range("B" & Rows.Count).End(xlUp).Row
With ws1
For i = 2 To lastRow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then _
ws2.Range("A" & i).Value = ws1.Range("A" & i).Value
ws2.Range("B" & i).Value = ws1.Range("B" & i).Value
ws2.Range("C" & i).Formula = "=IF(RC[-1]>R4C12,UpperCalc(RC[-1]),LowerCalc(RC[-1]))"
Next i
End With
End If
I am able to insert formula with one of these functions (for instance ws2.Range("C" & i).Formula = "=UpperCalc(RC[-1])", and also only with an "IF" formula (for instance ws2.Range("C" & i).Formula = "=IF(RC[-1]>R4C12,RC[-1],RC[-1]^2)" - This is of course not the actual calculation needed - only to test the "IF"-function).
Since the calculation behind UpperCalc and LowerCalc is rather "dirty", I would like to utilize the functions. Any ideas?
I am trying to copy and paste data from an input sheet into an output sheet, and once the data is in the next spreadsheet, it will be filled down the next rows from a starting date to an ending date.
As is, the code gets me the following errors when debugging:
ws1.Range("A" & NextRow) = Method of Range Object_Worsheet failed; same for ws1.Range("B" & NextRow); ws1.Range("C" & NextRow) etc...
ws1.Cells(LastRow, "H") = Application-defined or object-defined error.
Also, I noticed that when I set RawDataEntries it equals 37, that is the last non-empty row, but then when I try to use the For loop and point my mouse on RawDataEntries in For n=1 To RawDataEntries, VBA returns me a value of 105, which seems to come out of nowhere.
I think the logic behind the code is correct. What might have gone wrong?
Sub AddFlight_Click()
Const RNG_END_DT As String = "N2"
Dim NextRow1 As Long, LastRow1 As Long, ws1 As Worksheet
Dim ws2 As Worksheet, RawDataEntries As Long
Set ws1 = Sheets("JetAir Flight Plan")
Set ws2 = Sheets("TUI B Flight Plan")
LastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
NextRow1 = LastRow1 + 1
RawDataEntries = ws2.Range("A" & Rows.Count).End(xlUp).Row
For n = 1 To RawDataEntries
'Data from an input worksheet is copied and pasted into specific cells in an output worksheet.
ws1.Range("A" & NextRow).Value = ws2.Range("A" & n).Value
ws1.Range("B" & NextRow).Value = ws2.Range("B" & n).Text
ws1.Range("D" & NextRow).Value = ws2.Range("D" & n).Text
ws1.Range("E" & NextRow).Value = ws2.Range("E" & n).Text
ws1.Range("F" & NextRow).Value = ws2.Range("F" & n).Text
ws1.Range("G" & NextRow).Value = ws2.Range("G" & n).Text
ws1.Range(RNG_END_DT).Value = ws2.Range("H" & n).Value
'A series of dates is created from a starting date
' to an ending date in column A of ws1.
ws1.Range("A" & NextRow).DataSeries Rowcol:=xlColumns, _
Type:=xlChronological, Date:=xlDay, Step:=7, _
Stop:=ws1.Range(RNG_END_DT).Value, Trend:=False
'The data filled in the last row with the userform data through
' the first part of the macro will be copied and pasted in
' the next row until there is a blank cell in column A.
LastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
ws1.Range(ws1.Range("B" & NextRow), ws1.Cells(LastRow, "H")).FillDown
'We repeat the process for other rows on the sheet data are pulled from
Next n
Thanks a lot.
Change NextRow to NextRow1. Right now you are calling a variable that doesn't exist.
Or you can do the opposite (NextRow1 to NextRow)
Same with LastRow and LastRow1
I have the following macro running on a workbook to copy data with a specific criteria from "Master" sheet to "Quarantined" sheet;
Dim LR As Long, LR2 As Long
Application.ScreenUpdating = False
With Sheets("Quarantined")
LR2 = .Range("L" & Rows.Count).End(xlUp).Row
If LR2 > 2 Then
.Range("A3:I" & LR2).ClearContents
End If
End With
With Sheets("Master")
LR = .Cells(Rows.Count, 8).End(xlUp).Row
LR2 = Sheets("Quarantined").Range("L" & Rows.Count).End(xlUp).Row
With .Range("L2:L" & LR)
.AutoFilter Field:=1, Criteria1:="QUARANTINED"
.Offset(1).Resize(LR).EntireRow.Copy Sheets("Quarantined").Range("A" & LR2 + 1)
.AutoFilter
End With
End With
Application.ScreenUpdating = True
It works perfectly but if I update the master and run the macro again it pastes it under the original information on the quarantined sheet. How do I get it to overwrite the information that was already there instead of pasting underneath?
Here's hoping
It is pasting under the original info because you are telling it to.
You are clearing your range from .Range("A3:I" & LR2).ClearContents but you are taking the next available row from Col L LR2 = Sheets("Quarantined").Range("L" & Rows.Count).End(xlUp).Row
Either Change your code to this
.Range("A3:L" & LR2).ClearContents
or take the last row based on Col A instead of Col L