Dlookup after Copy Paste? - vba

Have a real quick question.
I am trying to figure out how to have my dlookup work if someone pastes to the field.
If you enter the pack_number manually this works fine but if someone pastes multiple pack_numbers into column they won't populate.
I've tried to have it update afterupdate on the form as an event but it seems to lock the form.
Any help would be greatly appreciated.
Code:
Private Sub Pack_Number_AfterUpdate()
Me.Refresh
Me.Req_High_Retail = DLookup("[Current High Retail]", "InSHighRetailqry", "Pack_Number='" & Pack_Number & "'")
End Sub

Related

How to set cell value on change of textbox value in excell sheet?

Before asking here I did a lot of search on the net but could not get the solution. So I am writing it here.
My Sheet:
I'm trying to set Opening balance value to the cell on Change event of the Opening Balance.
I tried to VBA too but not sure why it does not work for me.
Any help is appreciated.
If your TextBox name is eg TextBox1 then just use:
Private Sub TextBox1_Change()
Range("A1").Value = Me.TextBox1.Text
End Sub

Code that works for a form control doesn't work for ActiveX control

First time poster in StackOverflow (but not stackexchange) so please let me know if I can clarify or make any formatting changes. Thank you.
Try as I might, I can't find the answer to this question. I suspect it's due to a lack of understanding when it comes to the basics of VBA. I have knowledge of VBA, but little understanding. That being said, here's the problem.
I've set up a form control Combo Box linked to a macro. I've set the input range to a list of hyperlinks in a different sheet and named the range "Hyperlinks". Each hyperlink is to a different sheet in the workbook. I've set the cell link to a blank sell adjacent to the hyperlinks and named it "Linked_Cell." A picture is below.
Form Control View
The macro code is as follows
Sub DropDown10_Change()
HyperLink_Index = Range("Linked_cell")
If Range("HyperLinks").Offset(HyperLink_Index - 1, 0).Hyperlinks(1).Name <> "" Then
Range("HyperLinks").Offset(HyperLink_Index - 1, 0).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End If
End Sub
This automatically moves someone to the sheet they select when they select that sheet from the drop-down menu.
I would like to use an Active X combo box instead of a form control for all the obvious reasons (resize text, etc.) However, I can't get it to work.
I've set "ListFillRange" to "Hyperlinks" and linked cell to "Linked_cell" and entered the same macro code. It looks like this:
View of Active X Combo Box
When I select from the drop down in the Active X Combo box I receive run time error 1004: "Method 'range' of object '_worksheet' failed." I've verified that my named ranges are correct and the code returns no such error when it's in a macro linked to a form control.
Any help is much appreciated! Thank you!
UPDATE: Fixed the Range error by updating the code to the following
Sub ComboBox1_Change()
Dim HyperLink_Index As Range
Set HyperLink_Index = Sheets("SheetList").Range("Linked_Cell")
If Sheets("SheetList").Range("HyperLinks").Offset(HyperLink_Index - 1, 0).Hyperlinks(1).Name <> "" Then
Sheets("SheetList").Range("HyperLinks").Offset(HyperLink_Index - 1, 0).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End If
End Sub
I now receive a Type Mismatch error on the beginning of the IF statement. I don't see the error and still have no idea why this behavior doesn't appear for identical macro code linked to a form control.
P.S. Sorry, I don't mean to turn StackOverflow in to my personal debugging team, so the main question I have is "Why is the behavior different between a macro and active x code?"
UPDATE 2: Found a fix. Was using the wrong index. The fix is below. Leaving it here in case someone else can find it useful.
Sub ComboBox1_Change()
If ComboBox1.Value <> "" Then
Sheets("SheetList").Range("Hyperlinks").Hyperlinks(ComboBox1.ListIndex + 1).Follow NewWindow:=False, AddHistory:=True
End If
End Sub
Found a fix. Was using the wrong index. The fix is below. Leaving it here in case someone else can find it useful.
Sub ComboBox1_Change()
If ComboBox1.Value <> "" Then
Sheets("SheetList").Range("Hyperlinks").Hyperlinks(ComboBox1.ListIndex +1).Follow NewWindow:=False, AddHistory:=True
End If
End Sub

