Excel VBA - variable for loop - vba

I can't seem to get my second for loop right. I'm looking for the cell with value 'Persoonlijke prijslijst'. Once I have this cell I need to go up two and delete 8 down. When I debug, it says temp = 0 so I presume it's in my second for loop.
Dim i As Integer
For i = 1 To 800
Range("C" & i).Select
If Range("C" & i).Value = "Persoonlijke prijslijst" Then
Dim temp As Integer
For temp = i - 2 To temp + 8
Range("C" & temp).EntireRow.Delete Shift:=xlToLeft
Next temp
End If
Next i

Another way without looping 800 times:
Sub testing()
Dim rng As Range
Dim fAddress As String
Dim rngRows As Range
With Sheet1.Range("C1:C800")
Set rng = .Find("Persoonlijke prijslijst")
If Not rng Is Nothing Then
fAddress = rng.Address
Do
If rngRows Is Nothing Then
Set rngRows = rows(rng.Row - 2 & ":" & rng.Row + 5)
Else
Set rngRows = Union(rngRows, rows(rng.Row - 2 & ":" & rng.Row + 5))
End If
Set rng = .FindNext(rng)
Loop While Not rng Is Nothing And rng.Address <> fAddress
End If
End With
rngRows.EntireRow.Delete
End Sub

Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim StrSearch As String
Dim i As Long
'~~> Change this to the relevant sheet name
Set ws = Sheets("Sheet1")
StrSearch = "Persoonlijke prijslijst"
With ws
For i = 800 To 1 Step -1
If .Range("C" & i).Value = StrSearch Then
.Rows(i - 2 & ":" & i + 5).Delete
End If
Next i
End With
End Sub

Related

Move Worksheet to String Variable

I am having trouble making a string variable equal to the cell in a worksheet since I get a type mismatch. I would also like to make a single string variable (SheetString) equal to all worksheet content. A portion of my code is below:
Range("A1").Select
Set sht = ThisWorkbook.Worksheets("Sheet3")
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Set rng = Range("A1:A" & LastRow).SpecialCells(xlCellTypeBlanks)
rng.EntireRow.Delete
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
'MsgBox (Continue)
Set rng = Range("A1:A" & LastRow)
'For x = 1 To LastRow
'RowString = cell(x, 1).Value
'if instr(,RowString,("Sheet1").
SheetString = Range("a1:a" & LastRow).Value
MsgBox (Continue)
IE.Quit
Thanks
you can't assign a string like this, if you want to asign the whole Range in Column A, you nned a loop, like this:
For i = 1 To LastRow
SheetString = SheetString & ";" & Range("A" & i).Value
Next i
See this example
Sub Sample()
Dim SheetString As String
LastRow = 12 '<~~ Example
SheetString = range("a1:a" & LastRow).Value
End Sub
You can't do this. You can store the entire column in an array though. For that you have to declare SheetString as Variant as shown below else you will get the Type Mismatch error as you are currently getting
Sub Sample()
Dim SheetString As Variant
LastRow = 12 '<~~ Example
SheetString = range("a1:a" & LastRow).Value
End Sub
This will create an array which you can loop to access
For i = LBound(SheetString) To UBound(SheetString)
Debug.Print SheetString(i, 1)
Next i

Type mismatch error in VBA

This code to is to search each element from column A in worksheet 6 to be existing in Column A in worksheet 3
Sub checkpanvalueS()
Dim lastRow1 As Long
Dim lastRow2 As Long
lastRow1 = Sheet3.Cells(Rows.Count, 1).End(xlUp).Row
lastRow2 = Sheet6.Cells(Rows.Count, 1).End(xlUp).Row
Dim myArr As Variant
'Dim myArr2 As Variant
'For i = 2 To lastRow1
'myArr(i) = Sheet3.Cells(i, 1)
myArr = Sheet3.Range("A2:A" & lastRow1)
'myArr2 = Sheet6.Range("A2:A" & lastRow2)
'Next i
' For i = 2 To lastRow1
For m = 2 To lastRow2
'if UBound(Filter(myArr, Sheet6.Cells(m, 1))) > -1 and then
' MsgBox "All Yellow highlighted pan number (Column A ) should not be one from ptimary Cards ."
' If UBound(Filter(myArr, myArr(i))) >= 0 And myArr(i) <> "" Then
' If IsInArray(Sheet6.Cells(m, 1), myArr) Then
If Filter(myArr, Sheet6.Cells(m, 1)) = "" Then
' MsgBox ("Search Term SUCCESSFULLY located in the Array")
Range("A" & m).Interior.Color = vbYellow
MsgBox (" These pan numbers should'nt be equal to existing primary cards")
End If
Next m
' Next i
End Sub
Try this code - you should use the Find method of the Range object to look for a specific value:
Public Sub HighlightItems()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rngSearch1 As Range
Dim rngSearch2 As Range
Dim rngCell As Range
Dim rngFound As Range
Set ws1 = ThisWorkbook.Worksheets("Sheet6")
Set ws2 = ThisWorkbook.Worksheets("Sheet3")
Set rngSearch1 = ws1.Range("A1:A" & ws1.Cells(Rows.Count, 1).End(xlUp).Row)
Set rngSearch2 = ws2.Range("A1:A" & ws1.Cells(Rows.Count, 1).End(xlUp).Row)
For Each rngCell In rngSearch1
Set rngFound = rngSearch2.Find(rngCell.Value)
If Not rngFound Is Nothing Then
rngCell.Interior.Color = vbYellow
Debug.Print ws1.Name & "!" & rngCell.Address & " equals " & ws2.Name & "!" & rngFound.Address
End If
Next
End Sub

