I am trying to create a variable range that will update and bring in a different range of values based on what month it is.
January C8:D8
February C9:D9
March C10:D10
April C11:D11
May C12:D12
June C13:D13
July C14:D14
August C15:D15
September C16:D16
October C17:D17
November C18:D18
December C19:D19
These are the ranges I need based on what month it is.
My Existing Code:
Sub PullDataFromClosedWorkbook()
Dim Source As Workbook
Set Source = Workbooks.Open("H:\Integration Projects\Chk Req & Customer
Payment Sheet\Chk Reqs\Check Request.xlsm", True, True)
'bring to this workbook
ThisWorkbook.Activate
Worksheets("Sheet1").Range("B2:C2").Formula = Source.Worksheets("Consolidated
Summary").Range("C11:D11").Copy
Worksheets("Sheet1").Range("B2:C2").PasteSpecial xlPasteValues
Source.Close SaveChanges:=False
End Sub
My first idea was to create an if function that based on a drop down list with the months in it, that it could vlookup the month and set the range to that. Not sure how to do this in vba.
One way to do this would be to use the Month Function to return the current month number, which you can then use as an Offset from a starting range.
For example, since it's August, Month(Date) returns 8. If your starting range is C7:D7, Range("C7:D7").Offset(Month(Date)) refers to C15:D15.
So your code might look something like this (UNTESTED).
Sub PullDataFromClosedWorkbook()
Dim Source As Workbook
Set Source = Workbooks.Open("H:\Integration Projects\Chk Req & Customer Payment Sheet\Chk Reqs\Check Request.xlsm", True, True)
ThisWorkbook.Worksheets("Sheet1").Range("B2:C2").Value = Source.Worksheets("Consolidated Summary").Range("C7:D7").Offset(Month(Date)).Value
Source.Close SaveChanges:=False
End Sub
Related
I want a column (with a range) that shows the last date of the last month.
In May, I want 30/04/2022 in every cell from A3:A40.
In June, I want 31/05/2022 and so on.
Please, test the next code. It should do what you need:
Sub testLastDayPrevMonthInRange()
Dim sh As Worksheet, rng As Range, lastD As Date, refDate As Date
Set sh = ActiveSheet
Set rng = sh.Range("A3:A40")
refDate = Date 'you may change it in any date to test the code...
lastD = refDate - (Day(refDate))
rng.value = lastD
End Sub
Please, next time you place a question you should show us a piece of code, like a prove that you made researches on your own. We here do not offer free code writing services... We only help people correcting their code problems and learn.
so I need a macro code for naming a worksheet with the next date on it, as in if the previous sheet is called Tue 27 then the next sheet (new one) should be Wed 28,
my current code only names it as TODAYS date, this is what I am using
Dim szTodayDate As String
szTodayDate = Format(Date, "ddd") & Format(Date, " dd")
On Error GoTo MakeSheet
Sheets(szTodayDate).Activate
Exit Sub
MakeSheet:
Dim Srt As Worksheet
Set Srt = ActiveSheet
Sheets.Add
ActiveSheet.Name = szTodayDate
Is it even possible to do this, and if so, can anyone please tell me how,
Thank you
additional note: so the macro creates a new sheet everytime I run it, and then names it with todays date, I need it instead to name it with the NEXT date, in relation to the previous sheet. so if the last sheet made (prior to macro run) is called "Sun 02" the macro should create a sheet and name it "Mon 03", assume for now that the month doesn't matter, I will not run this macro after the month ends, so on workbook Feb, "Wed 28" would be the last time I run this macro.
Reason Explained: so I need to create a new worksheet everyday for work, but I sometimes end up having to make the worksheet a day later, so lets say on sun 02 I make the sheet on time, so now I have worksheet sun 02, but then I miss it on monday, then on tuesday I make the sheet, it ends up making Sheet Tue 04, so now I'm missing Mon 03.
Possible Alternate: If I could somehow set an IF function that can check to see if worksheet with yesterdays name exists (maybe going back upto 2 days) and if not create it, that would work to. but not sure how to code said IF function either (It would also need to create it and name it as today, if today is 01).
Thank you again
An important note for the following code: in its default state it requires that the Sheet Names include the Month, so the format is "ddd dd mmm" ("Wed 28 Feb") not "ddd dd" ("Wed 28"). If, for example, the Month is stored in the FileName instead then you will need to modify the code.
We need a variable to store the most recent date found, and a Worksheet object to use in a For Each loop. We will check each Worksheet's Name and check if it IsDate. If it is, then we will see if it is later than our currently stored Date. This will fail at year end, unless you start including the Year in the Sheet Name (or File Name) too. (Since 1 Jan comes before 31 Dec)
Dim dMaxDate AS Date, wsForLoop AS Worksheet, sTestString AS String
dMaxDate = DateSerial(1900,1,0) 'Default to 0
For Each wsForLoop In ThisWorkbook.Worksheets 'Loop through every worksheet
sTestString = wsForLoop.Name 'Get the name of the sheet
If InStr(sTestString, " ") > 0 Then sTestString = Mid(sTestString, 1+InStr(sTestString, " ")) 'Remove the Weekday
'If you need to add the Month/Year from your filename, do that here
If IsDate(sTestString) Then 'Only check Worksheets with Dates for Names
If cDate(sTestString) > dMaxDate Then dMaxDate = cDate(sTestString) 'If this is a later date then store it
End If
Next wsForLoop 'Return to start of loop
If dMaxDate < DateSerial(1900, 1, 1) Then dMaxDate = Now-1 'If we have no Worksheets with Dates for Names then default to yesterday
With ThisWorkbook.Worksheets.Add 'Add a new sheet
'Change the Format if you are adding the Month and/or Year from Filename
.Name = Format(dMaxDate+1, "ddd d mmm") 'And Name it the day after our stored date
End With
try using below, you can use counter instead of 1.
SZTodayDate = Format(Date + 1, "ddd") & Format(Date + 1, " dd")
I have a lite problem with some statement in my code. the situation is like this:
I am working with UserForm in excel based on selection of months and the data goes to my report from another report. To analyze data of certain month we need data of the month before. So for January we need data from last's year database for December.
I made special sub just in case we need to analyze data about January and we need the old database too. And with that statment I have a problem and I get that error "VBA End If without block If Error" :
'If the month that was selected is January then we need to open old Visual file Month
If MonthName = Lists.Range("A2").Value Then 'it means January
Call January
Else
For Each ws In Worksheets
If ws.Name = "December" Then
Sheets("December").Delete
End
End If
Next ws
End If
Please, can someone help?
Use Exit For to exit a For Loop.
If MonthName = Lists.Range("A2").Value Then 'it means January
Call January
Else
For Each ws In Worksheets
If ws.Name = "December" Then
ws.Delete
Exit For
End If
Next ws
End If
I am writing my first macro and have a question on how I can select a specific Row based on a value in a specific column. here is my code so far:
Sub Pipeline()
'Module 3
'Iterating through the Funding Date Column and looking for clients going live within 30 days
'Selecting the rows for each client in that target range
'TODO: Export information into an email template in Outlook
'TODO: Send email to distribution list
Dim fundingDate As range
Set fundingDate = range("M4:M500")
Dim todaysDate As Date
todaysDate = Date
For Each cell In fundingDate
If cell < todaysDate + 30 Then
'Need to select the entire row
Else
cell.Font.ColorIndex = 3
End If
Next
End Sub
replace 'Need to select the entire row with
cell.entirerow.select
UPDATE
Here is a much more efficient way to get what you need without all the looping.
In your code Replace from For Each cell ... to Next with this:
With fundingDate
.AutoFilter 1, "<" & todaysDate + 30
.SpecialCells(xlCellTypeVisible).Select
'here are your clients going live in next 30 days
.AutoFilterMode = False
End With
You may need to provide some error checking in case you don't have clients going live within 30 days (SpecialCells method will fail on this) and also, if M4 is not your column header, you may want to adjust how the range picks up the visible cells.
I need to automate a process that's not a one-off event, ~500 facilities, each with 100+ assets which are all scheduled for different dates throughout the year for completion. I have a Workbook set up with my main/source sheet as well as 12 month sheets (Jan, Feb, March, ... Dec). What I need is some sort of code that would allow me to search for a particular date and send it as well as other same-row corresponding data to the appropriate sheet.
For example I have an asset that is due for maintenance in June, 6/17/11. I need for Excel to search for it using the month only, and moving that asset as well as it's name, description, cost, etc to the June tab. Ive managed to get it to locate assets searching for "6/" however it cannot find assets with a date of 6/17/11. It copies all needed data and attempts to move it to the proper sheet, when it makes this attempt a Microsoft Visual Basic error code 400 pops up. Any ideas? All help appreciated.
see if this helps ...
Private Sub FindCells()
'' step 1, find all the rows containing your date (June 2011 dates hardcoded in this example)
Dim CollectionOfRowRanges As New Collection
Dim ws As Worksheet
Dim rgCell As Range
For Each ws In ThisWorkbook.Worksheets
For Each rgCell In ws.UsedRange.Cells
If IsDate(rgCell.Text) Then
If Month(CDate(rgCell.Value)) = 6 And Year(CDate(rgCell.Value)) = 2011 Then
'' for debugging only ... watch and make sure it stops at the right places
ws.Activate
rgCell.Select
Stop
'' end of debug code
Call CollectionOfRowRanges.Add(rgCell.EntireRow)
End If
End If
Next rgCell
Next ws
'' step 2, copy the rows to a new wb
Set ws = Workbooks.Add.Sheets(1)
ws.Name = "June 2011 Rows"
Dim rgRow As Range
Set rgCell = ws.Cells(1, 1)
For Each rgRow In CollectionOfRowRanges
Call rgRow.Copy
Call rgCell.EntireRow.PasteSpecial(xlPasteValues)
Set rgCell = rgCell.Offset(1)
Next rgRow
End Sub