I think my biggest problem here is that I don't know exactly how to ask what I am looking for.
I am trying to look up information in a column in(workbook A) using criteria. For instance, I need to look up a site name which we will call "Site X". If column 2 meets this criterion I want to select a range of cells and copy.
Once I have selected the range of cells I want to activate (workbook b) which will already be open in my macro process, select the correct sheet, find "Site x" in a specific column, compare the date in a column in (workbook A) and (workbook b), then place the copied data in the correct location in the existing data source.
I have found plenty of code that can do the first part of my problem, however, the paste criterion has me stumped. My apologies If this is already answered in this forum. Please direct me to a link if able.
Here is some of the code I am attempting to use.
Sub Part3Transfer1()
Dim LastRow As Integer, i As Integer, erow As Integer
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 2) = "Site x" Then
Range(Cells(i, 5), Cells(i, 17)).Select
Selection.Copy
Workbooks("InstanceOnePartTwo.xlsb").Activate
Worksheets("sheet1").Activate
If Cells(i, 2) = "Site X" Then
Range(Cells(i, 68), Cells(i, 81)).Select
Selection.Paste
Next i
End If
End Sub
Well this is not exactly an answer, but I thought I'd give you some pointers, I've tried commenting the code with some things that may help you,... (I may get corrected on these, but I would welcome some more pointers), you should definitely look into With Statements to clarify which workbook and worksheets you are working with:
Sub Part3Transfer1()
Dim LastRow As Long
Dim i As Long
'these are declared as Long because there are more cells in Excel than there are Integer values.
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
'*** LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row ***'
'To find the last row, its usually better to declare which Sheet by either
'their code name (ie. Sheet1 or Sheet2) or by declaring them by name (such as, Sheets("Sheet1"))
For i = 2 To LastRow
If Cells(i, 2) = "Site x" Then ' better to declare the Sheet and workbook when refering to Cells as you are moving things from workbook to workbook
' like Sheet1.Cells or Sheets("Sheet1").Cells
Range(Cells(i, 5), Cells(i, 17)).Copy 'its considerably faster not to use Select
'Sheet1.Range(Cells(i, 5), Cells(i, 17)).Copy
End If
Workbooks("InstanceOnePartTwo.xlsb").Activate
Worksheets("Sheet1").Activate
'by activating the workbook and sheet above, it assumes that the file are already open,
'so if they is isn't open I'm sure you'll see an error, but you could change the active for an alternative
'to actually open the workbook for you?
If Cells(i, 2) = "Site X" Then 'You should declare the workbook and Sheet when refering to Cells so it doesn't unintentionally pick the cells from the ActiveSheet
'something like before Worksheets("Sheet1").Cells(i, 68), Worksheets("Sheet1").Cells(i,80))
Range(Cells(i, 68), Cells(i, 81)).PasteSpecial xlPasteValues
'use PasteSpecial with xlPasteValues to paste after your copy
'the range of your copy statement selects a smaller range than the range you are pasting into
'so you won't get an error, but I'm pretty sure they should be the same size (ie. from column 5 to 17 to copy
'columns 68 to 81,...?
'Consider using With statements, instead of this, but this will
'show you how to refer to a cell in a specific workbook:
'Workbooks("MySpreadsheet.xlsx").Worksheets("Sheet1").Range("A1").Value = "Hello"
End If
Next i
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.