How to display value on textbox from other sheet using vlookup vba - vba

I am trying to display a value from another worksheet using vlookup and userform but it seems not to work
Private Sub upDate_Click()
Dim sht As String
Dim checkDate As String
Dim checkUpdate As String
'Varible assigned to store combobox value (sheets value on worksheets)
sht = Me.Combobox1.Value
'Uses vlookup to check if the date selected is already on the worksheet (sht)
'which is the value from the combobox.
checkDate = Application.WorksheetFunction.VLookup(Me.date.Value,
Worksheets(sht).Range("A:F"), 1, False)
'Uses vlookup to find the login time from the date selected
timeUpdate = Application.WorksheetFunction.VLookup(Me.date.Value,
Worksheets(sht).Range("A:F"), 2, False)
'If the date on the userform matches with a date on the worksheet then the login
'date will display on texbox what time the user logged in
If Me.date = checkDate Then
Me.time.Value = timeUpdate
End If

Related

Add textbox value to it's destination

Situation: I'm working on a UserForm with the following Controls:
Combobox: This is used to pull up a list of names on Sheet2 (Column A) and allows the user to select a name that'll be used for the form.
TextBox: This is used to add a numerical value. That value will be placed on Sheet2, Column C, and two rows over from the name that's been selected from the combo box
CommandButton: This button is used to add the numerical value that has been typed into the text box into the cell on Sheet2, two columns over, and two rows over from the cell matching the name that's been choosen from the combobox
Problem: I have the Combobox and Textbox set up correctly but am having trouble creating VBA for the CommandButton to add the text box value to it's destination.
VBA So Far:
Private Sub AddButton_Click()
Dim WS As Worksheet
Dim Rng As Range
Dim Crystal As Long
Set WS = Worksheets("ParticipantList")
With WS.Range("a2:c300")
FindColumn = Application.WorksheetFunction.Match(Me.Participants.Value, WS.Range("A2:A300"), 1)
Crystal = Me.NumberOfCryst.Value
If FindColumn <> "" Then
With WS.Range("a2:c300")
Text = Me.NumberOfCryst.Value
WS.Activate
FindColumn = Application.WorksheetFunction.Match(Me.Participants.Value, WS.Range("A2:A300"), 0)
End With
End If
End With
End Sub
Now obviously this is all over the place and I've made tons of changes and attempts at getting it to work.
maybe you're after something like this:
Private Sub AddButton_Click()
Dim Rng As Range
Set Rng = Worksheets("ParticipantList").Range("A2:A300").Find(What:=Me.Participants.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not Rng Is Nothing Then Rng.Offset(2, 2).Value = Rng.Offset(2, 2).Value + CLng(Me.NumberOfCryst.Text)
End Sub
you may also want to add some textbox text validation and be sure the user input a numeric value

How to generate new sheet from find result?

I would like to generate new sheet for all values contains "_S" :
Example:
When user click on "Generate page", two new sheet are created (one sheet with title "Folder22_S" and other one with title "Folder3_S"
In second time, i would like that new sheet generated from a model (not generate a empty sheet)
My question is: How to do this ? I doesn't know how to find all values contains "_S" and get cell value ( Folder22_S and Folder3_S) ?
My pseudo VBA code:
Sub generate()
'Array contains all values *_S'
Dim AllValuesContains_S As Variant
AllValuesContains_S = Array("Folder22_S", "Folder3_S", ...)
For Each item As String In AllValuesContains_S
Sheets.Add.Name = item
Next
End Sub
I will assume the range you want to search is A2:C7. Adjust that portion of the code to be whatever range you need.
Sub New_Wksht()
Dim SearchRange as Range, c as Range
Dim sht as Worksheet
With ThiwWorkbook
Set SearchRange = .Sheets("Your Sheet Name").Range("A2:C7") 'Change sheet name and range to be what you need
For Each c in SearchRange
If Right(c.Value,2) = "_S" Then
Set sht = .Sheets.Add
sht.Name = c.Value
End If
Next C
End With
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

VBA: VLookup between two workbooks

I am wondering if someone can help me out. I created a Userform with 3 comboboxes. Combobox 1 and 2 list all open workbooks. Combobox 3 lists the worksheets from Combobox 2. I now want to run a Vlookup. The lookup values are the values (in this case product codes) in each cell beginning at D9 to the last cell with a value in Column D of the first Worksheet of Combobox2's. The lookup range will be ("A5:S###"[number of rows varies depending on the file]").
The Vlookup formula should be in the Column I of the first Worksheet of Combobox2's value starting at "I9" looping through each cell in I9 until all the Codes in D9 are looked up.
I keep getting error the major one being “Runtime-error '9'”: Subscript out of range. Here is my code.
Option Explicit
Private Sub CancelButton_Click()
Stopped = True
Unload Me
End Sub
Private Sub ComboBox1_Change()
Dim ScheduleA As Workbook
Dim Termset As Worksheet
Set ScheduleA = Workbooks(Me.ComboBox1.Value)
With Me.ComboBox3
For Each Termset In ScheduleA.Worksheets
.AddItem Termset.Name
Next Termset
End With
End Sub
Private Sub FillACDButton_Click()
Dim ACDRebateInfo As Worksheet
Dim lastRow As Long
Dim NewRebate As Single
Dim NewRebateType As String
Dim LookUp_Range As Range
Dim ActionCode As String
Dim ACD_NewRebate As Range
Dim ACD_NewRebateType As Range
Dim ACD_ActionCode As Range
Dim SCC As Range
Dim Cell As Range
Set ACDRebateInfo = Workbooks(Me.ComboBox2.Value).Worksheets(1)
Set ACD_NewRebate = ACDRebateInfo.Range("I9:I500")
Set ACD_NewRebateType = ACDRebateInfo.Range("J9:J500")
Set ACD_ActionCode = ACDRebateInfo.Range("B9:B500")
Set LookUp_Range = Worksheets(Me.ComboBox3.Value).Range("A5:S400")
Set SCC = ACDRebateInfo.Range("D9:D230")
With ACDRebateInfo
For Each Cell In ACD_ActionCode
ActionCode = Application.WorksheetFunction.VLookup(SCC, LookUp_Range, 17, False)
Next Cell
End With
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim wkb As Workbook
For Each wkb In Application.Workbooks
Me.ComboBox1.AddItem wkb.Name
Me.ComboBox2.AddItem wkb.Name
Next wkb
End Sub
Not sure this is your issue but this piece of code does not make sense:
For Each Cell In ACD_ActionCode
ActionCode = Application.WorksheetFunction.VLookup(SCC, LookUp_Range, 17, False)
Next Cell
You are looping through the Action Codes but not using the Cell variable

Compare Columns on different Workbooks and copy from one to the other

EDIT: Am I required to use the object model?
So I am having trouble with this program. I have written a GUI that has two list boxes and some buttons. I am trying to start the program, select a column of data/numbers to compare with another column of data/numbers on another worksheet and then copy the adjacent cells of the first to one specified in the program. The copy part of my code worked fine along but when I added all the sheet stuff in it quit working. I don't Know if it is because you can't compare on two sheets after you do a .Active/.Open or if I plain don't understand .Active and .Open. If I am doing something wrong, I don't have a clue how to fix it. Any Suggestions would be much appreciated.
Thanks
Nick
P.S I have included only the part of the code where I think there is a problem. If needed I can submit the whole thing.
Sub copy2()
Dim ColCopyTo As String 'the column you want to copy to
Dim ColSelect As String 'the column with the initial data
Dim ColCompare 'the column you want to compare the initial data with
Dim ColCopyFrom 'the column you want to copy data from
Dim RowCrntCompare As Long
Dim RowCrntSelect As Long
Dim RowLastColCompare As Long
Dim RowLastColSelect As Long
Dim SelectValue As String
Dim WorkSheetSelect As Worksheet 'the worksheet with initial data
Dim WorkSheetCompare As Worksheet ' the worksheet you want to compare initial data on
Dim WorkBookCompare As Workbook 'the workbook you want to compare initial data on
Dim WorkBookSelect As Workbook ' the workbook with initial data on it
Dim WorkSheetIndex As Integer
With Sheet1
continue = False 'initialise continue to false
MsgBox "Select the Workbook and Worksheet"
CommandButton2.Visible = True
CommandButton1.Visible = False
Call Wait 'pause until button is clicked
'MsgBox ListBox2.value
WorkSheetIndex = udfSheetIndex(ListBox2.value) 'index of the worksheet
'MsgBox WorkSheetIndex
'Set WorkBookSelect = Workbooks(ListBox1.value)
Set WorkBookSelect = Workbooks.Open(ListBox1.value)
WorkBookSelect.Activate
Set WorkSheetSelect = ActiveWorkbook.Sheets(WorkSheetIndex)
'Set WorkBookCompare = ActiveWorkbook.Sheets(WorkSheetSelect)
'WorkBookSelect.Activate ' set the initial workbook to active
WorkSheetSelect.Activate ' set the initial worksheet to active
ColSelect = InputBox("which column do you want to select From") 'column you want to first select for copying
ColCopyFrom = InputBox("which column do you want to copy data ColCopyFrom") 'where you are copying data from
continue = False 'reset continue to false
MsgBox "select the workbook and worksheet you want to compare to"
CommandButton2.Visible = True
Call Wait 'wait for button click
'Set WorkBookCompare = Workbooks(ListBox1.value)
Set WorkBookCompare = Workbooks.Open(ListBox1.value)
WorkBookCompare.Activate
MsgBox ListBox2.value
WorkSheetIndex = udfSheetIndex(ListBox2.value) 'index of the worksheet
MsgBox "listbox2" & ListBox2.value
MsgBox WorkSheetIndex
Set WorkSheetCompare = ActiveWorkbook.Sheets(WorkSheetIndex)
WorkBookCompare.Activate 'set the second workbook to active
WorkSheetCompare.Activate ' set the second worksheet to active
ColCompare = InputBox("which column do you want to compare to ") 'the column you are comparing it to
ColCopyTo = InputBox("which column do you want to copy data to") 'where you are copying data to
RowLastColSelect = .Range(ColSelect & .Rows.Count).End(xlUp).Row 'length of the selected column
RowLastColCompare = .Range(ColCompare & .Rows.Count).End(xlUp).Row 'length of ColCompare
For RowCrntSelect = 1 To RowLastColSelect Step 1 ' from 1 to last
SelectValue = .Cells(RowCrntSelect, ColSelect).value ' value of cell
'MsgBox SelectValue
If SelectValue <> "" Then
For RowCrntCompare = 1 To RowLastColCompare Step 1
If SelectValue = Cells(RowCrntCompare, ColCompare).value Then
.Cells(RowCrntCompare, ColCopyTo).value = _
.Cells(RowCrntSelect, ColCopyFrom).value
End If
Next RowCrntCompare
End If
Next RowCrntSelect
End With
End Sub
On your line:
WorkSheetIndex = udfSheetIndex(ListBox2.value) 'index of the worksheet
try using:
WorkSheetIndex = Sheets(ListBox2.value).Index
UDF stands for "User Defined Function", so I'm guessing its not working because the function is not set up correctly.