I need a macro that runs through a workbook, copies specific cells, then pastes them into another workbook. I've seen macros that do this, but they all select ranges to accomplish it. I'm going to be running through hundreds of rows, so selecting ranges isn't going to work (I might as well do it be hand if I have to code hundreds of ranges). I'd like to accomplish this by using cell Offsets.
This is the code I have: I'm only showing the relevant areas.
Workbooks.Open (txtFileName) ' Opens the selected file
Set wbSource = ActiveWorkbook
Windows("CM activity log referral macro.xlsm").Activate
Set wbTarget = ActiveWorkbook
' Begin Populating CM Workbook: Caselist Worksheet
'Set initial rows
wbSource.Activate
wbSource.Sheets("Contact Log").Range("A4").Select
wbTarget.Activate
wbTarget.Sheets("Caselist").Range("A3").Select
'Populate Caselist from Selected Workbook
Dim i As Long
For i = 1 To 200
'Copy Date Encountered
wbSource.Sheets("Contact Log").ActiveCell.Copy
wbTarget.Sheets("Caselist").ActiveCell.PasteSpecial Paste:=xlPasteValues
'Copy First and Last Name
wbSource.Sheets("Contact Log").ActiveCell.Offset(0, 2).Copy
wbTarget.Sheets("Caselist").ActiveCell.Offset(0, 1).PasteSpecial
Paste:=xlPasteValues
wbSource.Sheets("Contact Log").ActiveCell.Offest(0, 1).Copy 'move to D4
wbTarget.Sheets("Caselist").ActiveCell.Offset(0, 1).PasteSpecial
Paste:=xlPasteValues
There's more to copy, and the for loop gets closed properly, but I'm not showing that here. I know that the problem is that Sheets doesn't support the following .Activecell statement. I need to know how to write this so I can use offset instead of Range to select the cells to copy and paste to.
Thank you so much for your time. I appreciate it.
Related
I am new to using VBA. I have an Excel document that has 500 sheets worth of data each sheet only has at most 45 rows and up to column W. I am trying to have a macro that copies the data from each of the 500 sheets and pastes it into a sheet named "Master". I am able to use the following code to perform this successfully.
Sub CopyPaste()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Master" Then
wks.Range("A1:W" & wks.Cells(Rows.Count, "A").End(xlUp).Row).Copy _
Destination:=Worksheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1)
End If
Next
End Sub
The issue I am running into is that some of the sheets have a row that is blank below the header before the data begins and as my code is written currently it seems like the macro thinks this is the end of the page and moves to the next sheet and is missing data that should be copied and pasted. I am looking for some assistance/guidance on how to account for these blank cells/rows so that the copy and pasting continue for the sheet through the end. Any help would be much appreciated, thank you.
I am a new programmer when it comes to VBA and I am writing a code that scans multiple Criteria and then copy and pastes to another sheet. I have 2 Sheets right now Sheet1 and Sheet2, I will first need to scan J9 on Sheet1 and then lookup Sheet2 for Sheet1’s J9, afterwards I will pull everything with J9 in it from Sheet2 to Sheet1 with it starting at a specific Row, I’ve tried doing AutoFilter but as I am rarely new I believe that my code should be obsolete, please go easy on me. Many Thanks in advance! PS(I am using VBA 2013)
Sub tgr()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim acrit() As String
Set wsData = Sheets("MaterialReport") 'Copying FROM this worksheet (it contains your data)
Set wsDest = Sheets("TIMINGBELTWPULLEY") 'Copying TO this worksheet (it is your destination)
'Populate your array of values to filter for
ReDim acrit(1 To 2)
acrit(1) = "TIMING"
acrit(2) = "PULLEY"
With wsData.Range("C1", wsData.Cells(wsData.Rows.Count, "C").End(xlUp))
.AutoFilter 1, aFruit, xlFilterValues 'Filter using the array, this avoids having to do a loop
'Copy the filtered data (except the header row) and paste it as values
.Offset(1).EntireRow.Copy
wsDest.Cells(wsDest.Rows.Count, "B29").End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False 'Remove the CutCopy border
.AutoFilter 'Remove the filter
End With
This is my code that I’m working with, I’ve pulled it from a training site and edited it to my needs
I have a workbook with multiple sheets and I'm trying to setup a macro in sheet2 which will clear the contents of sheet2 and then copy from sheet6 a range of data (columns A:Q) from row 2 down to the last row of data to the corresponding columns of sheet2 from row 2. I have found some code which can do the copying but I suspect that it will only work if the source sheet (sheet6) is the active sheet and I need this to be part of a macro to be run on sheet2 as there are other things to be added later to the macro.
The code I used on sheet2 is this:
Sub AImportData()
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
ActiveWindow.SmallScroll Down:=-3
Dim ws As Worksheet
Set ws = Sheets("Sheet2")
With Sheets("Sheet6")
.Range("A:Q").Copy Destination:=ws.Range("A2")
End With
End Sub
This clears the data in sheet2 but when it gets to the point where it needs to copy the data from sheet6 I get a subscript out of range error. I have tried to use ActiveWorksheet in my code but then I get an object required error. I need some guidance on this please?
Here, You can see the names of the sheets as shown in the Project Explorer:
There is a difference between a sheet's name and a sheet's object name. For example, you can see in this pricture that I have a sheet named "Sheet2" while its object's name is "Sheet5"
To refer to that sheet, you have to do one of the following:
1: Use the sheet name: Sheets("Sheet2")
2: Use the sheet's object name: Sheet5
3: Use the sheet's number (position): Sheets(2)
(The sheet's position will change when you remove sheets that are before this sheet. [For example, when you delete the first sheet, the sheet that was second will now be the first] So, be careful when you use this method.)
So, in your situation, you need to make sure you have a sheet named "Sheet6", or name it correctly.
Another issue you have is that you are trying to copy a set of whole columns (A:Q) to a position that is not a whole columns, but a part of a column starting from row number 2 (A2).
So, you have to do something like this:
Sub AImportData()
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
ActiveWindow.SmallScroll Down:=-3 'Do you really need this?
With Sheet6
maxRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
.Range("A2:Q" & maxRow).Copy Range("A2")
End With
End Sub
Apologies if this is a duplicate, but I do not know how to word this correctly.
I have a workbook with 7 worksheets all named differently.
I want to write an if function that will search all the worksheets, and if it matches that criteria to paste into "Name" sheet. I have tried multiple ways and cannot achieve this. This is where I will get the criteria.
Dim Client As String
Client = InputBox("Enter the Clients name")
This code works if I extract data from one sheet:
Dim pasteRowIndex As Long
pasteRowIndex = 2
Dim LR as Long
LR = Cells(Rows.Count, 1).End(xlUp).Row
For I = 2 To LR
If Cells(I, 1).Value = Client Then
'Now the code to grab the data and past into a new workbook
Rows(I).Select 'This just works on the active sheet, but I want this to iterate through
all the sheets and paste the range into sheet2 in this code
Selection.Copy
'Switch to the sheet where you want to paste it & paste
Sheets("Sheet2").Select 'However I will change this name
Rows(pasteRowIndex).Select
ActiveSheet.Paste
'Next time you find a match, it will be pasted in a new row
pasteRowIndex = pasteRowIndex + 1
'Switch back to your table & continue to search for your criteria
'Sheets("Sheet1").Select
End If
Next I
While the above code works for a single sheet I want the macro to iterate through all the sheets, and they are not named Sheet1-Sheet7.
Also, there must be a cleaner way of doing this.
Apologies if this is unclear, do let me know if more clarity is required.
You may loop through all the sheets in your workbook as following sample code:
Dim Sht As Worksheet
For Each Sht In Worksheets
If Sht.Name <> "Sheet1" Or Sht.Name <> "Sheet7" Then
'Do your stuff here
'sample code to refer the row at specific sheet without activating it
Sht.Rows(I).Copy
Sheets("Sheet2").Cells(pasteRowIndex, 1).PasteSpecial xlPasteAll
End If
Next
Will recommend to avoid use of .Select and .Activate method. Instead use Worksheet variable to refer to the specific sheet.
I am new to VBA and running into an issue with a paste / value / transpose Macro. I need to be able copy the information from multiple workbooks all with different names, however the workbook where it will be pasted will be the same except for the cells where it will be pasted.
I have tried changing ("Sheet") to the actual sheet name which is "Job Cost Summary" as well as changing "Sheet" to "Sheet1". One thing that happens is when I copy the information from the workbook, it shows the hashed line of the information I have copied but as soon as I hit "macros" the hash marks go away. Not sure if this is important.
Sub Pasteinfo()
Dim WS As Worksheet
Set WS = ThisWorkbook.Worksheets("Sheet1")
WS.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
End Sub
I am hoping for a Macro that will allow me to take previously copied information from workbooks & worksheets with different names and paste them as values & transposed into another workbook.
When I run above code I am getting an error 9 that says "Subscript out of range."