can not set a formula in specific cell

I trying to put a formula in my spreadsheet this formula works very well when i simply copy & paste in excel but when I try to create a macro for this some error occurs I cant understand what is the reason as it seems everything correct in my macro.
I am using below code-
Sub putformula()
range("K4").formula="=IF(ISBLANK($M4);"";IF($M4<=15;ABS($E4)*$K$3;""))"
range("L4").formula="=IF(ISBLANK($M4);"";IF($M4>31;"";IF($M4>15;ABS($E4)*$L$3;"")))"
range("M4").formula="=IF(OR($O4="ABS-Journal";$C4="ABS-Journal");"";IF(ISBLANK($D4);"";IF($C4=$O4;"";IF(MONTH($A4)=MONTH($M$3);IF(ISNUMBER(SEARCH("Invoice";$C4;1));$N4-$A4;$A4-$N4);""))))"
range("N4").formula="=IF(ISBLANK($B4);$N3;$A4)"
range("O4").formula="=IF(ISBLANK($B3);$O3;IF(ISBLANK($B3);"";$C4))"
End Sub
any suggestion will be appreciated.
Try this
Range("K4").Formula = "=IF(ISBLANK($M4),"" "",IF($M4<=15,ABS($E4)*$K$3,""""))"
You are also using ";" instead of "," .
The problem is you have to build a string and you were doing it wrong.
Try to understand and make the changes for every other range.

VBA code to save and update worksheet only works for me?

So I have a worksheet which should be saved and the Excel User's name be updated in cell P1 and the current time stamped in Q1. This should also lock cells for editing with a password when the save button is pressed.
However, it works fine when I update the sheet but not so with other users. They have macros enabled so I know that isn't the cause.
Below is the code that I am using:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Range("P1") = "Last Update: " & Format(Now)
Range("Q1").Value = Application.UserName 'Stamp the current user name
Worksheets("Org Chart").Activate
ActiveSheet.Protect ("Password")
End Sub
Is there something wrong in this code that could be causing this behavior?
Try using:
Range("Q1").Value = Environ("username")
this should gives different value when different user open the workbook. If multiple users using the same workstation to open the workbook, you will need some other technology...
Never mind, this question. I apologize, the user said that they were "sure" they enabled macros but apparently they had not.
However, I do think the alternative to using the environment name as opposed to the program license name is better so I appreciate your assistance.

find and edit data using a form in VBA

UPDATED - I have the form as shown below. The form can populate the list in the combobox. It can also locate the notes attached to the appropriate username. However, i need it to update the same cell which i am struggling with. I tried to use Siddharth Rout's (see comments) .find code but dont really understand it or how to make it apply to my sheets.
To populate the list i used the following
Private Sub UserForm_Activate()
With Worksheets("Notes")
ComboBox1.List = .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value
End With
End Sub
Since posting this i managed to locate the notes attached to a user name using vlookup function in vba. Using the code below.
Sub CallNotes()
the_value = ComboBox1.Text
If TextBox2 = "" Then
TextBox2 = "No Notes Applied."
Else
TextBox2 = Application.WorksheetFunction.VLookup(the_value, Worksheets("Notes").Range("A:B"), 2, False)
End If
End Sub
The main problem i now face is updating the notes. As soon as the comments button is clicked i need the code to look for the username and then paste the contents of the textbox in the cell next to the username.
I tried it with vlookup but it ultimately failed. I since deleted the code and cant remember exactly how i attempted to do it and cant get it back. Essentially i attempted to reverse the whole vlookup process.
All comments and advice is greatly appreciated.
Dim The_value
Dim The_note
Dim c As Range
The_value = Me.ComboBox1.Text
The_note = Me.TextBox2.Text
Set c = Worksheets("Notes").Range("A:A") _
.Find(What:=The_value, LookAt:=xlWhole)
c.Offset(ColumnOffset:=1).Value = The_note
This code comes courtesy of Hans Vogelaar MVP on MSDN
See here
Still thanks to Siddharth Rout for your assistance