loop from cell i to last cell non-empty cell - excel

As the title says, I want to loop from cell i to the last non-empty cell. But I keep getting a type mismatch error even though I am declaring it the same type.
Dim wsList as Worksheet
Dim wsCode as Worksheet
Dim i as Variant
Dim j as Long
Dim LastCell as Variant
Dim LastCell2 as Long
With wsList
LastCell = .Cells(.Rows.Count, "G").End(xlUp)
LastCell2 = .Cells(.Rows.Count, "F").End(xlUp)
End With
For i = 1 To LastCell
wsList.Range("G2").Offset(i - 1).Copy _
wsCode.Range("C2").Offset((i - 1) * 14)
Next i
For j = 1 To LastCell2
wsCode.Range("F11").Offset((j - 1) * 14).Value = _
wsList.Range("F2").Offset(j - 1).Value
Next j
End Sub
Mismatch error occurs between LastCell and i even though I declared them both as variant. I also tried declaring them as string but still getting the same error. The data type in that particular column is "AB12345" including quotation marks. How can I fix this?
Thanks in advance!

As stated in the comments, you're missing a .row. It would be best to define all your row variables as Long. One other thing to consider is that your method of using end(xlUP) will not account for any rows that are hidden. This answer discusses a variety of ways to address this.
With wsList
LastCell = .Cells(.Rows.Count, "G").End(xlUp).row
LastCell2 = .Cells(.Rows.Count, "F").End(xlUp).row
End With

I think you need to change
Dim LastCell as Variant
To
Dim LastCell as Long
Also, you should clearly identify the sheets for wsList and wsCode.

Related

VBA - Remove cell that contains word from same column

