find and edit data using a form in VBA - 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

Related

How to assign a selected chart name to a cell (not the other way around)?

I'm still a beginner with VBA and I'm learning a ton from stackoverflow and from general googling.
I'm hitting a wall on this very general task : I'm trying to show a text giving a very general explanation of a chart when it is selected / hovered-over.
The way I was thinking of approaching this was to create a tab with all my chart names (which I already have for other tasks) and create a little text for each of them. A cell (the VBA part) would contain the selected chart name that I could use to do a simple vlookup to fetch the explanation.
I tried to look on google how to do this and I'm usually pretty successful with forums and such, but there are sooooo many information on how to name a chart name based on a cell that I can't seem to find information on how to name a cell based on a chart name.
Edit : was cut off while typing by my newborn waking up, my bad completely forgot to come back and add my attempted code !!!
Sub Test_Chart_Name()
Dim T As String
T = ActiveChart.ChartTitle.Text
Range("AM41").Value = T
End Sub
So far it works when I run it, I do believe I should be able to make it run automatically whenever I select a new chart but right now the wrong behavior is that it display the chart title instead of the name I assigned to it (ie it paste 'Pay per month in dollars' instead of 'Monthly_pay'.
Here's how I approached the request. I wrote a macro that looks up the description of the chart, and displays it in a message box. For each chart you want to run it with, right click the chart, click Assign Macro from the pop-up menu, and select the macro. When you click on the chart, the macro runs. You can also run the macro anytime from Developer tab > Macros or the shortcut Alt+F8.
I set up a lookup range on the active sheet (it could be anywhere) with chart names in the first column and descriptions in the second.
Sub PopUpChartDescription()
Dim rTable As Range, rCell As Range
Dim sName As String, sDescription As String, sCaller As String
On Error Resume Next
sCaller = Application.Caller
If Len(sCaller) > 0 Then ' macro called by clicking on a chart
' activate the chart or it is deactivated after the macro runs
ActiveSheet.ChartObjects(sCaller).Activate
DoEvents
DoEvents
End If
On Error GoTo 0
If Not ActiveChart Is Nothing Then ' so ActiveChart is something, eh?
sName = ActiveChart.Parent.Name
Set rTable = ActiveSheet.Range("DisplayTable") ' my lookup range
Set rCell = rTable.Columns(1).Find(What:=sName) ' find the chart name
If Not rCell Is Nothing Then ' so rCell containing chart name was found
sDescription = rCell.Offset(, 1).Text
' show the description
MsgBox sDescription, vbInformation, "Chart Description"
End If
End If
End Sub
You could try the code below:
Sub Test_Chart_Name()
Dim T As String
T = ActiveChart.Parent.Name
Range("AM41").Value = T
End Sub
Hope that helps!

Check if cell next to found value is empty VBA

I am totally new to VBA. I use it maybe once a year and when I do I search this and other forums for code. For my humble needs, that usually works perfectly. Unfortunately due to my lack of real understanding of the code, in this case I am unable to see what I`m doing wrong. So hopefully somebody can help.
I have a timesheet where I want to copy the values to a datasheet. I have that working. But it is important that the users fill-out the right weeknumber in the form. In order to check if they have done that, I have written a piece of code that should return a messagebox when the weeknumber is not filled. When I simply use a reference to a single cell the code works. But as the weeknumber is not filled in in the same cell on all forms I want the macro to search for the text "Weeknumber: " and then check if the cell next to it is empty or not.
Sub Weeknumber()
dim cl as Range
Worksheets("Invul_Tabel").Activate
With Worksheets("Invul_Tabel").Cells
Set cl = .Find("Weeknummer:", After:=.Range("A1"), LookIn:=xlValues)
If IsEmpty(Range(cl).Offset(0, 1).Value) = True Then
MsgBox "Geen weeknummer ingevuld"
Exit Sub
End If
End With
End Sub
The code gives an error on the "If IsEmpty" line" (error 1004).
Is probably simple, but i can`t seem to figure it out!
Your cl variable is already a range.
Change
If IsEmpty(Range(cl).Offset(0, 1).Value) = True Then
to
If IsEmpty(cl.Offset(0, 1).Value) Then

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

VBA- Using inputbox to filter and copy data

i am working on a userform where i puted a button that when i click on it i got an input box where i try to filter data of the column (E) then after filtering this data copy from colum A1 till the value filtered in the column E in another sheet caaled filtred_data i am using this code this code but it show me a bug dont nw how to fix it
Private Sub CommandButton9_Click()
Dim xno As Integer, Found As Range
Do
xno = Application.InputBox("Enter the number of Top communities ", Type:=1)
If TypeName(xno) = "Boolean" Then Exit Sub
Set Found = Columns("E").Find(what:=xno, lookat:=xlWhole, LookIn:=xlValues)
If Found Is Nothing Then
MsgBox "the number was not found, please try again !!", vbInformation
Else
Found.Range("A1:F10000").Copy Destination:=Sheets("filtred_data").Range("A1:F10000")
End If
Loop
End Sub
if anyone can help me please , thank you
The problem in your case is that you are setting the Found variable to be a single cell by using the Find method. Later in your code, you are trying to copy the A1:F10000 of that ONE cell when you write Found.Range("A1:F1000").
Essentially, your code looks like this (assume Found refers to cell E25):
Range("E25").Range("A1:F1000").Copy
Can you see why that would cause an error?
I'd try to offer more help, but it's hard to determine exactly what you want. Can you possibly give more details about your spreadsheet layouts and an example of what you want to happen?

Spell check an Excel sheet in VBA

I have scoured the Internet and I have found a handful of possible solutions to this issue, but I wanted to ask here as well.
The goal is to click a button, and spell check an entire sheet.
Here's some code
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
Also, I found this:
Sub SpellCheck()
Dim Checkword As String, Result As Boolean
Checkword = Selection.Value
Result = Application.CheckSpelling(Checkword)
Selection.Offset(0, 1) = Result
End Sub
Any ideas? Neither is working for me. Thanks!
You can check the whole workbook by doing something like:
Sub SpellCheck()
For Each sh In Worksheets
Sheets(sh.Name).Cells.CheckSpelling
Next
End Sub
this will cycle through each sheet in the entire book and run a spellcheck on each one. What I can't figure out yet is how to make the spell checker actually move to the position of the spelling error. So, with the above you just get a list of spelling errors with no context with which to asses them.
I noticed I just had a typo in my code.
Below works:
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
But, if anyone knows how to do the entire workbook, I'd be interested in that. Thanks.
This code will work on selected cells .This will highlight if any spell mistakes in a cell
Dim Myrange As Range
Selection.SpecialCells(xlVisible).Select
For Each Myrange In Selection
If Application.CheckSpelling(word:=Myrange.Value) = False Then
Myrange.Font.Color = vbRed
End If
Next
OK, so you can use the following command to invoke the toolbar's spellchecker which does move you to the position of the spelling error as long as you have screen updating enabled at the time.
Application.CommandBars("Tools").Controls("Spelling...").Execute
You can use this command embedded in the loop above to loop through the sheets in the workbook and invoke this command on each new sheet.
Cap
This uses a code snippet from a previous answer to restrict the area used for spellchecking to a specific region. Something I needed to do in a small project. This give the full functionallity of the spellchecker with errors shown in context. The rowNumber is calculated elsewhere.The selection range can be fully controlled elsewhere in your code to suit your particular need. Thought this might help others searching this posting.
With Sheets("Sheet1")
slic = CStr(rowNumber)
.Range("AL3:AN" & slic).Select
Application.CommandBars("Tools").Controls("Spelling...").Execute
End With
Thanks to previous posters this solved a problem form me. I am most grateful.