Hiding rows using VB - vba

My goal is to hide each row that contains a cell filled with red. Here is my code:
Sub AstInv()
Dim myTable As ListObject
Dim myArray As Variant
Set myTable = ActiveSheet.ListObjects("Sheet2")
Set myArray = myTable.ListColumns("Código de Barras2").Range
For Each cell In myArray
If Rng.Interior.Color = vbRed Then
rw.EntireRow.Hidden = True
End If
Next
End Sub
Each time I run it, I get this error:
Compile error: Invalid outside procedure
Please help! Thanks!

Your code is a bit disjointed and attempts to use variables that have not been dimmed or instantiated.
Sub AstInv()
Dim myTable As ListObject
Dim myArray As range
Set myTable = ActiveSheet.ListObjects("Table2")
Set myArray = myTable.ListColumns("Código de Barras2").Range
For Each cell In myArray
If cell.Interior.Color = vbRed Then
cell.EntireRow.Hidden = True
End If
Next
End Sub

There are several issues with your code:
Compile error: Invalid outside procedure
means that you have some random text at the top of your module (outside all Subs), as in:
random ' <- Invalid outside procedure
Sub AstInv1()
Dim myTable As ListObject
Dim myArray As Variant
...
But there are more issues:
It is unlikely that you have a table named "Sheet2"
Your ActiveSheet (the sheet currently visible) may not contain the intended table / ListObject
myArray is declared as Variant when it should be Range
Rng and rw objects are undeclared and, as mentioned, are not connected to any other objects
To fix them
Use Option Explicit - this will force you to declare all variables
Check your objects' names and location
In the code call your objects explicitly
Option Explicit
Public Sub AstInv()
Dim myTable As ListObject
Dim myCol As Range
Dim cell As Range
Set myTable = ThisWorkbook.Worksheets("Sheet2").ListObjects("Table1") 'Check names
Set myCol = myTable.ListColumns("Código de Barras2").Range
For Each cell In myCol
cell.EntireRow.Hidden = (cell.Interior.Color = vbRed)
Next
End Sub
This version uses an AutoFilter and shows how to avoid errors when objects are missing
Option Explicit
Public Sub AutoFilterRedCells()
Dim tbl As ListObject, col As Long
On Error Resume Next 'Ignore expected errors
Set tbl = ThisWorkbook.Worksheets("Sheet2").ListObjects("Table1") 'Check names
col = tbl.ListColumns("Código de Barras2").Range.Column
On Error GoTo 0 'Stop ignoring errors
If col > 0 Then tbl.Range.AutoFilter Field:=col, Operator:=xlFilterNoFill
End Sub

Related

Delete table name in Excel VBA

I would like to remove/delete the name of my table which is "Table2" using Excel VBA. I know how to remove the name by hand, but I cannot figure out how to set the name "Table2" as a name in VBA. I found other questions on this topic but those codes deleted all named ranges and I would like to remove just the table named "Table2".
This is my code that is not working:
Sub Delete_Name_Table()
Dim n As Name
n = "Table2"
n.Delete
End Sub
Anyone who knows how to set n correctly to "Table2"?
Thanks!
You can backup data from table, delete table and restore data.
Sub test()
Dim rng As Range
Dim rngVals As Variant
Set rng = YourSheet.ListObjects("Table2").Range
rngVals = rng.Value
YourSheet.ListObjects("Table2").Delete
rng.Value = rngVals
Set rng = Nothing
End Sub
I hope I understood your post, and you want something like the code below:
Option Explicit
Sub Delete_Name_Table()
Dim Sht As Worksheet
Dim Tbl As ListObject
Set Sht = Worksheets("Sheet1") ' <-- change to your sheet that has the table
' set the Table Object
Set Tbl = Sht.ListObjects("Table2")
Tbl.Name = " " ' clear the name, you need to give it some kind of name
End Sub
There is a good way to make a table to a range. Thus, the name is removed automatically, but the value stays. Like this:
Public Sub TestMe()
Dim tblN As Object
Dim rngRange As Range
For Each tblN In ActiveSheet.ListObjects
Debug.Print tblN
If tblN = "Tabelle16" Then
tblN.Unlist
End If
Next tblN
End Sub
If you want to remove the data within as well, here is the code:
Public Sub TestMe()
Dim n As String
n = "Tabelle2"
Range(n).Delete
End Sub
It will set n to a Table name and it will delete it.

Extracting the item from a DICTIONARY in VBA

I have created a dictionary that stores the sheet name and its number.
So the code is as:
Sub SetDiction()
Dim num as Excel.Range
Dim wks As Excel.Worksheet
For each wks in ThisWorkbook.Worksheets
Set num = Nothing
Set num = wks.Range("SheetNumber")
If not(num is Nothing) Then
rModule.rDictionary.Add:=num.Value Item:=wks
End If
Next
End Sub
Now am trying to get the wks name and put it in another worksheet.The code is:
Sub GetSheet()
Dim key as variant
For each key in rModule.rDictionary.Keys
With Sheet2
.Cells(2,1).Value = rModule.rDictionary.Item(key)
End With
End Sub
It is giving me application defined or Object-defined error.
Can some one help please?
Your .Add call is syntactically incorrect and you are adding the whole worksheet to the dictionary where presumably you just want to add the named range, change to:
rDictionary.Add num.Value, num
To add the name:
rDictionary.Add num.Value, wks.Name