I've seen similar posts out there but not quite the same and seem to be confused on the results I'm getting...
I essentially need to de-dupe a column on LIKE words, so it's somewhat straightforward but apparently not as easy as I thought.
I have a dataset like soo...
When I run my macro it removes rows (as I intended), but doesn't seem to remove all the rows or the wrong rows...
It actually removes the highlighted/yellow rows
I was thinking it should actually remove something like the bottom rows.. where it would keep "aerospace" but remove "aerospace 2019", since the 2019 is kinda redundant and not applicable to me.
My macro is simple, but I thought it would do the trick... what am I doing wrong?
Sub container()
Dim ws As Worksheet, rw As Long, col As Long, i As Long
Set ws = ActiveSheet 'or whatever
i = 2
'For col = 2 To 5 'placeholder in case multiple columns are needed - remove Set col above
For rw = 2 To ws.Cells(Rows.Count, 1).End(xlUp).Row 'from row 1 til last non-empty row
v = ws.Cells(rw, 2).Value 'set range
If Cells(i, 2).Value Like v Then 'determine if the cell contains the value of the word
Cells(i, 2).EntireRow.Delete 'delete
i = i + 1
End If
Next rw
'Next col
End Sub
After Ron's post I was able to create the below, but appears I'm still stuck. I think I've just been looking at this too long.
Sub container()
Dim ws As Worksheet, rng As Range, i As Long, rw As Long
Set ws = ActiveSheet 'or whatever
Set rng = ws.Range("B2:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row) 'set array range
i = Range("B" & Rows.Count).End(xlUp).Row
For rw = ws.Cells(Rows.Count, 1).End(xlDown).Row To 2
v = ws.Cells(rw, 2).Value
If InStr(1, v, rng) > 0 Then
cell.EntireRow.Delete
i = i - 1
End If
Next rw
End Sub

Excel VBA - Delete Rows on certain conditions

If a row has the value INACTIVE in column D and #N/A in column H, I want to delete that row.
I tried to achive that with my code below, but no row actually gets deleted.
Dim ws3 As Worksheet
Dim r As Integer
Set ws3 = ThisWorkbook.Sheets("Sheet2")
With ws3
For r = Sheet2.UsedRange.Rows.Count To 2 Step -1
If Cells(r, "D").Value = "INACTIVE" And Cells(r, "H").Value = "#N/A" Then
Sheet2.Rows(r).EntireRow.Delete
End If
Next
End With
Several issues.
You don't properly qualify your range objects.
You (properly) use With ws3 but then never refer back to it
If the #N/A is an actual error value, and not a text string, your macro will fail with a type mismatch error.
If the first row of UsedRange is not row 1, then rows.count.row will not reflect the last row
r should be declared as Long, not Integer.
Integer is limited to 32768 and there could be many more rows in a worksheet.
VBA will convert Integer to Long internally anyway.
Also, as pointed out by #FoxfireAndBurnsAndBurns Sheets("Sheet2") may not be the same as Sheet2. You seem to be using them interchangeably in your code. Set ws3 to whichever one you really want. And examine vba HELP for CodeName to understand the difference.
The following modification of your code may work:
Option Explicit
Sub due()
Dim ws3 As Worksheet
Dim r As Long
Dim lastRow As Long
Set ws3 = ThisWorkbook.Sheets("Sheet2")
With ws3
lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
For r = lastRow To 2 Step -1
If .Cells(r, "D").Value = "INACTIVE" And .Cells(r, "H").Text = "#N/A" Then
.Rows(r).EntireRow.Delete
End If
Next
End With
End Sub

How to set up an Array formula using Index/match in VBA to get first match cell

I'm trying to set up array formula within a for loop to do a partial match of a string to a list of names on another worksheet called Project Name. This should be the end product I got the formula working in the spreadsheet using the method from exceljet but I ran into an error of "object required" when I tried to covert it to VBA. The cells(i,6) is the location of the string that I am trying to do a partial match to the project names. The column doesn't have to be "6", it is where the Please help. Thanks!
Sub Shortname()
Dim SRng As Variant
Dim SName As Integer
Dim SNrow As Integer
Dim PLcol As Integer
Dim PLrow As Integer
Worksheets(3).Activate
SNrow = Cells(Rows.Count, 1).End(xlUp).Row
SRng = Range(Cells(2, 1), Cells(SNrow, 1)).Value
Worksheets(2).Activate
PLcol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
PLrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To PLrow
Cells(i, PLcol).Value = Application.WorksheetFunction.Index(SRng, Application.WorksheetFunction.Match("TRUE", Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Search(SRng.Value, Cells(i, 6))), 0), 1)
Next i
End Sub
Mysterious Variable (SRng)
I would write the code something like this (not the solution just a more readable version):
Sub Shortname()
Dim SRng As Variant
Dim SName As Integer
Dim SNrow As Integer
Dim PLcol As Integer
Dim PLrow As Integer
'Missing Declaration
Dim i As Long
'To avoid jumping around worksheets do NOT use Activate or Select
With Worksheets(3)
SNrow = .Cells(Rows.Count, 1).End(xlUp).Row
SRng = .Range(Cells(2, 1), Cells(SNrow, 1)).Value
End With
With Worksheets(2)
PLcol = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
PLrow = .Cells(Rows.Count, 1).End(xlUp).Row
End With
With Application.WorksheetFunction
For i = 2 To PLrow
'Run-time error 424: Object required
Worksheets(2).Cells(i, PLcol).Value = .Index(SRng, .Match("TRUE", _
.IsNumber(.Search(SRng.Value, Cells(i, 6))), 0), 1)
Next i
End With
End Sub
The error (mystery) lies in the SRng variable. Why is it declared as variant?
If it is a String declare it as String and change the line SRng = .Range(Cells(2, 1), Cells(SNrow, 1)).Value to
SRng = .Range(Cells(2, 1), Cells(SNrow, 1)).Address
If it is a Range object then declare it as Range and remove .Value,
but then you still get the error in the For Next loop since you use SRng.Value in the Search part of the statement, but a range has no value (maybe you wanted to use something like this SRng.Cells(i, 6).Value).
If this doesn't help you provide some more info like a sample of the worksheet(s) to see what's in the cells, ranges ... and explain what it is you're searching for in the For Next loop.

