Subscript out of range for pasting transposed values - excel

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

Related

Can't point to a range in sheet

The code is the following:
Private Sub UnlockSheet()
Range(Selection.Offset(640, 1), Selection.Offset(0, 0)).Select
' I've tried this
Sheet1.Range(Selection.Offset(640, 1), Selection.Offset(0, 0)).Select
' I've tried this too
Dim ws as Worksheet: Set ws = ThisWorkbook.Sheets(1)
ws.Range(Selection.Offset(640, 1), Selection.Offset(0, 0)).Select
' and of course this, too
End Sub
This code - with any of the specified declaration - gives me error runtime error 1004.
Could someone explain me why? I can't get it. Docs is unclear to me.
First note that
Sheets(1)
Sheets("Sheet1")
Sheet1
can be 3 totally different sheets. Excel uses 3 different naming systems to reference sheets and they are totally independent.
Sheets(1) uses the position in the tab bar of the sheet.
Sheets("Sheet1") uses the tab name of the sheet
Sheet1 uses the VBA name of the sheet (that has nothing to do with the tab name!).
Also note that using Range without specifying a sheet makes Excec decide which sheet you mean. So if you want reliable code you always have to specify which sheet that range should be in.
You might benefit from reading
How to avoid using Select in Excel VBA.
If you try to .Select a range that is not in the active sheet VBA will throw "Runtime Error 1004". So you have to activate/select the sheet first. Also if Selection is not in the same sheet you try to select, it will fail too.
Dim ws As Worksheet
Set ws = ActiveSheet 'This ensures that `Selection` and `ws` are in the same sheet.
ws.Range(Selection.Offset(640, 1), Selection).Select
Note that .Offset(0, 0) does not make any sense. So remove it.
Note that the best practice is not to use .Select at all. Unless you want the user to see that selection.

Excel VBA Copy and Paste loop accounting for blank cells

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.

Setting up a macro to copy a range from one worksheet to another in the same workbook

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

How to copy data from one worksheet to another (using an indirect reference for the latter worksheet)

I'm trying to add some simple VBA functions to excel files at work (i work for a marketing company) and am struggling to code a button that can copy data from one sheet to another (the position it needs to copy to changes daily)
I'm using VBA to try and select the range of values i need copied (and it works) but i'm struggling to paste them in the daily-changing cells required.
I've tried writing a formula that presents a pathway to the correct cells and then introducing an indirect function to the VBA code for pasting but i can't seem to get it to work.
Sub Gross_Wrap_Click()
Sheets("AUTOM").Select
Range("C1:C5").Select
Selection.Copy
Sheets("Daily").Select
Range([indirect("'AUTOM!O7'")]).Select
Selection.Paste
End Sub
I expect the end product to be the contents of cells C1:C5 in the AUTOM sheet copied and pasted to the required cells in the DAILY sheet (the required cells are derived from a function and included in VBA with an Indirect pathway).
However, i'm just getting a load of different error messages
If I didn't messed it reading your code, this should do it:
Option Explicit
Sub Test()
Dim rng As Range 'Range object, means a cell or a range of cells
With ThisWorkbook 'here you reference the workbook, so you don't need to write Thisworkbook everytime, just "."
Set rng = .Sheets("Daily").Find(.Sheets("AUTOM").Range("O7")) 'we set the object rng to the cell in sheets Daily which has the Cell O7 from AUTOM sheet value.
.Sheets("AUTOM").Range("C1:C5").Copy 'copy the range from AUTOM
rng.PasteSpecial xlPasteValues 'and paste it's values to the range object we declared and defined above
End With
End Sub

Copy and Paste between Workbooks without using Range

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.

Resources