How can I access a Range inside a filtered list to retrieve the values? VBA - vba

I'm trying to access the values in C(Number):D(Number) inside the filtered list, however I seem to be doing something wrong because the MsgBox never shows up.
'Filter only numeric values
With MaterialListSheet
.AutoFilterMode = False
.Range("B1").AutoFilter Field:=1, Criteria1:="0*"
End With
Set rangeInventory = InventorySheet.Range("N1:N" & Rows.Count)
' I had Set rangeMaterialList = MaterialListSheet.Range("B1:B" & Rows.Count) in the beginning but I realized If I need C and D i'm only selecting B
Set rangeMaterialList = MaterialListSheet.Range("B1:F" & Rows.Count)
For Each CellML In rangeMaterialList.SpecialCells(xlCellTypeVisible)
BomCodesToSplit = CellML.Range("C" & Rows.Row & ":D" & Rows.Row).Values
MsgBox BomCodesToSplit
For Each CellI In rangeInventory.SpecialCells(xlCellTypeVisible)
Next CellI
Next CellML
Tried this but no luck:
BomCodesToSplit = MaterialListSheet.Range("C" & Rows.Row & ":D" & Rows.Row).Values
I'd like to select
C1:D1
C2:D2
C3:D3
.
.
.
Meaning something like this so it selects it depending on the loop index
Cn:Dn
In some other programming languages I would use the index of the loop but since I'm new to VBA I have no idea how to do this.
How to achieve this?

Not entirely sure what you are doing but you can use the iterating variable property.
In for each loops iterating over some range it's best to use the Range type variable to get the intellisense
example
Dim cell as Range
for each cell in Range("A1:A10")
debug.? cell.Value, cell.Address, cell.Row, cell.Column
next
Note: as you type the cell. you get an intellisense which only lists the properties that are currently available to the object you are working with.

Related

For each or Named Range to populate ListBox

All I am trying to populate a listbox with a For Each loop which iterates through the rows. The for each loop is going through the items in a Named range (ProgramIDs).
The current code I am using is
If Len(ProjectInformation.Range("H2").Value) = 7 Then
Dim Lr As Long
Lr = Range("H1048576").End(xlUp).Row
For Each C In Range("H2:H" & Lr)
With Program_ListBox
.AddItem C.Value
End With
Next C
End If
I fear this is a very basic question however after researching the website / google I simply cannot get this simple task to function.
Any help would be appreciated.
There is no need to loop, you can pass the range as the source of the listbox
Program_ListBox.List = Range("H2:H" & Lr)
Range("H2:H" & Lr) references the cells on the ActiveSheet. You should always fully qualify your references.
With ProjectInformation
If Len(.Range("H2").Value) = 7 Then
For Each C In .Range("H2", .Range("H" & .Rows.Count).End(xlUp))
With Program_ListBox
.AddItem C.Value
End With
Next C
End If
End With
There is no need loop the cells to the add the values to the listbox. You can assign the Range().Value array directly to the Listbox.List array.
With ProjectInformation
If Len(.Range("H2").Value) = 7 Then
Program_ListBox.List = .Range("H2", .Range("H" & .Rows.Count).End(xlUp)).Value
End If
End With

Excel VBA Changing Combobox list from static range to dynamic

This should be pretty simple, but I am struggling.
Right now, this code works:
cboCategoryEdit1.List = Sheets(2).Range("A2:A40").Value
I am trying to "clean up" my project by changing how the combobox is populated. I'd like it to be a combobox with a range that only takes populated cells. Meaning I need to use the last row function. I changed the code to this and I just get an error of "Method or Data Member Not Found". Here is my problem code:
Dim i As Range
With Sheets("xRef-Categories")
Set i = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With
Me.cboCategoryEdit1.ListFillRange = i.Address
Thanks for any help on this one.
btw: Sheet2 is "xref-Categories"
You simply need this...
With Sheets("xRef-Categories")
Me.cboCategoryEdit1.List = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value
End With
You can simplify it like this:
With Sheets("xRef-Categories")
Me.cboCategoryEdit1.List = .Range("A2" , .Range("A" & .Rows.Count).End(xlUp)).Value
End With

