I need to use the current and the previous month in "mmmm" format, and the best solution I found is MonthName(Month(Date)).
However, Excel keeps giving me:
Compile Error: Object Required
And it's driving me crazy. I'm pretty new to VBA so can anyone explain what I am doing wrong here?
Sub test()
Dim CurrentMonth As Variant
Dim PreviousMonth As Variant
Set CurrentMonth = MonthName(Month(SetupSheet.Range("C9"))) '<-------HERE
Set PreviousMonth = MonthName(Month(SetupSheet.Range("C9") - 1))
...
End Sub
You don't need to use Set. You only use that if you are passing Object to a variable.
This line returns a String and not an Object (e.g. Range, Worksheet, Chart, etc.)
MonthName(Month(SetupSheet.Range("C9"))) 'if C9 contains a valid date format
So to pass it to CurrentMonth variable, simply assign it using = sign:
CurrentMonth = MonthName(Month(SetupSheet.Range("C9")))
Lowercase 'm' is minute. Uppercase 'M' is for month.
Related
A piece of my macro adds a new worksheet with the current date but when I want to assign the worksheet to an object it doesn't work. I get a
run time error 9
Dim shA As Worksheet
Set shA = Worksheets(Date)
Can anyone help?
You need to convert Date to a String format, depending on the format of your date.
Dim shA As Worksheet
Set shA = Worksheets(Format(Date, "dd.mm.yyyy"))
Considering that your computer date is in format DD.MM.YYYY, if you are looking to change the tab name of the worksheet, then it is like this:
ActiveSheet.name = Date()
or
shA.Name = Date()
Thus, you will get this:
I am trying to create a macro for Word 2013 that does the following: the macro should capture the value of a cell of a word table and then add another value and paste the result in another cell of the same table.
My code so far is:
Sub prueba()
Dim a As String, b As String, c As String
Dim entero1 As Double, entero2 As Double
Dim resultado As Double
Dim tabla1 As Table
Set tabla1 = ActiveDocument.Tables(1)
a = tabla1.Cell(Row:=1, Column:=3).Range
entero1 = CDbl(a)
End Sub
But when I run it I get an error 13
To evaluate the error add the following two lines to validate if the data type obtained in "a" was a string
MsgBox (TypeName(a))
MsgBox (a)
And I got the following
I believe that the CDbl function does not finish converting the string to double because as they see the chain has a small square, what is not like to erase it so that the conversion is achieved.
Thank you very much for your help.
One way of extracting just the numeric portion of the Range would be to use the Val function, e.g.
entero1 = Val(a)
If the string a contained, for instance, 123.23XYZ4567 then Val(a) would return the number 123.23.
That should ensure that the non-numeric character that you are getting at the end of your Range is removed.
The answer provided by YowE3K is elegant and has my vote. For further information:
That 'small square' is the end of cell marker which is part of Cell.Range.Text (.Text is the default property returned when returning a range object is inappropriate).
To actually remove the end of cell marker (Chr(13) & Chr(7)) you can use something like this:
?CDbl(Replace$(Selection.Range.Cells(1).Range.Text, Chr(13) & Chr(7), vbNullString))
A possible advantage of this approach is that it may provide better opportunity to trap errors if you are only expecting numeric characters.
I am trying to reference a file that has the date of the previous Friday at the end in the form of mm.dd.yy.
I need to now take that date and add it to the end of a string, to end of a string in order to open select the other workbook. This is what I have right now.
File Name:
Submittals Wk Ending 06.02.17.xlsx
This is what I have so far
Dim wrbk As String
Dim weekdate As String
range("a1").value="=TODAY()-WEEKDAY(TODAY())-1"
weekdate = Range("a1").Value
'range("b1").value="06.02.17"
'weekdate = Range("b1").Value
msgbox weekdate 'use to check what the date format is
wrbk = "Submittals Wk Ending " & weekdate
Windows(wrbk & ".xlsx").Activate
When I read it from B2 with the typed in format of 06.02.17 it works, however no matter what I do, I cannot get it to read it from A1 because it changes the format to m/d/yyyy. I have tried to copy it and paste as value. Nothing seems to work.
I have the other workbook open as well when I try to run it.
Any ideas? Thanks!
To get the previous Friday of any date, try below UDF. This should work fine if the Date NumberFormat is same as your System's Date format. The key is the CDate() which converts according to System's Date format which Office apps defaults to.
Option Explicit
Function GetLastFridayDate(AnyDate As Variant) As Date
Dim dInput As Date, dLastFriday As Date
dInput = CDate(AnyDate)
dLastFriday = dInput - Weekday(dInput) + vbFriday - IIf(Weekday(dInput) > vbFriday, 0, 7)
GetLastFridayDate = dLastFriday
End Function
Try
Range("A1").Value = Format$(Date - Weekday(Date) - 1, "MM.DD.YY")
I am trying to pull all data entries that are within a userform selected month and year. I can get the code to run fine when I hard code the year but I want the year to come off of a text box. I converted the Textbox value to an integer using Cint() and dim'd it to "Year" in my if statement. I can get it to work if I write Cdate("3/1/2016"), but I want see if there is a way to run it like: Cdate("3/1/Year"). I tried it this way and get a typematch error on the Cdate Im pretty new to VBA so excuse my stupidity.
Ignore the "Month" variable I was just using that to put a stop on the code and step it through to see if it would enter my if statement.
Thanks in advance.
My Code
Private Sub OKBtn_Click()
Dim Sales As Range
Dim Year As Integer
Dim Month As Integer
Dim i As Integer
Year = CInt(YearText.Value)
Set Sales = Worksheets("Sales").Range("A4")
i = 0
If Sales.Offset(i, 1).Value >= CDate("3/1/2016") And Sales.Offset(i, 1).Value <= CDate(" 3/31/2016 ") Then
Month = 1
End If
In order for the CDate to work, you need to seperate the stings inside the brackets to 2 parts
1.The constant, in your case "3/1/".
2.And the variable, CInt(YearText.Value).
Option Explicit
Private Sub OKBtn_Click()
Dim DDate As Date
DDate = CDate("3/1/" & CInt(YearText.Value))
' for debug only
MsgBox "Date entered is :" & DDate
End Sub
All,
I'm trying to use vlookup in a simple VBA function, but it is continually returning #VALUE!
Here is the code:
Public Function getAreaName(UBR As Integer) As String
Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("UBR Report")
' check level 3 then 2 then 4 then 5
result = Application.WorksheetFunction.VLookup(UBR, sheet.Range("UBRLookup"), Application.WorksheetFunction.Column(sheet.Range("UBRLookup[Level 3]")), False)
getAreaName = result
End Function
Any thoughts?
I'm not quite sure what you're trying to do with the "UBRLookup[Level 3]" reference, but as Joseph has pointed out, that's the bit that you're doing wrong.
[ is not a valid character for a named range in Excel.
The column that you're referencing needs to be a numeric value, the offset from the start of the table-array you've defined as your named range.
The below should work, provided the column you want to pull out is the second column in your named range (e.g. what you're referring to as [level 3] is in the second column).
Public Function getAreaName(UBR As Integer) As String
Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("UBR Report")
result = Application.WorksheetFunction.VLookup(UBR, sheet.Range("UBRLookup"), 2, False)
getAreaName = result
End Function
Update:
I've had a look at Excel 2007 and from what I can see the column function isn't exposed as an Application.WorksheetFunction.
You can use it on the sheet with =Column(D4), but when trying to autocomplete within the vba editor, the function isn't there. This may be due to a difference in versions, so I'll ignore that for now.
It still definitely seems like you're mis-using the third argument. If you really don't want to use the number reference we need to find out where the function is going wrong.
A few tests along the lines of
Debug.Print Application.WorksheetFunction.Column(D4)
Debug.Print sheet.Range("UBRLookup[Level 3]")
should hopefully help to show you exactly where it's going wrong - I believe that it will object to both of the above, but if it returns some useful information then we may be a step closer to your solution.
Break you function up into more pieces. Then debug it and make sure every piece is set up the way you expect.
Example:
Public Function getAreaName(UBR As Integer) As String
Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("UBR Report")
Dim range as Range = sheet.Range("UBRLookup")
Dim column as Column = Application.WorksheetFunction
.Column(sheet.Range("UBRLookup[Level 3]"))
result = Application.WorksheetFunction.VLookup(UBR, range, column, False)
getAreaName = result
End Function
In fact, just by doing that I noticed something weird. You use a range in two different places, but in one place you're looking for UBRLookup, and in another you're looking for UBRLookup[Level 3], is that correct?
I am disturbed by
Dim column as Column =
Application.WorksheetFunction.Column(sheet.Range("UBRLookup[Level 3]"))
You should Dim column as long, I think, and maybe use a variable name that's not to be confused with a property, like lngCol.
This part: sheet.Range("UBRLookup[Level 3]") is suspect as "UBRLookup[Level 3]" is not a valid range name.