Range of object error coming up with this code (error 1004).
Can you please help me out debug this?
Sub ggg()
last = Sheet1.Cells(1, 1).End(xlDown).Row
last1 = Sheet2.Cells(1, 1).End(xlDown).Row
Sheet1.Range(Cells(1, 1), Cells(last, 2)).Copy Sheet2.Range(last1 + 1, 1)
End Sub
The problem is that this doesn't find the last used cell
last = Sheet1.Cells(1, 1).End(xlDown).Row
last1 = Sheet2.Cells(1, 1).End(xlDown).Row
It sometimes jumps to the very last cell of Excel and then you try to add +1 here
Sheet2.Range(last1 + 1, 1)
which fails because there is no additional cell after the very last cell.
Use
last = Sheet1.Cells(Sheet1.Cells.Rows.Count, 1).End(xlUp).Row
last1 = Sheet2.Cells(Sheet2.Cells.Rows.Count, 1).End(xlUp).Row
to find the last used cell.
Second problem is that Sheet2.Range(last1 + 1, 1) doesn't accept two numeric parameters (as YowE3K pointed out in the comment). Therefore use Sheet2.Cells(last1 + 1, 1)
VBA Best Practices include (1) to never assume the worksheet and (2) use fully qualified cell objects.
Related
Sample Data :
0votes
https://stackoverflow.com/questions/32382401/autohide-multiple-rows-in-excel/32383360#32383360
vbaexcel
answered Sep 3, 2015 at 18:53
0votes
Accepted
https://stackoverflow.com/questions/32273121/corretc-excel-vba-macro-to-return-the-values-as-text-and-as-date/32273219#32273219 'clickable format
vbaexcel
answered Aug 28, 2015 at 14:18
I want to insert a row between votes and url line if the answer is not accepted and url immediately follws votes line with a purpose to make grouping of 5 rows for ultimately transposing data in a single row.
Code use by me is as follows :
Sub insertrow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "A").Value) Like "*vote*" And (Cells(i + 1, "A").Value) <> "Accepted" Then
Cells(i + 1, "A").EntireRow.Insert
End If
Next i
End Sub
I am getting Run time error 13 Type mismatch on the following line although this program ran succesfully yesterday night for simmiliar data.
` If (Cells(i, "A").Value) Like "*vote*" And (Cells(i + 1, "A").Value) <> "Accepted" `
Any help shall be appreciated very much.
Did a quick test with the data and the code you provided and had no issues. However, there is a problem with the code: Although you assign a specific sheet to a variable, you continue to work with the ActiveSheet. It is likely that this sheet is not the sheet with your sample data and may contain data that lead to the type mismatch error.
When programming VBA in Excel, you should always qualify exactly on which worksheet you want to work. One way is to use a With-statement. Take care that before every instance of Cells, Range, Rows and Columns you add a dot - that tells VBA you are referring to the object of the With-statement.
Sub insertrow()
With ThisWorkbook.Sheets("Sheet2")
Dim Last As Long, i As Long
Last = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (.Cells(i, "A").Value) Like "*vote*" _
And (.Cells(i + 1, "A").Value) <> "Accepted" Then
.Cells(i + 1, "A").EntireRow.Insert
End If
Next i
End With
End Sub
Or, if you like that better:
Sub insertrow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
Dim Last As Long, i As Long
Last = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (ws.Cells(i, "A").Value) Like "*vote*" _
And (ws.Cells(i + 1, "A").Value) <> "Accepted" Then
ws.Cells(i + 1, "A").EntireRow.Insert
End If
Next i
End Sub
Errors like type mismatch usually can be revealed rather easy by checking the related data (in this case content of 2 cells) with the debugger.
I am fairly new in excel vba scenario. What I am trying to accomplish here in this macro is,
I have two sheet, two column, sheet1 Column A, sheet2 Column A, both have possible matches in column A. I am trying to find all the matches between two sheets and copy matched entire rows from sheet1 to exactly below matched rows in sheet two with the header of sheet1.
sheet1
Data-----------name
012-----------AAA
022-----------BBB
033-----------CCC
Sheet2
id-----------address
012-----------NYC
021-----------Philly
033-----------CT
Result
id-----------address
012-----------NYC
Data-----------name
012-----------AAA
021-----------Philly
033-----------CT
Data-----------name
033-----------CCC
The code I have so far only copying the first row, no idea how to fix it.
Sub oneMacro()
Dim lastrowone As Integer, lastrowtwo As Integer
lastrowone = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lastrowtwo = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrowone
For j = 2 To lastrowtwo
If Sheets("Sheet1").Cells(i, "A").Value = Sheets("Sheet2").Cells(j, "A").Value Then
Sheets("Sheet1").Cells(i, "A").EntireRow.Copy
Sheets("Sheet2").Cells(j, "A").Offset(1).Insert Shift:=xlDown
End If
Next j
Next i
End Sub
There are a couple of problems with your code. First off, to help you learn how you could trouble shoot this... First you would want to add some breakpoints, and setup a few watches. But you will see that your loops are setup perfecly at first, but do not adapt properly as you add data.
Pretty much your loop statement continues looping until the your hit lastrowtwo which at first is set for a value of 3 (based on your example above). Instead your code needs to add +1 each time you find a true result to the lastrowtwo variable. I have modified your code below to overcome this issue.
Another issue is that you are coping everything from one cell to another, then shifting it down. When doing this, you will comparing that next (which will come back as being a match). After a while you will see that this only will scan the first line item. To overcome this you can simply skip the next line in the loop check statement. You can do this by adding +1 to the j variable. See below for the modifications.
Sub oneMacro()
Dim lastrowone, lastrowtwo As Long
lastrowone = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lastrowtwo = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrowone
For j = 2 To lastrowtwo
If Sheets("Sheet1").Cells(i, 1).Value = Sheets("Sheet2").Cells(j, 1).Value Then
Sheets("Sheet1").Cells(i, 1).EntireRow.Copy
Sheets("Sheet2").Cells(j, 1).Offset(1).Insert Shift:=xlDown
j = j + 1 ' Modified = this must be added to overcome an issue with DOUBLE checking the newly inserted data
lastrowtwo = lastrowtwo + 1 ' Modified = This is added to overcome an issue with not completing all rows
End If
Next j
Next i
End Sub
I am working on some data organization and need to move information into a form. I have a column of IP addresses and some columns have more than one, or even none. I preferably need to be able to go through the column and keep the first 13 characters and remove everything after it, have 581 lines I need it to be able to run through. I think if I could get a VBA that will run through the column and find the first "," and remove that and anything after it will work. I tried this, but it did nothing.
Sub cleanup()
lastrow = Range("G581").End(xlUp).Row
For i = 2 To lastrow
If Len(Cells(i, 1)) > 13 Then
Cells(i, 1) = Left(Cells(i, 1), 14)
End If
Next i
End Sub
As mentioned before, you are counting the rows in column G, hopefully column G and Column A have the same number of rows.
I'm thinking column G may be the issue.
lastrow = Range("A581").End(xlUp).Row
For i = 2 To lastrow
If Len(Cells(i, 1)) > 13 Then
Cells(i, 1) = Left(Cells(i, 1), 14)
End If
Next i
Honestly there could be a few reasons that you're having trouble. The big one is that when you use the Left() function, you tell it to keep 14 characters instead of the 13 you said that you want!
Another option could be that excel thinks you are trying to do this operation on another sheet. To combat this one, I made a worksheet object and qualified all of the ranges mentioned just to be sure that excel knows what location you're talking about.
An (unlikely based on your description, but possible) problem could be with the way that you specify the lastrow. If you have a column of contiguous values (although your description would suggest you do not) and you specify the last row of that column with a value, the .End(xlUp) will actually trace back up to the last cell with no value which could be the first row - so the loop never even runs! You could avoid this just by changing it to lastrow = Range("G582").End(xlUp).Row (one row below what you know to be the last full row), but why bother when you can just let excel do the work for you like in my answer below.
Sub cleanup()
Dim ws As Worksheet
Set ws = sheets("Sheet1")
Dim lastRow As Long
lastRow = ws.Range("G" & rows.count).End(xlUp).row
Dim i As Long
For i = 2 To lastRow
If Len(ws.Cells(i, 1)) > 13 Then
ws.Cells(i, 1) = Left(ws.Cells(i, 1), 13)
End If
Next i
End Sub
I also made a version with the second option that you mentioned in the question about splitting each row by a comma ",".
Sub cleanup2()
Dim ws As Worksheet
Set ws = sheets("Sheet1")
Dim lastRow As Long
lastRow = ws.Range("G" & rows.count).End(xlUp).row
Dim i As Long
For i = 2 To lastRow
ws.Cells(i, 1) = Split(ws.Cells(i, 1), ",")(0) 'option 2
Next i
End Sub
Hope this solves your problem!
I am trying to create a rolling twelve month sum in excel using vb. The last column with data is dynamic so I created a variable to store that column number each time. I then tried to create a loop that would output the sum of the previous 12 cells and move to the next cell until it reached 12 calculation cells. But this is not working... Is there something obvious that I am doing wrong?? Thanks!
Sub OSR_ReportComplete()
Dim lCol As Long
Dim p As Integer
lCol = Cells(7, Columns.Count).End(xlToLeft).Column
For p = 0 To 12
Range(Cells(15, lCol - p)).Value = Application.Sum(Range(Cells(7, lCol -
p), Cells(7, lCol - p - 12)))
Next p
End Sub
Could be done in a neater way but this works
Sub OSR_ReportComplete()
Dim lCol As Long
Dim p As Integer
lCol = Cells(7, Columns.Count).End(xlToLeft).Column
For p = 0 To 12
Cells(15, lCol - p) = Application.Sum(Range(Cells(7, lCol - p), Cells(7, lCol - p - 12)))
Next p
End Sub
Also, If your lCol evaluates to a value less then 25 it will fall over though due to trying to reference a column less than 1. I'd recommend using Offset instead
However, a rolling total can be done very easily in excel formulas also. Say your first value is in Column A and your date is in row 1, in row 2 enter in Column A =SUM($A$1:A1) and drag along the rest of your range
Giving:
Formulas:
The following code will generate the formulas above for row 1 in one line instead of doing your loop. This is an example as I don't understand the layout of your sheet from your code
Sub OSR_ReportComplete2()
With Range(Cells(1, 1), Cells(1, Columns.Count).End(xlToLeft))
.Offset(1, 0).Formula = "=SUM(" & .Cells(1).Address & ":" & Split(.Cells.Address(RowAbsolute:=False, ColumnAbsolute:=False), ":")(0) & ")"
End With
End Sub
This is easier to write in R1C1 notation (however will be displayed in A1 in Excel Sheet)
Sub OSR_ReportComplete2()
With Range(Cells(1, 1), Cells(1, Columns.Count).End(xlToLeft))
.Offset(1, 0).FormulaR1C1 = "=SUM(" & .Cells(1).Address(ReferenceStyle:=False) & ":R[-1]C)"
End With
End Sub
First of all, I suggest you don't use VBA for this. Instead, use the inbuilt OFFSET function, like this.
Your code is quite complex, but maybe you should be using xlToRight instead of xlToLeft to find the last column.
If you want to use VBA, you can try using WorkSheetFunction.offset, putting it into a range variable and then summing that range.
I was trying to do an automation and i was stuck here, where i need to add sum formula dynamically in between the space ranges. I'm completely lost here for adding formula using VBA can anyone help me out.
Thank you in advance :)
I'm assuming what you want is if there is a blank in a cell, you want all of the other elements summed and the result placed in that blank. There are probably any number of ways to code this, but here is my attempt
Sub formulateSubtotals()
finalRow = Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
finalCol = Cells(1, Worksheets("Sheet1").Columns.Count).End(xlToLeft).Column
For j = 1 To finalCol
For i = finalRow + 1 To 1 Step -1
If IsEmpty(Cells(i, j)) Then
If IsEmpty(Cells(i - 2, j)) Then
firstRow = i - 1
lastRow = firstRow
Else
lastRow = i - 1
firstRow = Cells(i - 1, j).End(xlUp).Row
End If
Cells(i, j) = Application.WorksheetFunction.Sum(Range(Cells(firstRow, j), Cells(lastRow, j)))
End If
Next i
Next j
End Sub
This also assumes that the sheet in question is entitled "Sheet1".
Without VBA:
Say we have data in column A like:
and we want to calculate of sum of each of the blocks separated by a blank cell. In B2 enter:
=IF(A2<>"","",SUM($A$1:A2)-SUM($B$1:B1))
and copy down:
If this is of no value, I will delete the answer.