Add (A,i) to combobox if (M,i) has a value

I've got a problem :
Inside my Excel spreadsheet, I have a ComboBox (from Developer>insert>form>combobox)
I want to populate this combobox with values from the A column IF the same number in the M column has a value.
What VBA code would I use for it?
See below some that i made up (which obviously doesn't work)
Thanks in advance!
For i = 10 To 239
cell1temp = i
If Sheets("MASTER SHEET").cell(M, cell1temp).Value <> "" Then
DropDown35.AddItem "" & Worksheets("MASTER SHEET").Cells(A, cell1temp) & _
" " & Worksheets("MASTER SHEET").Cells(B, cell1temp)
End If
Next i
In facts, if you go to see the Format Control, in the Control tab, you'll see that for the form that you inserted can only take a range as an input for the values proposed in the list.
If you go ahead and use the Macro recorder, you'll see that these objects are actually called DropDown as seen in the code you posted.
So if you want to stick to that object for a particular reason, you'll have to :
Pre-extract the values to add,
Paste them in adjacents cells to use the range in the next step,
Set the ActiveSheet.Shapes.Range("Drop Down 35").ListFillRange with the address of the previous range
Now if you can change, to a real ComboBox (from Developer>Insert>ActiveX>ComboBox) :
You can use a structure like the one you posted :
Dim ValA As String, _
Ws As Worksheet
Set Ws = ThisWorkbook.Sheets("MASTER SHEET")
For i = 10 To 239
If Ws.Cells(i, "M").Value <> vbNullString Then _
Ws.Shapes.Range("ComboBox1").AddItem Ws.Cells(i, "A") & " " & Ws.Cells(i, "B")
Next i
Set Ws = Nothing

1004: Application defined error when trying to examine cell contents

I am trying to loop through the rows in my sheet, adding cells B-F of the current row to a range to be copied to another sheet. The cells in the row (B-F) should only be added to the range if the value in column G is "Active" and if the value in column C has a value (not empty/nothing/null/!#VALUE...)
I've tried several ways around it, but I keep getting 1004: App/Object defined error off the first If statement
The msgbox shows me the range is valid, I've tried qualifying to the tiniest detail and also using Cells() instead of .range to no avail.
MsgBox (ActiveWorkbook.Worksheets("Staging").range("G" & Cells(rows.Count, 5).End(xlUp).Row).Value)
For i = Cells(rows.Count, 5).End(xlUp).Row To i = 1 Step -1
If ActiveWorkbook.Worksheets("Staging").Cells("G" & i).Value = "Active" Then
If Not IsError(ActiveWorkbook.Worksheets("Staging").range("C" & i)) Then
Set selectRange = range("B" & i & ":F" & i)
Set copyRange = Union2(copyRange, selectRange)
Else
'Do Nothing
End If
Else
'Do Nothing
End If
Next
Am I just missing something simple here? I've been banging my head over this for hours now.
...and for you eagle eyes out there, Union2 isn't a typo, just a user defined function to avoid not being able to join ranges set to "Nothing"
Try the following:
Change the second line to
For i = Cells(rows.Count, 5).End(xlUp).Row To 1 Step -1
Change the third line to
If ActiveWorkbook.Worksheets("Staging").Range("G" & i).Value = "Active" Then
To start change:
If ActiveWorkbook.Worksheets("Staging").Cells("G" & i).Value = "Active" Then
To
If ActiveWorkbook.Worksheets("Staging").Range("G" & i).Value = "Active" Then
Or
If ActiveWorkbook.Worksheets("Staging").Cells(i , 7).Value = "Active" Then
You also need to fully qualify all your ranges as alot of your ranges are pointing to the active sheet and NOT "staging" Unless it is the active sheet, but just to be sure you should use the following code:
With ActiveWorkbook.Worksheets("Staging")
MsgBox (.Range("G" & .Cells(.Rows.Count, 5).End(xlUp).Row).Value)
For i = .Cells(.Rows.Count, 5).End(xlUp).Row To 1 Step -1
If .Range("G" & i).Value = "Active" Then
If Not IsError(.Range("C" & i)) Then
Set SelectRange = .Range("B" & i & ":F" & i)
Set copyRange = Union(copyRange, SelectRange)
Else
'Do Nothing
End If
Else
'Do Nothing
End If
Next
End With
Also You are using copyRange in this scope without it being declared, I am assuming you are assigning it to a range earlier in your code but if not please make sure to do that.
This is wrong:
ActiveWorkbook.Worksheets("Staging").Cells("G" & i).Value
You can use these:
ActiveWorkbook.Worksheets("Staging").Cells(i, 6)
ActiveWorkbook.Worksheets("Staging").range(cells(i, 6), cells(i, 6)).value
ActiveWorkbook.Worksheets("Staging").range("G" + strings.trim(str(i))).value
I was getting the below Error:
Go to Home -> Select the cell for which you are getting Error-> Use clear all funtion to clear the method.
I executed the script again and it started working fine.

macro: if radio button on then copy formula down a range

My prob is this:
I want to be able to use a macro to copy & calculate formula down a range of cells if radio button is on.
But I don't know how to set the variable inside the formula. The macro below should copy the formula to ranges shown (I12:I252, K12:K252, M12:M252).
The formula itself includes a subtraction of two cells in the range of C12:C252 & B12:B252. I cannot seem to reference those cells. I thinks that's the problem...
Anyway, it doesn't work. Any help would be greatly appreciated.
Thanks!
Dim shp1 As Shape
Dim shp2 As Shape
Dim i As Long
On Error Resume Next
Set shp1 = Worksheets("Worksheet").Shapes("Button 1")
Set shp2 = Worksheets("Worksheet").Shapes("Button 2")
If shp1.ControlFormat.Value = xlOn Then
MsgBox "Auto Calculating"
For i = 12 To 252
Range("I" & i).Formula = "=IFERROR(((C & i)-(B & i))*I6/(E7-E6);"")"
Range("K" & i).Formula = "=IFERROR(((C & i)-(B & i))*J6/(E7-E6);"")"
Range("M" & i).Formula = "=IFERROR(((C & i)-(B & i))*K6/(E7-E6);"")"
Next i
Else
If shp2.ControlFormat.Value = xlOn Then
MsgBox "Manually insert calculation"
End If
End If
Few improvements:
Replace the ; in your formulas with ,. ; is your local
setting, but .Formula uses the English setting!
If you want to refer to each column, you need place the i outside the quoatation marks, i.e. instead of =IFERROR(((C & i)... write =IFERROR(((C" & i & ")...
No need to loop each cell and set the formula. If you use $ in your formula properly, you can replace all with one formula: =IFERROR(($C12-$B12)*I$6/($E$7-$E$6),"")
Better use .FormulaR1C1 - this way, your formula will also work, when you would applied it to some other range. To easily convert a formula, type it normally into a cell and the run ? Selection.FormulaR1C1in the VBA Immediate Window. The above formula translates to =IFERROR((RC3-RC2)*R6C/(R7C5-R6C5),"")
Don't hard code cell references (in your case I12:K252). Better assign this range to a named range and use this as a reference. This way, your code will also work if you later add/remove rows or columns.
Don't use On Error Resume Next! This is a invitation to oversee an error that should be fixed
Optional: Alternatively to accessing the controls directly in VBA, you can also assign each one to a cell, name this cell as in step 4 and refer to in by this name. Makes your code more flexible/less complex!
So all in all, I end up with:
Public Sub YourSub()
If Range("SwitchOne") Then
Range("YourRange").FormulaR1C1 = _
"=IFERROR((RC3-RC2)*R6C/(R7C5-R6C5),"""")"
Else
If Range("SwitchTwo") Then
MsgBox "Manually insert calculation"
End If
End If
End Sub