DoEvents
Dim lastrow As Long
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
Range("A3:H" & lastrow).Sort key1:=Range("H3:H" & lastrow), _
order1:=xlAscending, Header:=xlNo
I want to sort my table by the dates in row H, but it give me the error messages "Run-time error '1004': Sort method of Range class failed" and it points to the last row of code. Thanks in advance
Try changing lastrow = Cells(Rows.Count, 2).End(xlUp).Row to lastrow = Cells(Rows.Count, 8).End(xlUp).Row
Related
I'm trying to use a vlookup in sheets("formula").range("D3") and autofill it to all cells between D3 and the cell in the last row and last column in sheets("formula"). The lookup values are in sheets("combined") which has a dynamic range in terms of the number or rows. Here is my code so far:
Sub Vlookup ()
Dim lastcol As Integer
With Sheets("Formula")
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
Dim lastrow As Long
With Sheets("Formula")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Dim comlast As Long
With Sheets("Combined")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Sheets("Formula").Range("D2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC3&"" | ""&R1C,Combined!R2C3:R" & comlast & "C4, 2, FALSE),"" "")"
Sheets("Formula").Range("D2").AutoFill Destination:=Sheets("Formula").Range("D2:D" & lastrow), Type:=xlFillDefault
Sheets("Formula").Range("D2:D" & lastrow).AutoFill Destination:=Sheets("Formula").Range(Cells(2, "D"), Cells(lastrow, lastcol)), Type:=xlFillDefault
End Sub
I'm getting a run-time error '1004': application-defined or object-defined error.
Any assistance would be appreciated
I would change the following:
Dim comlast As Long
With Sheets("Combined")
Comlast = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
And
With Sheets("Formula")
.Range("D2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC3&"" | ""&R1C,Combined!R2C3:R" & comlast & "C4, 2, FALSE),"" "")"
.Range("D2").AutoFill Destination:=.Range("D2:D" & lastrow), Type:=xlFillDefault
.Range("D2:D" & lastrow).AutoFill Destination:=.Range(Cells(2, "D"), .Cells(lastrow, lastcol)), Type:=xlFillDefault
End With
I'm trying to clculate a formula that takes number from Column D and calcultes this number minus 14 in column C.
Then, I'm triyng to autofill the range down to the first cell I calculate.
at first it was working, but now it shows me an error:
appliction-defined or object-defined error
to this line of code
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow)
If someone would help me to solve this prblem I'll be glad.
OP_wb.Sheets("Optic Main").Activate
Dim FirstRow As Range
Dim lastrow As Range
Set FirstRow = Range("C1").End(xlDown).Offset(1, 0)
Set lastrow = Range("E1").End(xlDown).Offset(0, -2)
Range("E1").End(xlDown).Offset(0, -2).Select
Range(FirstRow, lastrow).FormulaR1C1 = "=(c4-14)"
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow)
Instead of
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow)
try
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow.Row)
I have an issue. I am filtering from a big product table (with about 32'000 rows) the products that I need and sort it first by the product name and then by the delivery number.
My issue is, that if I wanna filter and sort numbers as product names (which is also possible), it does not sort correctly (see picture).
Picture of the filtered and sorted table
My code is this one:
'*************************************************************
'Filter Product Code
Set myrange = Range("A1:A" & lastrow)
For Each c In myrange
If Len(c.Value) <> 0 Then
ThisWorkbook.Worksheets(Worksheets.Count).Columns("A:D").AdvancedFilter xlFilterCopy, _
Sheet1.Range("A1:A" & lastrow), Sheet1.Range("G1"), False
End If
Next
'Sheet1.Range("51:100").RemoveDuplicates Columns:=3, Header:=xlYes
'*******************************************************************
'Sort the filtered list first by Product Code then by the Delivery number
Dim lngRow As Long
Dim lngRowMax As Long
Dim wsf As WorksheetFunction
With Sheet1
lastrow = Cells(Rows.Count, 8).End(xlUp).Row
Range("G2:J" & lastrow).Sort Key1:=Range("G2:G" & lastrow), _
Order1:=xlAscending, Key2:=Range("I2:I" & lastrow), _
Order1:=xlAscending, Header:=xlNo
Set wsf = Application.WorksheetFunction
lngRowMax = .UsedRange.Rows.Count
End With
How can I proceed to have it sorted correctly. First the same product codes, then by the delivery number (ascending) as I want to sell the oldest products first.
Thank you for your reply.
Now I found the solution for it. Only a small adjustment. The code for the sorting looks as follows now:
Dim lngRow As Long
Dim lngRowMax As Long
Dim wsf As WorksheetFunction
With Sheet1
lastrow = Cells(Rows.Count, 8).End(xlUp).Row
Range("G1:J" & lastrow).Sort Key1:=Range("G1:G" & lastrow), _
Order1:=xlAscending, Key2:=Range("I1:I" & lastrow), _
Order2:=xlAscending, Header:=xlYes, DataOption1:=xlSortTextAsNumbers
Set wsf = Application.WorksheetFunction
lngRowMax = .UsedRange.Rows.Count
End With
I have the following partial code and my for loop breaks ONLY on the 35th tab when I try to activate. The code runs correctly for sheets 6-33. I deleted sheet 34 so I'm thinking it might be because it "jumps" to sheet 35. However, when I debug and place the cursor over endTab I see it contains the value 35. The activate for some reason doesn't work only on this sheet and breaks. Any thoughts?
Dim lastRow As Long
Dim startRow As Long
Dim currentRow As Long
Dim endTab As Integer
Sheets(4).activate
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
startRow = 2
Range("A2:AB" & lastRow).Sort key1:=Range("AB2:AB" & lastRow), _
order1:=xlAscending, Header:=xlNo
For i = startRow To lastRow
Sheets(4).activate
endTab = Range("AB" & startRow + i - 2)
Range("A" & startRow + i - 2 & ":" & "AB" & startRow + i - 2).Copy
Worksheets(endTab).activate
Columns("A").Find("", Cells(Rows.Count, "A")).PasteSpecial xlPasteValues
Next
From comments:
Sheet code names are created when the sheet is first created - they do not adjust to match the order of the sheets in the workbook. Your code is addressing "the 35th sheet" (counting your sheet tabs from left to right) which is not the necessarily the sheet with the codename of "Sheet35"
VBA is throwing the above given error on the line Sheets("Sheet1").Range("A" & i).Copy Destination:=Sheets("Sheet2").Range("A" & i & "A" & LastCol - 1)
What I am trying to do is actually to copy the "A" & i cell (in first iteration it's A2) to a range in the second worksheet named Sheet2.
Sub FindFill()
Dim DatesRange As Range
Dim i As Integer
Dim TransposeThis As Range
Dim LastCol As Integer
If WorksheetFunction.CountA(Cells) > 0 Then
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
End If
With Sheets("Sheet1")
Set DatesRange = Range("B2" & LastCol)
End With
i = 1
Do While i <= ActiveSheet.Rows.Count
Sheets("Sheet1").Range("A" & i + 1).Copy Destination:=Sheets("Sheet2").Range("A" & i & "A" & LastCol - 1)
i = i + 1
Loop
End
End Sub
You are missing a ":" before "A"
Range("A" & i & ":A" & LastCol - 1)
FOLLOWUP
After I went through your comments, I saw lot of errors in your code
1) You have dimmed i as Integer. This can give you an error in Excel 2007 onwards if your last row is beyond 32,767. Change it to Long I would recommend having a look at this link.
Topic: The Integer, Long, and Byte Data Types
Link: http://msdn.microsoft.com/en-us/library/aa164754%28v=office.10%29.aspx
Quote from the above link
Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647
2) You are finding the Last Column But in Which Sheet? You have to fully qualify the path Like this.
If WorksheetFunction.CountA(Sheets("Sheet1").Cells) > 0 Then
LastCol = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
End If
Same is the case with
With Sheets("Sheet1")
Set DatesRange = Range("B2" & LastCol)
End With
You are missing a DOT before Range
This is the correct way...
.Range("B2....
Also Range("B2" & LastCol) will not give you the range that you want. See the code below on how to create your range.
3) You are using a variable LastColumn but using LastCol. I would strongly advise using Option Explicit I would also recommend having a look at this link (SEE POINT 2 in the link).
Topic: To ‘Err’ is Human
Link: http://www.siddharthrout.com/2011/08/01/to-err-is-human/
4) What happens if there .CountA(Sheets("Sheet1").Cells) = 0? :) I would suggest you this code instead
If WorksheetFunction.CountA(Sheets("Sheet1").Cells) > 0 Then
LastCol = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Else
MsgBox "No Data Found"
Exit Sub
End If
5) ActiveSheet.Rows.Count will not give you the last active row. It will give you the total number of rows in that sheet. I would recommend getting the last row of Col A which has data.
You can use this for that
With Sheets("Sheet")
LastRow =.Range("A" & .Rows.Count).End(xlup).row
End With
Now use LastRow instead of ActiveSheet.Rows.Count You also might want to use a For Loop so that you don't have to increment i every time. For example
For i = 1 to LastRow
6) Finally You should never use End. Reason is quite simple. It's like Switching your Computer using the POWER OFF button. The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Also the Object references held (if any) by other programs are invalidated.
7) Based on your image in Chat, I believe you are trying to do this? This uses a code which doesn't use any loops.
Option Explicit
Sub FindFill()
Dim wsI As Worksheet, wsO As Worksheet
Dim DatesRange As Range
Dim LastCol As Long, LastRow As Long
If Application.WorksheetFunction.CountA(Sheets("Sheet1").Cells) = 0 Then
MsgBox "No Data Found"
Exit Sub
End If
Set wsI = Sheets("Sheet1")
Set wsO = Sheets("Sheet2")
With wsI
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set DatesRange = .Range("B1:" & Split(Cells(, LastCol).Address, "$")(1) & 1)
.Columns(1).Copy wsO.Columns(1)
DatesRange.Copy
wsO.Range("B2").PasteSpecial xlPasteValues, _
xlPasteSpecialOperationNone, False, True
.Range("B2:" & Split(Cells(, LastCol).Address, "$")(1) & LastCol).Copy
wsO.Range("C2").PasteSpecial xlPasteValues
End With
End Sub