VBA iterate drop down list

I have a spread sheet with J1 being a drop down list. Content in row 8-14 would change based on what you choose in J1. I need to iterate through all values in the drop down list and copy all the corresponding rows to a new workbook. the copy paste part is working, but I am having problem iterating through the drop down list. particularly, I need some help to define Formula1. I am using excel 2010. Here is my code. Thanks in advance
Sub iterate_dropdown()
Dim inputRange As Range
Dim c As Range
Dim Current As Range
Set inputRange = Evaluate(Workbooks("sample.xlsm").Worksheets("Credit Research Journal").Range("J1").Validation.Formula1)
For Each c In inputRange
Workbooks("sample.xlsm").Worksheets("Credit Research Journal").Range("J1").Value = c.Value
Workbooks("sample.xlsm").Sheets("Credit Research Journal").Activate
Workbooks("sample.xlsm").RefreshAll
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
Cells(8, 1).Resize(FinalRow - 7, 10).Copy
Workbooks("Book2.xlsm").Sheets("Sheet3").Activate
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Set Current = Cells(NextRow, 1)
Current.PasteSpecial xlPasteValues
Next c
End Sub
I did some experimenting with the code you posted, and it seems you were pretty close. I discovered that the step where you were getting inputRange was including an equals sign (=), which then caused the Evaluate function to fail.
Here is the code I used:
Sub iterate_dropdown()
Dim inputRange As Range
Dim c As Range
Dim Current As Range
Dim strRange As String
Dim strRange2 As String
strRange = Worksheets("Credit Research Journal").Range("J1").Validation.Formula1
strRange2 = Replace(strRange, "=", "") 'Get rid of the equals sign
Set inputRange = Evaluate(strRange2)
For Each c In inputRange
Workbooks("sample.xlsm").Worksheets("Credit Research Journal").Range("J1").Value = c.Value
Workbooks("sample.xlsm").Sheets("Credit Research Journal").Activate
Workbooks("sample.xlsm").RefreshAll
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
Cells(8, 1).Resize(FinalRow - 7, 10).Copy
Workbooks("Book2.xlsm").Sheets("Sheet3").Activate
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Set Current = Cells(NextRow, 1)
Current.PasteSpecial xlPasteValues
Next c
End Sub
I used two string variables strRange and strRange2 so that you can easily step through with the debugger and see what is happening.
Also I am assuming that your dropdown list is being populated by values that are references to other cells.

Copy data up to last used column with vba

I was successfully able to copy data up to the last used row using VBA. I am trying to do the same thing but copy data from A1 to LastColumn2. Here is the code I have put together thus far:
Sheets("Results").Select
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Range("A1:" & LastColumn & "2").Select
Selection.Copy
The debugger highlights the third line. This is just a portion of the code - All of the variables have been dimensioned properly.
You are getting the error because LastColumn is number. You want the string equivalent of it i.e the column name. For Further Reading
Avoid the use of .Select and fully qualify your objects. INTERESTING READ
Is this what you are trying?
Sub Sample()
Dim ws As Worksheet
Dim rng As Range
Dim LastCol As Long
Dim LastColumn As String
Set ws = ThisWorkbook.Sheets("Results")
With ws
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'~~> Return column name from number
LastColumn = Split(.Cells(, LastCol).Address, "$")(1)
Set rng = .Range("A1:" & LastColumn & "2")
Debug.Print rng.Address
rng.Copy
End With
End Sub
The problem is that the range you are passing is wrong because it is wating simething like:
Range("A1:C2").Select
and you are passing:
Range("A1:32").Select
So what you can do is:
Range(cells(1,1),cells(2,lastcolumn)).Select
Cell(1,1) = A1 beacuse its is row number 1 column number 1
As mentioned it is better if you just
Range(cells(1,1),cells(lastcolumn,2)).copy
Hope it helps

Resources