Excel Combobox VBA issues multiple items - excel

I have a dynamic range in a combo box that I use as an index with several concat formulas in the workbook. I fill the box on dropbuttonclick with
Private Sub ComboBox1_DropButtonClick()
'Updates on drop down
'This place row number in H3 making the dynamic vba combo box work like form control
Sheets("Home").Shapes("ComboBox1").Select
With Selection
.ListFillRange = "ParticipantData!$A$3:$A$" & Sheets("ParticipantData").Range("A3").End(xlDown).Row
lngrow = ActiveSheet.ComboBox1.ListIndex + 1
Range("H3") = lngrow
End With
End Sub
The issue is when I delete a value from Column A it throws a 438 error on me. The range changes quite often and I'd like to be able to add & delete and names as necessary. I typically copy and past values from a census and need to eliminate the .listfilRange 438 erro. I use the combobox to select a name which pulls data, generates a report and ultimate is saved as a pdf. lngrow is used as the row for the index of a dynamic range PartData in shee "ParticipantData".
Edit: Removed 2nd question
I know I have rofl vba skills, so thank you for your time helping me.

Related

Userform Initialise with listbox filled with array from cells

I have a userform within a userform (userform-ception) where I implement a search function for part list in Row B. Prior to searching I want to check for duplicates, which triggers a second userform when true. I am having trouble populating userform2's list box when it initialises. My logic is
If duplicate count > 1, filter by column B for that search criteria
Open second userform
From the current visible cells, grab the first 7 columns and populate a list box with this (Using visible cells as array)
However when I try to open the userform it turns out blank so I am wondering if anyone can shed some light on this.
This is in my Search Function
Dim Dupl As Integer
Dupl = Application.WorksheetFunction.CountIf(Range("B:B"), PartNumberSearch.Text)
If Dupl > 1 Then
With ActiveSheet.Range("A1:M1")
.AutoFilter Field:=2, Criteria1:=PartNumberSearch
End With
UserForm2.Show
'Exiting the sub here so that when userform 2 is used to disambiguate the part it can continue to search using PartID instead
Exit Sub
This is in my userform2_initialise() function
Private Sub UserForm2_Initialize()
DupList.ColumnWidths = "35;50;60;25;10;20;40"
'Checks last row of the list
DupRow = ActiveSheet.Range("A65536").End(xlUp).Row
'Puts all the result into the ResultsList
DupList.List = Range("A2:G" & DupRow).Value
End Sub
A sample snippet of my table
Any help is appreciated. Thanks!

excel vba listbox additem

I am running office 365 home on windows 10. I am programming Excel using VBA.
I have a data range in a worksheet row. I want the user to be able to select one item from this row of data. I am trying to populate ListBox or ComboBox with dta from the range row. Having read MS vba.reference documentation I decided to get my range data into an array and use listbox = myarray() and got "Object does not support this method or property." I tried looping through my data range and putting each item in using listbox.additem (mydata()) with the same result. On examination of the listbox prperties AddItem is not there. Seems they have been withdrawn or maybe never existed for Excel VBA.
Any suggestions?
If you are using Additem then you should only add a single item not an array. If you want to use an array you have to use List and the array should be one-dimensional
MyListBox.List=MyOneDArray
Personally I never use .List and an array with Listboxes because I found some circumstances in which it did not work as expected.
There are two ways you can do this:
loop through each item individually and use combobox.additem
Set the combobox.list = array (or variant)
See below for an example of both assuming you want to populate the data from cells A1 to A10 in 2 separate comboboxes:
Private Sub UserForm_Initialize()
Dim i As Integer
Dim arr As Variant
' looping through parameters 1-by-1
With UserForm1.ComboBox1
For i = 1 To 10
.AddItem ThisWorkbook.Sheets("Sheet1").Range("A" & i).Value
Next i
End With
' setting combobox list to arrauy
With UserForm1.ComboBox2
.List = ThisWorkbook.Sheets("Sheet1").Range("A1:A10").Value
End With
End Sub
Make sure to insert your code in the userform code and not the module code

Get value of dropdownbox 1 and use dropdownbox2's value as a cell indicator where to paste it

