Change the Format of the Date in VBA - vba

I think this is a basic question but I do not find a way to solve my problem
I created this code :
Sub test()
Range("A11") = CDate(Evaluate("WORKDAY(TODAY(),-1)"))
Range("A11").NumberFormat = "yyyymmdd"
MsgBox Range("A11")
End Sub
But my problem is that the MsgBox do not take into account the NumberFormat "yyyymmdd", it shows "3/5/2021" instead of "20210305". It there a way to change the code so that when I write MsgBox Range("A11") it shows directly "20210305" ?
Thank you for your help.

If you click the cell, you will most likely see 3/5/2021 as the value of the cell. Because it hasn't really changed, just the cell formatting.
MsgBox Range("A11") is the same as MsgBox Range("A11").Value, which will give you the value of the cell, without the formatting.
Try using MsgBox Range("A11").Text instead.

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

VBA - How to convert value of cell from text to number

I have to columns with exported numbers, which are stored as text.
I tried to convert those data to numbers with this code.
Sub FormatColumns()
Columns(9).NumberFormat = "0"
Columns(10).NumberFormat = "0"
End Sub
It has done the job correctly (or it seems so), but I am still not able to work with those data as with numbers, but Excel tell me, that it is stored as number.
But when I verify it via formula =ISTEXT(), it always shows me TRUE
Could you help me with it, please? All tutorials and advices, which I've found on Google was via Formating cells or =VALUE(), but VALUE fucntion doesnt work for me, it shows me #VALUE.
Thank you in advance!
Another solution is:
Sub Macro1()
Range("A:A").Select 'Range of values you need converted
For Each Cell In Selection
Cell.Value = (Cell.Value * 1)
Next
End Sub
A non-vba equivalent solution is to have a "1" entered in a cell, copy that cell, select the range you would like converted to numbers, right click and press Paste Special -> Multiply.
This will leave all values in that range as numbers without changing the format. Tested with ISTEXT.
Make sure your settings also match with commas replacing decimals otherwise it may continue to have issues.
you must replace "," ~~> "."
Sub FormatColumns()
With Columns("i:j")
.Replace ",", "."
.NumberFormat = "#,###.00"
End With
End Sub
Check entire relevant region is set to Number format first.
Selection the relevant region and do a global replace of for nothing ("space" to "leave empty"). Then depending upon your locale try a global replace of , to ..
Next enter 1 in a spare cell, copy that cell, select the entire relevant region and Paste Special, Option, Multiply.
If that does not work switch . back to , and repeat the Paste Special.
Its the comma, needs to be . so 405,90 = 405.90
Converting numbers stored as text to numbers via Macro
Sub macro()
Range("F:F").Select 'specify the range which suits your purpose
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
End Sub
Hilight the numbers you with to "fix" and run:
Sub fixNumbers()
With Selection
.NumberFormat = "General"
.Value = .Value
End With
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

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?

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