I want to open excel and have a textbox pop up. whatever string the user inputs in the text box will be the variable that is used in Power Query data loaded into the worksheet.
I currently have this M code:
let
Source = Sql.Databases("BMCCL006.DS.BUILDWITHBMC.COM\DW", [HierarchicalNavigation=true]),
trendsql = Source{[Name="trendsql"]}[Data],
dbo = trendsql{[Schema="dbo"]}[Data],
poeh1 = dbo{[Name="poeh"]}[Data],
#"Removed Other Columns" = Table.SelectColumns(poeh1,{"pono", "posuf", "stagecd", "shiptonm", "enterdt", "whse", "vendno", "takenby"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each [stagecd] < 3),
#"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each [takenby] = "mytextboxstring"),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows1",{{"enterdt", type date}}),
#"Filtered Rows2" = Table.SelectRows(#"Changed Type", each true),
#"Sorted Rows" = Table.Sort(#"Filtered Rows2",{{"enterdt", Order.Descending}})
in
#"Sorted Rows"
I want to use the textbox ("mytextboxstrig") to be the value of to filter the rows by in [takenby].
can this be done through VBA so that this query runs and loads to worksheets("Sheet1")?
Thank you.
1 Create a range name, here aaa
2 Use VBA to populate it
Private Sub Workbook_Open()
Range("aaa").Value = _
InputBox(Prompt:="Type the value you want")
End Sub
3 Refer to the named range in powerquery
NameValue= Excel.CurrentWorkbook(){[Name="aaa"]}[Content]{0}[Column1],
#"Filtered Rows" = Table.SelectRows(#"YourPriorStepName", each ([takenby] = NameValue))
Related
Update 5/21/17. Thank you for the suggestion of using a Table. That was helpful. I actually figured it out. I made myinputable a global variable by declaring the Dim statement at the top and making it a Datagridview type. Now I can turn it off in the other event that I needed to do it.
I am a novice. I have created a Datagridview in VB 2015 to capture a bunch of data from the use. When the user is finished with the data entry, I want to store the cell values in my variables. I do not know how to capture any event from my dynamically created datagridview "myinputable." My code is below. Please help.
Private Sub inputmodel()
Dim prompt As String
Dim k As Integer
'
' first get the problem title and the number of objectives and alternatives
'
prompt = "Enter problem title: "
title = InputBox(prompt)
prompt = "Enter number of criteria: "
nobj = InputBox(prompt)
prompt = "Enter number of alternatives: "
nalt = InputBox(prompt)
'
' now create the table
'
Dim Myinputable As New DataGridView
Dim combocol As New DataGridViewComboBoxColumn
combocol.Items.AddRange("Increasing", "Decreaing", "Threashold")
For k = 1 To 6
If k <> 2 Then
Dim nc As New DataGridViewTextBoxColumn
nc.Name = ""
Myinputable.Columns.Add(nc)
Else
Myinputable.Columns.AddRange(combocol)
End If
Next k
' now add the rows and place the spreadsheet on the form
Myinputable.Rows.Add(nobj - 1)
Myinputable.Location = New Point(25, 50)
Myinputable.AutoSize = True
Me.Controls.Add(Myinputable)
FlowLayoutPanel1.Visible = True
Myinputable.Columns(0).Name = "Name"
Myinputable.Columns(0).HeaderText = "Name"
Myinputable.Columns(1).Name = "Type"
Myinputable.Columns(1).HeaderText = "Type"
Myinputable.Columns(2).Name = "LThresh"
Myinputable.Columns(2).HeaderText = "Lower Threshold"
'Myinputable.Columns(2).ValueType = co
Myinputable.Columns(3).Name = "UThresh"
Myinputable.Columns(3).HeaderText = "Upper Threshold"
Myinputable.Columns(4).Name = "ABMin"
Myinputable.Columns(4).HeaderText = "Abs. Minimum"
Myinputable.Columns(5).Name = "ABMax"
Myinputable.Columns(5).HeaderText = "Abs. Maximum "
Myinputable.Rows(0).Cells(0).Value = "Help"
If Myinputable.Capture = True Then
MsgBox(" damn ")
End If
End Sub
As #Plutonix suggests, you should start by creating a DataTable. Add columns of the appropriate types to the DataTable and then bind it to the grid, i.e. assign it to the DataSource of your grid, e.g.
Dim table As New DataTable
With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
DataGridView1.DataSource = table
That will automatically add the appropriate columns to the grid if it doesn't already have any, or you can add the columns in the designer and set their DataPropertyName to tell them which DataColumn to bind to. As the user makes changes in the grid, the data will be automatically pushed to the underlying DataTable.
When you're done, you can access the data via the DataTable and even save the lot to a database with a single call to the Update method of a data adapter if you wish.
I have a Userform in which I submit some data, from the data my box populates with more data, and after I select the data from that box I need to select data from for that item from a third box. The data gathered is from a PivotTable.
Box1 is just a supplied combobox while 2 and 3 are directly from a PivotTable. I have the functionality for Box2 working based off Box1 but that's because they are separated by sheets.
The goal is that Box1 can = A,B,C then if Box1=A then Box2 can = 1,2,3 then if Box2 = 1 then Box3= x,y,z.
The problem being in Box3 it returns the information for Box2= 1,2 AND 3 rather than just 1.
My current code is:
lCommentCount = Sheets(pt).PivotTables("Pivottable1").TableRange2.Rows.Count
For i = 1 To lCommentCount
If Me.ptDatabox.Value = Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 0).List Then
OEEDataEntry.commentbox.AddItem Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 1)
End If
Next i
For i = Me.commentbox.ListCount - 1 To 0 Step -1
If Me.commentbox.List(i) = "" Or Me.commentbox.List(i) = "Grand Total" Or Me.commentbox.List(i) = ("(blank)") Then
Me.commentbox.RemoveItem (i)
End If
Next i
I have been toying around with this all day and found that you have to make the second row visible. This code works well. Note showing the detail BEFORE counting.
Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").ShowDetail = _
True
For i = 1 To lCommentCount
pti = Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 0).Value
If Me.ptDatabox.Text = pti Then
OEEDataEntry.commentbox.AddItem Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 1)
End If
Next i
For i = Me.commentbox.ListCount - 1 To 0 Step -1
If Me.commentbox.List(i) = "" Or Me.commentbox.List(i) = "Grand Total" Or Me.commentbox.List(i) = ("(blank)") Then
Me.commentbox.RemoveItem (i)
End If
Next i
If for whatever reason you want to change the information given in the first box then you would need to make it so that on reexit of that first box that you collapse all the bullets so Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").ShowDetail = False
but that needs to be before you start counting items in the list.
In VBA I am writing a code that gathers information based off an initial input in a userform.
From the initial entrance point I want the code to go out and find the corresponding data and add it. I have that all working well.
But I am gathering the data from a pivot table and if the pivot table is open it returns "" spaces, "(blank)" and "Grand Total", which I want to get rid of.
My Current code is:
lcomboCount = Sheets(pt).PivotTables("Pivottable1").TableRange2.Rows.Count
For i = 1 To lcomboCount
datapoint = Sheets(pt).PivotTables("PivotTable1").PivotFields("combo").LabelRange.Offset(i, 0).Value
UserForm1.ComboBox.AddItem Sheets(pt).PivotTables("PivotTable1").PivotFields("combo").LabelRange.Offset(i, 0)
Next i
For i = 0 To Me.ComboBox.ListCount - 1
If Me.ComboBox = "" Or Me.ComboBox = "Grand Total" Or Me.ComboBox = ("(blank)") Then
Me.ComboBox.RemoveItem (i)
End If
Next i
I had a Msgbox in there at one point to see if my values were reading correctly and they were.
You just need to .List(i) when you test the values stored into the ComboBox.
Also I changed the "direction" (see second comment for details) of the second loop to avoid missing items.
Here is you revised code :
lcomboCount = Sheets(pt).PivotTables("PivotTable1").TableRange2.Rows.Count
For i = 1 To lcomboCount
datapoint = Sheets(pt).PivotTables("PivotTable1").PivotFields("combo").LabelRange.Offset(i, 0).Value
UserForm1.ComboBox.AddItem datapoint
Next i
For i = Me.ComboBox.ListCount - 1 To 0 Step -1
If Me.ComboBox.List(i) = "" Or Me.ComboBox.List(i) = "Grand Total" Or Me.ComboBox.List(i) = ("(blank)") Then
Me.ComboBox.RemoveItem (i)
End If
Next i
I have an ActiveX listbox on an Excel 2007 worksheet. I want to populate it directly, not by pointing its RowSource property to a range, because there is no range that has the desired values.
The listbox's ColumnCount is set to 2.
I set ColumnWidths to "20;20", and now it returns:
20 pt;20 pt
So as far as I understand, two columns in the listbox should be available for writing, right?
Populating the first column is no problem:
activesheet.lstApplyCurves.List = array("Select All","Deselect All","aaa","bbb","ccc")
(or)
activesheet.lstApplyCurves.additem
activesheet.lstApplyCurves.List(0,0) = "Col1, Row1"
But how do I populate column 2? I get an error 380 ("Could not set the list property. Invalid property value.") on this:
activesheet.lstApplyCurves.List(0,1) = "Col2, Row1"
FWIW I've also tried this, but get the same error:
activesheet.lstApplyCurves.List(1,1) = "Col2, Row2"
So...how do I set values in the 2nd column?
UPDATE:
In addition to the answer below, FWIW I also found it's possible to assign a mulit-dimensional array to the List property, which is faster:
Dim ArrayToListbox() As Variant
ReDim ArrayToListbox(0 To 4, 0 To 2)
ArrayToListbox(0, 0) = "Select All"
ArrayToListbox(1, 0) = "Deselect All"
ArrayToListbox(2, 0) = "Row1-Col1"
ArrayToListbox(2, 1) = "Row1-Col2"
ArrayToListbox(2, 2) = "Row1-Col3"
ArrayToListbox(3, 0) = "Row2-Col1"
ArrayToListbox(3, 1) = "Row2-Col2"
ArrayToListbox(3, 2) = "Row2-Col3"
ArrayToListbox(4, 0) = "Row3-Col1"
ArrayToListbox(4, 1) = "Row3-Col2"
ArrayToListbox(4, 2) = "Row3-Col3" '"(" & Join(Array("a", "b", "c"), "|") & ")"
ActiveSheet.lstApplyCurves.Clear
ActiveSheet.lstApplyCurves.ColumnCount = 3
ActiveSheet.lstApplyCurves.List = ArrayToListbox
This works for me. If the below doesn't work on your system then delete the listbox and re-create it and then try this code again.
Private Sub CommandButton1_Click()
With ListBox1
.Clear
.ColumnCount = 2
For i = 1 To 2
.AddItem
.List(i - 1, 0) = "Col1, Row" & i
.List(i - 1, 1) = "Col2, Row" & i
Next i
End With
End Sub
I am using Winforms datagridview (n-tier architecture) to populate from a dataset. I want to have a linkbutton as the last column which should change its text based on the value of two columns. While I have managed to get the linkbutton, I cannot get the value to change. I need the value to change so that when the linkbutton is clicked, it should open up different windows. My code is as below
Private Sub ShowProductRequisitionsInListView(ByVal data As DataSet, ByVal dgv As DataGridView)
dgvRequisitionDetails.Columns.Clear()
dgvRequisitionDetails.DataSource = Nothing
dgvRequisitionDetails.DataSource = data.Tables(0)
dgvRequisitionDetails.Columns(0).Width = 80
dgvRequisitionDetails.Columns(0).HeaderText = "Product Code"
dgvRequisitionDetails.Columns(1).Width = 180
dgvRequisitionDetails.Columns(1).HeaderText = "Product Name"
dgvRequisitionDetails.Columns(2).Width = 150
dgvRequisitionDetails.Columns(2).HeaderText = "Sales UOM"
dgvRequisitionDetails.Columns(3).Width = 60
dgvRequisitionDetails.Columns(3).HeaderText = "Qty. Reqd."
dgvRequisitionDetails.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvRequisitionDetails.Columns(3).DefaultCellStyle.Format = "N3"
dgvRequisitionDetails.Columns(4).Width = 105
dgvRequisitionDetails.Columns(4).HeaderText = "Qty. In Stock"
dgvRequisitionDetails.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvRequisitionDetails.Columns(4).DefaultCellStyle.Format = "N3"
Dim lnk As DataGridViewLinkColumn = New DataGridViewLinkColumn
dgvRequisitionDetails.Columns.Add(lnk)
lnk.HeaderText = "Action"
lnk.Text = "Click Here"
lnk.Name = "lnkAction"
lnk.UseColumnTextForLinkValue = True
End Sub
If the difference between the QtyReqd and QtyInStock is a negative value, the link button should read as "Not Available" and if there is enough stock it should read as "Available". Based on these two values different windows will open upon clicking the link
I tried to check the condition in the DataBindingComplete event, but its not working. What am I doing wrong here?
CL
I think that the easiest way is to add a computed column to the data table and use this as the data bound item for the link column. Something like this:
Dim expr As String = "IFF((([QtyInStock] - [QtyReqd]) >= 0), 'Available', 'Not Available')"
data.Tables(0).Columns.Add("Action", GetType(String), expr)
In the datagrid designer add a handler for: OnRowDataBound="someEvent_RowDataBound"
In the code behind you will have a function:
protected void someEvent_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
If(e.Row.RowType == DataControlRowType.DataRow){
((linkbutton)e.Row.Cells[someindex].controls(0)).Text = "somevalue";
}
}