This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Closed 7 years ago.
I'm trying to copy and paste a certain range from one worksheet to two other worksheets. This is a snippet of code where it seems to go wrong:
row = ActiveWorkbook.Sheets("SheetX").Cells(Rows.Count, 3).End(xlUp).Row
ws.Range("A1", "J1").Copy
ActiveWorkbook.Sheets("Sheet1").Range("B2", "K2").PasteSpecial xlPasteValues
ActiveWorkbook.Sheets("SheetX").Range(Cells(row, 3), Cells(row,12)).PasteSpecial xlPasteValues
Since the amount of rows is dynamic "row" holds the row number I want to paste it to.
The problem is that I get a "Application-defined or Object-defined"-error on the last line, where I try to past it to the second worksheet.
When you run Cells(row,3) you are actually calling Cells(row,3) on the ActiveSheet and not on Sheets("SheetX"). Instead you should do this:
row = ActiveWorkbook.Sheets("SheetX").Cells(Rows.Count, 3).End(xlUp).Row
ws.Range("A1", "J1").Copy
ActiveWorkbook.Sheets("Sheet1").Range("B2", "K2").PasteSpecial xlPasteValues
Range(ActiveWorkbook.Sheets("SheetX").Cells(row, 3), ActiveWorkbook.Sheets("SheetX").Cells(row,12)).PasteSpecial xlPasteValues
Related
This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 4 years ago.
i have the following method to set to define a ranga:
Set RegJan = Sheets("BD").Range("T3:T50000")
the range is smaller than this, but in order to make it work for some months, i set it to 50000 rows.
i then use this code in the following line of vba:
ws.Cells(8, 2).Value = Application.WorksheetFunction.SumIfs(RegJan,
EquipaBd, EquipaForm, AgenteBd, AgenteForm) 'Soma dos Registos
This last line i already transformed in a loop so that it calculates in every line i need it,the problem is that i have to do the first line of code for every month of the year an for 6 more variables
is there a better way of doing this? possibly a dynamic way?
This might give you some ideas:
With Sheets("BD")
' Since T is column #20 and goes to 6 more
For col = 20 To 26
' get last row
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
' set range
Set yourRng = .Range(.Cells(3, col), .Cells(lastRow, col))
' you might want to change 8,2 to make dynamic
ws.Cells(8, 2).Value = Application.WorksheetFunction.SumIfs(yourRng , EquipaBd, EquipaForm, AgenteBd, AgenteForm)
Next
End With
I know this is a real simple solution, but what I want to do is take column D14:E, and paste the numerical values into another part of the worksheet, the correct code is below:
Range("D14", Cells(Rows.Count, "E")).Copy
Range("AH14", Cells(Rows.Count, "AI").End(xlUp)).PasteSpecial Paste:=xlPasteValues
My issue here is when I am trying to take a second set of columns and paste it underneath the one I just pasted. The code I am trying is below:
Range("N14", Cells(Rows.Count, "O")).Copy
Range("AH14" & Cells(Rows.Count, "AI").End(xlUp)).PasteSpecial Paste:=xlPasteValues
Note that the columns are varying sizes (meaning column D could be a different size because of previous variables that were inputted (I had a question that was answered previously if you want to know what I mean)).
Range("N14", Cells(Rows.Count, "O").End(xlUp)).Copy ' Copy columns
Range("AH14").End(xlDown).Offset(1, 0).Select ' Find the first free column, select it
Selection.PasteSpecial Paste:=xlPasteValues 'paste copied cells
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'd like to start by telling you that I spent at least 2 hours reading different questions/answers on Stackoverflow and random google search results. I couldn't find an answer to my specific problem although a lot of questions/answers dealt with similar problems.
Every week, I'm manually copying rows from an Excel sheet into another Excel sheet based on certain criteria. In one column, the value of the cells that interest me are "not done" and in a second column I'm looking for due date that is in the past, i.e. overdue items. If both criteria are met, I copy the entire row into a newly created sheet in another Excel file.
I know VBA basics and thought about making my life easier by writing a macro that copies the respective rows into another Excel file and a new sheet. However, I'm not able to write a rather complex macro yet :(
Can you please help me by explaining how to write two loops (of some sort) that first look through the first column (find cells where value is not X) and after that look for a date in the past in a second column and then copy the rows where these two criteria met? Is that even possible with VBA? I'm not asking for the whole macro because I like to figure out how to get the remaining code right, but these loops are very complicated for a beginner and I'd really appreciate some guidance here.
Thanks in advance for taking the time to read this wall of text.
Edit: After checking excel-easy (thanks #maxhob17 ) I managed to make some progress. Please see this code so you get an idea of my progress. This code gets all the relevant rows based on the first criterion (status = done) and copies them into a new sheet in the same Excel file.
Public Sub Copy_Relevant_Items()
Dim CurrentWorkbook As Workbook
Dim InputWS As Worksheet
Dim OutputWS As Worksheet
Set CurrentWorkbook = Workbooks(ActiveWorkbook.Name)
Set InputWS = CurrentWorkbook.Sheets("Overview")
Set OutputWS = CurrentWorkbook.Sheets("Relevant")
Dim criterion As String
criterion = "Done"
Dim cells As range, cell As range
'Find the last used row in a Column: column C in this example
With InputWS
LastRow = .cells(.rows.Count, "C").End(xlUp).row
End With
Set cells = range("C2:C" & LastRow)
'Copy all the relevant rows into another sheet
For Each cell In cells
If cell.Value <> criterion Then
cell.EntireRow.Copy Destination:=OutputWS.range("A" & rows.Count).End(xlUp).Offset(1)
End If
Next cell
End Sub
you could use AutoFilter()
assuming your database spans from column A to D and dates are in column D then you could code
Option Explicit
Public Sub Copy_Relevant_Items()
Dim InputWS As Worksheet, OutputWS As Worksheet
Dim criterion As String
Set InputWS = ActiveWorkbook.Sheets("Overview")
Set OutputWS = ActiveWorkbook.Sheets("Relevant")
criterion = "Done"
With InputWS
With .Range("A1:D" & .cells(.Rows.Count, 1).End(xlUp).Row) '<--| reference its columns A to C from row 1 down to column A last not empty row. Change A and D to your actual data limit columns index
MsgBox .Address
.AutoFilter Field:=3, Criteria1:="<>" & criterion '<--| filter column C cells with content different from 'Criterion'. change "3" to your actual relative position of "status" column inside your database
.AutoFilter Field:=4, Criteria1:="<" & CLng(Date) '<--| filter column D cells with content less than current date. change "4" to your actual relative position of "date" column inside your database
If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Copy Destination:=OutputWS.Range("A" & Rows.Count).End(xlUp).Offset(1) '<--| if any cell filtered other than headers then copy them to 'OutputWS'
End With
End With
End Sub
This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Closed 7 years ago.
I am attempting to store the value of an 8 x 1 range into a range of identical dimensions, but on another sheet in the workbook. This would be easy except that my script is looping through different ranges of these same dimensions and I need to store them all on the second sheet. Currently my code looks like this:
Sheets("Sheet1").Range(Cells(i, 2), Cells(i + 7, 2)).Value = Sheets("Sheet2").Range("OriginalData").Value
Where "i" is the variable being used as the iterator in the loop.
This code throws an error "Error 1004 "Application-defined or Object-defined error"". Can someone explain what I'm doing wrong, and how to properly define range objects dynamically in this fashion?
Your problem is that the Cells inside the Sheets("Sheet1").Range don't know that they are supposed to belong to Sheets("Sheet1").
with Sheets("Sheet1")
.Range(.Cells(i, 2), .Cells(i + 7, 2)) = _
Sheets("Sheet2").Range("OriginalData").Value
end with
'alternate
Sheets("Sheet1").Range("B" & i).Resize(8, 1) = _
Sheets("Sheet2").Range("OriginalData").Value
The With ... End With statement allows you to definitively pass the parent worksheet into both .Range and .Cells with the prefix period (aka full stop).
Would a copy and paste work? I'm not sure of the specifics of your data but seems like a copy and paste would work better.
So like
Sheets("Sheet1").Range("x:x").Copy Sheets("Sheet2").Range("x:x")
This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Closed 7 years ago.
I have multiple sheets in Excel that contain columns of data. What I'm trying to do is copy and paste all Columns D from the data worksheets into another new worksheet, all Columns E from the data worksheets into another worksheet, etc. I'm able to create 7 new Excel Worksheets to collect the data and the data worksheets start with Worksheet 8 and on. Here is a snip of my code:
For t = 8 To tabs
Sheets(t).Range("D1:D2544").Copy Worksheets("OCM_VMonM24").Range(Cells(1, t - 6), Cells(2544, t - 6))
Sheets(t).Range("E1:E2544").Copy Worksheets("OCM_VMonM12").Range(Cells(1, t - 6), Cells(2544, t - 6))
Sheets(t).Range("F1:F2544").Copy Worksheets("OCM_VMonP24").Range(Cells(1, t - 6), Cells(2544, t - 6))
Next t
It works fine for the first line in the loop however when it attempts to copy Column E (second line of code in the For Loop) it fails. I'm new to VBA and can't figure out what I'm doing wrong.
Your paste area needs more qualification Change:
Worksheets("OCM_VMonM24").Range(Cells(1, t - 6), Cells(2544, t - 6))
to this
Worksheets("OCM_VMonM24").Range(Worksheets("OCM_VMonM24").Cells(1, t - 6), Worksheets("OCM_VMonM24").Cells(2544, t - 6))
Change each of the three paste ranges to reflect the changes I did to the first one.
If you do not qualify the Cells reference it looks at the active sheet. So you are calling a range from one sheet but referencing cells from another.