Filter data and copy values VBA

My code below is supposed to filter data in the wsData and then copy it into the wsTest worksheet after each other in column A. The code works except that it copies the values over each on the destination sheet rather then after each other. Any idea why?
Sub PrintReport()
Dim wbFeeReport As Workbook
Dim wsData As Worksheet
Dim wsForm As Worksheet
Dim wsTest As Worksheet
Dim FrRngCount As Range
Dim i As Integer
Dim k As Integer
Dim t As Integer
Dim s As Integer
Set wbFeeReport = Workbooks("FeExcForm.xlsm")
Set wsData = wbFeeReport.Worksheets("Data")
Set wsTest = wbFeeReport.Worksheets("Test")
wsTest.Cells.Clear
wsData.Activate
i = 1
For k = 1 To 2
With ActiveSheet
.AutoFilterMode = False
With Range("A1", Range("A" & Rows.Count).End(xlUp))
.AutoFilter 1, k
On Error Resume Next
.SpecialCells(xlCellTypeVisible).Copy Destination:=wsTest.Range("A" & i)
End With
i = wsTest.Range("A" & Rows.Count).End(xlUp)
.AutoFilterMode = False
End With
Next k
End Sub
As first point: if using a range with AutoFilter the copy will always exclude the hidden cells
With Range("A1", Range("A" & Rows.Count).End(xlUp))
.AutoFilter 1, k
.Copy wsTest.Range("A" & i)
End With
is all you need here.
Regarding your error: On Error Resume Next hides the error of i = wsTest.Range("A" & Rows.Count).End(xlUp) which would return a range rather than a numerical value.
i = wsTest.Range("A" & Rows.Count).End(xlUp).Row + 1
is your friend here :)
Everything together should look something like that:
Sub PrintReport()
Dim wbFeeReport As Workbook
Dim wsData As Worksheet
Dim wsForm As Worksheet
Dim wsTest As Worksheet
Dim FrRngCount As Range
Dim i As Integer
Dim k As Integer
Dim t As Integer
Dim s As Integer
Set wbFeeReport = Workbooks("FeExcForm.xlsm")
Set wsData = wbFeeReport.Worksheets("Data")
Set wsTest = wbFeeReport.Worksheets("Test")
wsTest.Cells.Clear
wsData.Activate
i = 1
For k = 1 To 2
With wsData
.AutoFilterMode = False
With .Range("A1", Range("A" & Rows.Count).End(xlUp))
.AutoFilter 1, k
.Copy wsTest.Range("A" & i)
End With
i = wsTest.Range("A" & Rows.Count).End(xlUp).Row + 1
.AutoFilterMode = False
End With
Next k
End Sub
EDIT: For excluding headers just change:
.Copy wsTest.Range("A" & i)
to:
If i = 1 Then .Copy wsTest.Range("A" & i) Else .Offset(1, 0).Copy wsTest.Range("A" & i)
and if you do not want any headers at all then directly use:
.Offset(1, 0).Copy wsTest.Range("A" & i)
But I havent tested it. Just tell me if you get any problems ;)

Excel VBA delete entire row if both columns B and C are blank