I have 2 drop down boxes and a button. Drop box 1 has numbers from 1 to 30, and Drop box 2 has numbers from 1 to 130.
What I need is a way to take the value of Drop box 2 and - having Drop box 1's value as a row number - paste it into the C column of another sheet, allowing for live data editing.
Note: I am a complete noob to VB. The Drop down boxes and button are on the chart sheet if that makes it any different. Also I have to have the drop boxes on the chart sheet, not the data sheet.
I have tried selecting drop box values however am unable to figure out the correct way to do so. Things such as DropBox2.Value & DropBox2.Value.Select don't seem to work (most likely doing it wrong). Googled various ways of how to get drop box value and paste elsewhere but to no avail. Apologies for wasting anyone's time if this seems mundane.
Sub TEST()
' TEST Macro
Sheets("Sheet1").Select
DropBox2.Value.Select
ActiveSheet.Cell("$C""DropBox1.Value").Select
Selection.Paste
Chart2.Select
End Sub
Expected to do what I said in the description however the error message
Object required
appears.
Try the below code:
Sub TEST()
Dim varDrowDown1 As Variant: varDrowDown1 = ThisWorkbook.Worksheets("Sheet1").Shapes("Drop Down 1").OLEFormat.Object.Value
Dim varDrowDown2 As Variant: varDrowDown2 = ThisWorkbook.Worksheets("Sheet1").Shapes("Drop Down 2").OLEFormat.Object.Value
ThisWorkbook.Worksheets("Sheet2").Cells(varDrowDown1, 1).Value = varDrowDown2
End Sub
Check that the names of the objects are correct. I assumed Sheet1 is where the Drop Down Boxes are, Drop Down 1 and Drop Down 2 are their names, Sheet2 is the Worksheet where you want to copy the Value to, and you would like to insert it into the first column.
You can find the name of your Drop Down Boxes by right clicking them and looking at the place where the Active Cell's reference (e.g. A1) usually appears. The Sheets' Name is trivial, and you should know the column's number, too.
Sheets("Chart2").Select
Dim varDropDown1 As Variant: varDropDown1 = ThisWorkbook.Sheets("Chart2").Shapes("DropDown1").OLEFormat.Object.Value
Dim varDropDown2 As Variant: varDropDown2 = ThisWorkbook.Sheets("Chart2").Shapes("DropDown2").OLEFormat.Object.Value
ThisWorkbook.Worksheets("Sheet1").Cells(varDropDown1, 3).Value = varDropDown2
Sorted, thank you for all of your help!

VBA: Deleting rows that have a blank in a certain column and making a button in Excel

So I am trying to write VBA code to delete an entire row in Excel if a cell in that row in a certain column is blank. I am completely new to VBA, so I literally just had to learn as I coded. I think I have the methodology down, but it always leaves one last row with a blank in that specific column in the worksheet. In some cases, it leaves 5 rows with a blank in that specific column. Any help fixing would be appreciated, thank you!
I also want to have it where I can click on a button in the workbook, more than likely on a separate sheet, and it will do the deleting rows methodology for a specific sheet. If that is possible, am I able to move all of that methodology from one from workbook to another?
I've already tried some ways of implementing both of these using VBA, but this is my first time ever using VBA, so a lot is still new to me.
Private Sub Button_Click()
Dim LR As Long
Application.ScreenUpdating = False
For LR = Sheets("Test2").Range("AB" & Rows.Count).End(xlUp).Row To 2 Step 1
If Range("AB" & LR).Value = "" Then
Rows(LR).EntireRow.Delete
End If
Next LR
Application.ScreenUpdating = True
End Sub
I expect all the rows with blanks in that column to be deleted when I press the button in the worksheet.
If the blank cells in that column are truly blank and not zero length strings returned by formulas (e.g. "") then SpecialCells can remove them all at once.
Private Sub Button_Click()
Intersect(Me.Columns("AB"), Me.Cells.SpecialCells(xlCellTypeBlanks)).EntireRow.Delete
End Sub
For your own code, you should step backwards in your loop (Step -1). Further, you should not look at column AB for the last cell. If there were blanks in the last row or rows, they would be ignored.

How can you shift selected cell using Excel VBA?

I am trying to automate a rolling calendar spreadsheet that tracks various metrics and charts them into a spark line. The script I would like to write would shift the selected range in the spark-lines every time it is ran.
I have done some googlefu and have tried using the offset function to no avail. This is because the data is in a predefined range defaulting to num 0 based on the formulas used to populate the spreadsheet int the first place.
excel vba : selected cells loop
https://www.excel-easy.com/vba/examples/loop-through-entire-column.html
https://support.microsoft.com/en-us/help/291308/how-to-select-cells-ranges-by-using-visual-basic-procedures-in-excel
I am stuck at incrementing the ActiveCell.SparklineGroups.Item(1).Item(1).SourceData from its current selected range to PPTracking!G8:R8 ... H8:S8 ... and so on each time the macro is ran.
This is my first time working in VBA and any help is greatly appreciated!
Sub Macro4()
Dim selectedRange As Range
Set selectedRange = PPTracking!F8:Q8
Range("E5:E6").Select
Application.CutCopyMode = False
ActiveCell.SparklineGroups.Item(1).Item(1).SourceData = "PPTracking!F8:Q8"
Range("E5:E6").Select
End Sub
You can either use Sparkline.ModifySourceData or directly change the Sparkline.SourceData property, as it looks like you are currently aiming to do.
This code will shift the SourceData 1 column to the right - from F8:Q8 to G8:R8, then to H8:S8, etc. - by using the original SourceData value as a reference within Range, which is then Offset by 1 column.
It concatenates the Parent.Name to the Address to get the full Worksheet Name and cell reference.
Sub ShiftSparklineData()
If ActiveCell.SparklineGroups.Count > 0 Then
With ActiveCell.SparklineGroups.Item(1)
.SourceData = "'" & Range(.SourceData).Parent.Name & "'!" & Range(.SourceData).Offset(, 1).Address
End With
End If
End Sub
Avoid using ActiveCell where possible though; reference the cell(s) with a sparkline using Sheets("Yoursheetname").Range("Cellreference")

Resources