VBA- Using inputbox to filter and copy data - vba

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?

Related

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

Excel date formatting error

I'm doing some work on an already done excel program, but I'm very rusty in this area or probably never had done something in this part, I'm very new in VBA so please don't judge me if it's a simple mistake or error the links are in the description.
So I have problem where if I put the confirmation option, for example: in one cell press "1" in the product you have, and in another cell write "S" if you already have the product and it puts you a date of today in another cell.
The problem is when I delete the info that I inserted, and reenter it the date deformats itself becoming smaller and the location on the cell changes too.
I'm going to put the links because, like I said I'm rusty and I can't find where the code of this date comes.
http://www.docdroid.net/12dh4/master-atual-20155.xls.html -->This one is the Excel
http://www.docdroid.net/12dhj/errorphotos.pdf.html --> photos showing error
this is the code of one of the sheets the other ones are almost the same, If you guys see the photos it would help to understand the error itself.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Set rng1 = Intersect(Range("v3:v500"), Target)
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
On Error Resume Next
If rng1.Value <> "" Then
rng1.Offset(0, 2).Value = Now()
Else
rng1.Offset(0, 2).Clear
End If
Application.EnableEvents = True
End Sub
The code would be triggered when you do the changes at column V. You may find the code in the worksheet itself:
it will clear the date if the column V contains no value.
By the way, the date is not getting smaller but it show the date of today in different format. you may check the date format by right click the cell and choose the format cells... option to look at it.

Can I find whether a Cell in Excel is referenced in a formula?

I have searched alot of sites on the internet, and I'm guessing I haven't found an answer because it is not possible, to try to understand the following:
I would like to understand if a Cell containing a value (say 10) plays into / forms part of a formula further into the document. I KNOW that the cell I have is NOT a formula cell, it is a single entry, however, I am trying to understand if this cell forms part of a formula. The need for this is there are ALOT of formulas and ALOT of data, and I am trying to see which data is relvant and which data is not.
Hope somone can help?
Kind regards
Paul
Manually you can use "Trace Dependents" on the 'Formula Auditing' menu under "Formulas" tool tab.
Using VBA you can check the number of dependents like this:
Range("A1").Dependents.Count
If you want a list of cell addresses that cell A1 is used in you can do this:
MsgBox (Range("A1").Dependents.Address)
I have A1 used in two other cells so my result looks like this:
Full code with error checking:
Sub test()
On Error Resume Next
If Range("A1").Dependents Is Nothing Then
MsgBox ("No dependents found")
Else
MsgBox (Range("A1").Dependents.Address)
End If
End Sub
The Dependents property of a Range object gives you the dependent cells of your formula. Be careful you will get an error if there are no dependent cells
Function hasDependents(r As Range) As Boolean
On Error GoTo err
hasDenpedents = r.Dependents
Exit Function
err:
hasDependents = False
End Function
And then
if hasDependents([A1]) then
'...

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