I'm trying to delete an entire row in excel if column B and C are blank for that row. I have this vba code that deletes an entire row if the whole row is blank. How can I only delete the row if B and C have no value?
Thank you
Sub DeleteBlank()
Dim rng
Dim Lastrow As Integer
Set rng = Nothing
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
For Each i In Range("B1:B" & Lastrow)
If Application.CountA(i.EntireRow) = 0 Then
If rng Is Nothing Then
Set rng = i
Else
Set rng = Union(rng, i)
End If
End If
Next i
MsgBox (Lastrow)
If Not rng Is Nothing Then
rng.EntireRow.Delete
End If
End Sub
--Update--
The problem is solved. Thanks to izzymo and sous2817
Here is the current code
Sub DeleteBlank()
Dim i As Integer
Dim Lastrow As Integer
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
MsgBox (Lastrow)
For i = Lastrow To 2 Step -1
If Trim(Range("B" & i).Value) = "" And Trim(Range("C" & i).Value) = "" Then
Range("B" & i).EntireRow.Select
Selection.Delete
End If
Next i
MsgBox "Done"
End Sub
As asked for, here is a way to do it without looping:
Sub NoLoopDelete()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
With Sheet1.Range("A1:I" & lr)
.AutoFilter
.AutoFilter Field:=2, Criteria1:="="
.AutoFilter Field:=3, Criteria1:="="
.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilter
End With
End Sub
The results should be the same, but this way should be faster, especially if you have a lot of rows. Obviously, change the column reference to suit your layout and feel free to fancy it up w/ some error checking,etc.
Try this
Sub DeleteBlank()
Dim i as Integer
Dim Lastrow As Integer
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow
If Trim(Range("B" & i).Value) = "" And Trim(Range("CB" & i).Value) = "" Then
Range("B" & i).EntireRow.Select
Selection.Delete
i = i - 1
End If
Next i
MsgBox "Done"
End Sub

how to get a % value after matching same name from 2 different worksheets

I was wondering if someone can help me solve the following problem. Someone has previously helped me on get the % of dctest/In value on the same worksheet. But right now, i need to do the same thing but on a different worksheet.
Say Sheet1
this is copied Sheet1 (1) after taking the %
Sub marco1()
'start making Sheet1 into %
'~~> Add/Remove the text here which you want to ignore
Excludetext = "In,test1,test2,test3,test4,test5,test6"
MyArray = Split(Excludetext, ",")
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
'Set Column B into %
For i = 1 To LastRow
boolContinue = True
For j = 0 To UBound(MyArray)
SearchText = UCase(Trim(MyArray(j)))
If UCase(Trim(ws.Range("A" & i).Value)) = SearchText Then
boolContinue = False
Exit For
End If
Next j
If boolContinue = True Then
With Range("B" & i)
.Formula = _
"=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(A" & i & _
",$A$1:$A$45,0),1,0),1,1,1,'Duplicated_Sheet1')),0,1)/$B$5"
.NumberFormat = "0.00%"
End With
End If
Next i
End sub
There is some error showing at the formula, did i make a mistake with the formula? Thank you in advance!
Is this what you are trying?
TRIED AND TESTED
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim wsData As String
Dim SearchText As String, Excludetext As String
Dim LastRow As Long, i As Long, j As Long
Dim MyArray() As String
Dim boolContinue As Boolean
'~~> Add/Remove the text here
Excludetext = "In,Test1,Test2,Test3,Test4,Test5,Test6"
'~~> Change this to the relevant sheetname which has the data
wsData = "Sheet1"
MyArray = Split(Excludetext, ",")
Set ws = Sheets("Sheet2")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
boolContinue = True
For j = 0 To UBound(MyArray)
SearchText = MyArray(j)
If ws.Range("A" & i).Value = SearchText Then
boolContinue = False
Exit For
End If
Next j
If boolContinue = True Then
With ws.Range("B" & i)
.Formula = _
"=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(A" & i & _
"," & wsData & "!$A$1:$A$11,0),1,0),1,1,TRUE,""" & _
wsData & """)),0,1)/" & wsData & "!B1"
.NumberFormat = "0.00%"
End With
End If
Next i
End Sub
When using ADDRESS() for a cell in different sheet, you have to specify additional arguments.
Straight from Excel's help
Syntax of ADDRESS Function
ADDRESS(row_num, column_num, [abs_num], [a1], [sheet_text])
Where [sheet_text] is the name of the sheet which we are referring to. I would recommend reading more about it in Excel Help.
This is the actual formula for say dctest
=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(A7,Sheet1!$A$1:$A$11,0),1,0),1,1,TRUE,"Sheet1")),0,1)/Sheet1!B1
HTH
Sid