Excel VBA Changing Combobox list from static range to dynamic - vba

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

Related

Excel VBA - Making a Named Range based on a found cell

I am trying to figure out how to make a named range, based on a cell found using:
For Each Cell In ISBN_Range
If Cell.Value = ISBN Then
ISBN_Valid = True
ISBN_Found = Range("A" & Cell.RowIndex & ":E" & Cell.RowIndex)
ISBN_Found.Interior.ColorIndex = 6
Exit For
End If
Next Cell
This is not working, and I am not sure why, I haven't been able to find the answer elsewhere, sorry if this is really simple! I basically just want to make the row of data that the found cell exists in a named range. ISBN_Found is declared as a range much earlier in my code, so that is not the issue.
I figured out the answer with some help from the guys in the comments, final code looks like this:
For Each Cell In ISBN_Range
If Cell.Value = ISBN Then
ISBN_Valid = True
Set ISBN_Found = Range("A" & Cell.Row & ":E" & Cell.Row)
ISBN_Found.Interior.ColorIndex = 6
Exit For
End If
Next Cell
Just needed to delete "Index" from "Cell.RowIndex"

Referencing a Worksheet in a Formula That's Always Changing in VBA

I am trying to write a macro that copies data from another worksheet. I am having troubles on how to properly input the worksheet name in a formula. The Summary worksheet is the destination and the 5th worksheet, which will change daily (and is in format x.xx_1), is the source. Here's my code:
Sub steadf()
Dim SN As String
SN = InputBox("Enter Tab Date - 2.24, 10.24, etc.")
Worksheets(5).Name = SN & "_1"
Sheets("Summary").Select
Range("D24").Select
ActiveCell.Offset(0, 3).Formula = "=SN" & "_1" & "!" & "Cost"
End Sub
When I run this, the formula in G24 is
=SN_1!Cost
The formula I'm looking to use is ='2.24_1'!Cost. I would appreciate any help. Thanks in advance.
Try this:
Formula = "='" & SN & "'!Cost"

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

How can I access a Range inside a filtered list to retrieve the values? 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.

Excel macro to concatenate one row at a time to end of file

I need an Excel macro to join seven columns of data on each row until the end of the data is reached. For example if I have a formula like this:
=A1&B1&C1&D1&E1&F1&G1
How can I write the macro so that it increments for every row to the end of the file in a sequence like this?
=A1&B1&C1&D1&E1&F1&G1
=A2&B2&C2&D2&E2&F2&G2
=A3&B3&C3&D3&E3&F3&G3
With so many answers, the main focus on what assylias and I were highlighting has gone to waste :)
However, if you still want a VBA answer. Use this method. This is much faster than Looping or an Autofill.
Option Explicit
Sub Sample()
Dim LastRow As Long
Dim Ws As Worksheet
Set Ws = Sheets("Sheet1")
LastRow = Ws.Range("A" & Ws.Rows.Count).End(xlUp).Row
'~~> If your range doesn't have a header
Ws.Range("H1:H" & LastRow).Formula = "=A1&B1&C1&D1&E1&F1&G1"
'~~> If it does then
Ws.Range("H2:H" & LastRow).Formula = "=A2&B2&C2&D2&E2&F2&G2"
End Sub
If you have 1000's of rows then you might want to switch off Screenupdating and change Calculation to Manual before you run the code and then reset them at the end of the code.
I think the easiest way to do this would be to just fill down as assylias says but if you want to use VBA:
Selection.AutoFill Destination:=Range("Your Fill Range"), Type:=xlFillDefault
Should copy across the other rows.
I agree 100% with the comments and the other answers, why do you need VBA to do this, but just to answer your original question, this is how I would accomplish it:
Sub FillAllWithFormula()
Dim i As Variant
Dim wsht As Worksheet
'If you are using this for a specific Worksheet use the following
Set wsht = ThisWorkbook.Worksheets(yourWorksheetName)
'or if you are always using this for the active sheet use the following
Set wsht = ActiveSheet
For i = 1 To wsht.Rows.Count
'Replace "X" with the column letter you want your formula to appear in
wsht.Range("X" & i).Formula = "=A" & i & "&B" & i & "&C" & i & "&D" & i & "&E" & i & "&F" & i & "&G" & i
Next
Set wsht = Nothing
End Sub