Run time error 9 - subscription out of range - excel

I am getting constant error of "out of range" but the reference of the range is correct, please help me to solve the error: the error or bug in the coding is highlighted as bold in the below mentioned code:
Sub CopyStuff()
**Sheets("Data-BNF").Range("D11:X76").Copy**
Sheets("Storage-OI").Range("C" & Rows.Count).End(xlUp).Offset(2, 0).PasteSpecial xlPasteValues
End Sub

Modify the below and try:
Option Explicit
Sub Copy()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lastrow As Long
With ThisWorkbook
Set ws1 = .Worksheets("Data-BNF")
Set ws2 = .Worksheets("Storage-OI")
End With
Lastrow = ws2.Cells(ws2.Rows.Count, "C").End(xlUp).Row
ws1.Range("D11:X76").Copy
'Try the **ONE of the below**
ws2.Range("C" & Lastrow + 1).PasteSpecial Paste:=xlPasteValue
ws2.Range("C" & Lastrow + 1).PasteSpecial xlPasteValues
End Sub

Related

Im trying to copy the hole row of a cell and paste it on last row of another column, but I keep getting an error

Im getting error 1004 vba application defined
here goes my code:
Sub Example()
Dim finalRowF As Integer
finalRowF = Cells(Rows.Count, "F").End(xlUp).Row
Range("A1").EntireRow.Copy Cells(finalRowF + 1, "H")
End Sub
Can someone take a look?
How about:
Sub Example()
Dim ws As Worksheet
Dim FinalRowF As Long
Set ws = Sheets("Sheet1") 'Change as necessary
FinalRowF = ws.Range("F" & ws.Rows.Count).End(xlUp).Row
Set CopyRange = ws.Range("A2:A" & FinalRowF)
CopyRange.SpecialCells(xlCellTypeVisible).Copy
ws.Range("H2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub

Index Match Code Modification Help Required to fix it

I have been using this code and it is working perfectly but here is one issue which solution is required that i don't want same formula in output it should paste just result rather than this formula =INDEX(J:J,MATCH(I1,I:I,0))"
Sub FindValue()
Dim Lastrow As Long
Dim SourceSheet As Worksheet
Dim SourceLastrow As Long
Set SourceSheet = Worksheets("Summary")
With SourceSheet
SourceLastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With Worksheets("Summary")
Lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("K1:K" & Lastrow).Formula = "=INDEX(J:J,MATCH(I1,I:I,0))"
End With
End Sub
Try this code, please:
Lastrow = Worksheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row
With Worksheets("Summary").Range("K1:K" & lastRow)
.Formula = "=IF(ISNA(INDEX(J:J,MATCH(I1,I:I,0))),"""",INDEX(J:J,MATCH(I1,I:I,0)))"
.Value = .Value
End With

How to copy data from sheet to another sheet on empty row

Hy Experts, I have two worksheets. I am trying to copy the data from sheet "Input Sheet" to "Database" sheet by using this code.
Sub CopyInvoiceNo()
Dim ws As Worksheet, ws1 As Worksheet
Dim lastrow As Long
Set ws = Sheets("Input Sheet")
Set ws1 = Sheets("Database")
lastrow = ws.Cells(Rows.Count, 4).End(xlUp).Row
ws.Range("A3:J" & lastrow).Copy
ws1.Range("B3").PasteSpecial xlPasteValues
ws1.Activate
End Sub
this code is working very fine. But there is a problem. It overwrites the data when I press the button 2nd time.
The goal is that every time when I press "Paste Button" it should paste data after the first filled row. I tried but in vain. HOw could it be done. Thanks in advance..
You aren't adjusting the target of the paste operation.
Sub CopyInvoiceNo()
Dim ws As Worksheet, ws1 As Worksheet
Dim lastrow As Long
Set ws = Sheets("Input Sheet")
Set ws1 = Sheets("Database")
lastrow = ws.Cells(Rows.Count, 4).End(xlUp).Row
ws.Range("A3:J" & lastrow).Copy
ws1.Range("B" & rows.count).end(xlup).offset(1, 0).PasteSpecial xlPasteValues
ws1.Activate
End Sub

VBA- Transferring data to another work sheet if a column has certain text and pasting it in a certain cell range

I am currently trying to filter data and paste it into another sheet to a certain range but it is only posting the latest data row. How do I fix the code so that it selects all the rows with the code word and pastes it into the other sheet.
This is my code:
Private Sub CommandButton1_Click()
Dim lastrow As Long, i As Long
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Sheets("sheet1").Cells(i, 1) = "pp" Then
Sheets("sheet1").Range(Cells(i, 2), Cells(i, 5)).Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet5").Range("A11:A22")
End If
Next
End Sub
I think this is what you want.
Private Sub CommandButton1_Click()
Dim ws1 as Worksheet: Set ws1 = Thisworkbook.Sheets("Sheet1")
Dim ws2 as Worksheet: Set ws2 = Thisworkbook.Sheets("Sheet5")
Dim LRow1 As Long, LRow2 as Long, i As Long
LRow1 = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row
LRow2 = ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If ws1.Cells(i, 1) = "pp" Then
ws1.Range(Cells(i, 1), Cells(i, 5)).Copy
ws2.Range("A" & LRow + 1).PasteSpecial xlPasteValues
End If
Next
End Sub
Here is a more effecient method using a For Each loop and one instance of Copy/Paste instead of 1 iteration for every matched cell.
Option Explicit
Sub Copy()
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")
Dim TargetRange As Range, TargetCell As Range, CopyRange As Range
Set TargetRange = ws1.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
For Each TargetCell In TargetRange
If TargetCell = "pp" Then
If CopyRange Is Nothing Then
Set CopyRange = TargetCell.Resize(1, 4)
Else
Set CopyRange = Union(CopyRange, TargetCell.Resize(1, 4))
End If
End If
Next TargetCell
CopyRange.Copy
ws2.Range("A" & ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValuesAndNumberFormats
End Sub
Another method would be to apply a filter for your target value pp and then copy/paste visible cells.

Range "Object Required"

I can't figure this out.
Sub Paste1()
Dim NextRow As Range
Set NextRow = Range("A" & Sheets("AMCurrent").UsedRange.Rows.Count + 1)
AMPaste.Range("A3:F3").Copy
AMCurrent.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
I get an "object error" on row 4, AMPaste.Range("A3:F3").Copy.
I'm assuming you didn't declare your Worksheets, and that you used their name instead of Sheets("SheetName"), but I believe this could be your solution:
Sub Paste1()
Dim NextRow As Long
Dim wsPaste As Worksheet: Set wsPaste = Sheets("AMPaste")
Dim wsCurrent As Worksheet: Set wsCurrent = Sheets("AMCurrent")
NextRow = wsCurrent.Cells(wsCurrent.Rows.Count, "A").End(xlUp).Row + 1
wsPaste.Range("A3:F3").Copy
wsCurrent.Range("A" & NextRow).PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
End Sub
I had the same problem. My worksheet was named "tagsListSheet" and in my script I had a Worksheet variable also named "tagsListSheet". My line of code that produced your error looked like:
Set wwTags = Range(tagsListSheet.Cells(2, 2), tagsListSheet.Cells(lastTagRow, 2))
and when I changed the name of the worksheet variable, it worked.
Set wwTags = Range(tagSheet.Cells(2, 2), tagSheet.Cells(lastTagRow, 2))

Resources