Copy/Paste in VBA Loop [duplicate] - vba

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.

Related

Is there a limit to how many cells I can assign to an array? [duplicate]

This question already has answers here:
Arrays in Excel VBA. At some point it puts NA instead of the value
(1 answer)
Best workaround for VBA Transpose array length limit?
(3 answers)
Why is my array breaking the column when I use TRANSPOSE to paste it into a worksheet?
(2 answers)
Closed last year.
I am trying to assign all the values from a column to an array using VBA.
Dim all_datapoints() As Variant
With ActiveSheet
'get number of datapoints in the desired column
n = .Cells(1, 4).End(xlDown).Row
'assign column values to array
all_datapoints = Application.Transpose(.Range(.Cells(1, 4), .Cells(n, 4)))
End With
The value of n is about 240,000 but the array all_datapoints() only contains 42,783 datapoints. I looked over my datasheet and there are no N/A, Error, or missing values in this column (should just be a column of strings). I am not sure why it refuses to get the rest of the data.
Even if I include ReDim all_act_molecules(1 to n) before assigning the range, it limits to only grab the first 42,783 values. Help!

How to fill cell with data from a separate worksheet in excel?

I am attempting to match 2 columns in two separate worksheets and then fill data from worksheet 2 in worksheet 1.
I need to match Column A(Worksheet 2) to Column D(Worksheet 1). Once Matched I need to fill Column F(Worksheet 1) with the data from Column B(Worksheet 2). Once data is populated I would also like to change the color of Column F(Worksheet 1) based on the data that is present. Worksheet 1
Worksheet 2
Put this in F2 On Sheet 1, update the sheet name Sheet2 to whatever your second sheet is named, then copy down:
=VLOOKUP($D2,Sheet2!$A:$B,2,FALSE)
Then you can apply conditional formatting to Column F on Sheet 1.
If you might have values in Sheet 1 that aren't in Sheet 2, this will handle the error:
=IFERROR(VLOOKUP($D2,Sheet2!$A:$B,2,FALSE),"Not Found!")
[Updated for additional question about spanning accross workbooks]
For another open workbook, use the following and replace [Book2] with the path of the second workbook or the name of an open workbook:
=IFERROR(VLOOKUP($D2,[Book2]Sheet2!$A:$B,2,FALSE),"Not Found!")
Also note, Excel will automatically build all of the references if you select them manually while building the formula in the formula bar: Excel Formulas Overview on MSDN
More information is needed to provide you with an exact code but here is a good start
'Assuming there are 10 rows in each worksheet
dim i as integer
dim j as integer
for i = 1 to 10
for j = 1 to 10
if sheet1.cells(i,4).value = sheet2.cells(j,1).value then
sheet1.cells(i,6).value = sheet1.cells(j,2).value
sheet1.cells(i,6).interior.color = vbyellow
end if
next j
next i
the color can also be controlled with the rgb function, simply replace the vbyellow in the above code:
For example rgb(255,204,255) will be a light pink

Copying one sheet to another workbook based on one criteria

I have 2 different workbooks, main and copy.
Row 1 is meant for header/labeling the information it will be providing for both workbooks.
The "main" workbook will be using columns A to N. The copy will be using columns A to M.
The criteria to determine whether the code will be copying is the workbook, "main", column M.
If the cell contains "X" - it will copy column A to L, and N, to the workbook "copy". After which, it will go on to the next row to determine the same thing.
If the cell is empty, it will proceed down to the next row to determine the same thing as well.
The code has to be dynamic as new information will be added every 3 months, such as new rows added or the criteria changing from "X" to empty, or empty to "X".
I am a beginner in VBA excel, and have been trying out multiple codes but it doesn't seems to work. Would greatly appreciate it if someone could help me out with this.
Showing your code so far will help us a lot.
Maybe this helps a little:
Dim wks As Worksheet
Dim wks_copy As Worksheet
Set wks = Worksheets("main")
Set wks_copy = Worksheets("copy")
j = 2
For i = 2 To wks.UsedRange.Rows.Count
If wks.Cells(i, 13).Value = "x" Then
wks.Range(Cells(i, 1), Cells(i, 14)).Copy Destination:=wks_copy.Cells(j, 1)
j = j + 1
End If
Next i
This will copy the entire row. If you don't want to copy column M, I suggest clearing or hiding the column after copying.
If the macro runs again after 3 months, it will overwrite the existing data on Worksheet copy. But you should delete the worksheet's values before that, for example by using
Worksheets("copy").UsedRange.Offset(1, 0).ClearContents
or manually clearing the range.

Using a variable to define a range [duplicate]

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")

Excel VBA Copy Paste [duplicate]

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