Type Missmatch when copy/pasting to visible cells only

I'm trying to copy and paste a list into visible cells only. For some reason I'm getting a type miss-match error and I don't understand why. When debugging the error occurs on the third line.
Sub Copy_Filtered_Cells()
Set from = Sheets(Sheet2).Range("I16831:I20610")
Set too = Application.InputBox("J4:J16821", Type:=8)
For Each Cell In from
Cell.Copy
For Each thing In too
If thing.EntireRow.RowHeight > 0 Then
thing.PasteSpecial
Set too = thing.Offset(1).Resize(too.Rows.Count)
Exit For
End If
Next
Next
End Sub
Best to use Option Explicit at top of module, I am guessing at what you are trying to achieve. Here is a stab...
Option Explicit
Sub Copy_Filtered_Cells()
Dim from As Excel.Range
Set from = Sheets("Sheet2").Range("I16831:I20610")
Dim too As Excel.Range
Set too = Sheets("Sheet2").Range("J4:J16821") 'Application.InputBox("J4:J16821", Type:=8)
Dim Cell As Excel.Range
For Each Cell In from
Cell.Copy
Dim thing As Excel.Range
For Each thing In too
If thing.EntireRow.RowHeight > 0 Then
thing.PasteSpecial
Set too = thing.Offset(1).Resize(too.Rows.Count)
Exit For
End If
Next
Next
End Sub

Changing value of TextBox due to selection of ComboBox VBA Excel

I have a project in which I have to change to value of a textbox to a value that is searched in a workseet against a vlaue that has been selected from a combobox. for example if I select "A" from the combobox the it should search the worksheet "test" find the input for A and change the text box value to 1 as this is the value entered for A. I have looked at some of the other questions that have been asked here but could not seem to get it to work for me. Below is the code that I have been trying to use.
Private Sub IDComboBox_Change()
Dim domainRange As Range
Dim listRange As Range
Dim selectedString As Variant
Dim lastRow As Long
If IDComboBox.ListIndex <> -1 Then
selectedString = IDComboBox.Value
lastRow = Worksheets("test").Range("A" & Rows.Count).End(xlUp).Row
Set listRange = Worksheets("test").Range("A2:A" & lastRow)
For Each domainRange In listRange
If domainRange.Value = selectedString Then
DomainOwnerTestBox.Value = "test"
End If
Next domainRange
End If
End Sub
Any help would be great. If you need anymore information then please let me know and also please be paient with me as im new to VBA.
Thanks
Try this code. It uses Excel built-in MATCH function to search for value in column A of worksheet 'test'.
Private Sub IDComboBox_Change()
Dim wks As Excel.Worksheet
Dim selectedString As Variant
Dim row As Long
Dim value As Variant
Set wks = Worksheets("test")
If IDComboBox.ListIndex <> -1 Then
selectedString = IDComboBox.value
On Error Resume Next
row = Application.WorksheetFunction.Match(selectedString, wks.Columns(1), 0)
On Error GoTo 0
If row Then
value = wks.Cells(row, 2) '<--- assuming that input values are in column 2.
DomainOwnerTestBox.value = value
Else
'Value not found in the worksheet 'test'
End If
End If
End Sub

error 91 object variable or with block variable not set on range

I'm new to VBA as was trying to write a macro that finds duplicates in different columns from a worksheet. I found the answer here helpful. However this solved it only for one column. So to add a few more columns I altered the code as follows
Sub test()
Dim iWarnColor As Integer
Dim rng As Range
Dim rngCell As Variant
Dim iWarnColor1 As Integer
Dim rnga As Range
Dim rngCell1 As Variant
Set rng = Range("A1:A17") ' area to check '
iWarnColor = xlThemeColorAccent2
For Each rngCell In rng.Cells
vVal = rngCell.Text
If (WorksheetFunction.CountIf(rng, vVal) = 1) Then
rngCell.Interior.Pattern = xlNone
Else
rngCell.Interior.ColorIndex = iWarnColor
End If
Next rngCell
Set rnga = Range("B1:B17") ' area to check '
iWarnColor1 = xlThemeColorAccent3
For Each rngCell1 In rnga.Cells
vVal = rngCell1.Text
If (WorksheetFunction.CountIf(rnga, vVal) = 1) Then
rngCell1.Interior.Pattern = xlNone
Else
rngCell1.Interior.ColorIndex = iWarnColor1
End If
Next rngCell1
End Sub
On running this code I get
"Run time error 91: object variable or with block variable not set "
It says the error is at
For Each rngCell1 In rnga.Cells
What am I doing wrong here??
I can run your code locally in Excel 2010 without any errors, so not sure exactly what the problem is.
Try to change the type of rngCell1 from Variant to Range and see if it makes any difference.
As a side note, vVal has not been Dimed. It will only complain if you add Option Explicit at the top of your module, so it shouldn't matter here.