My Assistant
![]() ![]() |
|
|
Apr 6 2012, 01:27 PM
Post
#1
|
|
|
UtterAccess Addict Posts: 220 From: Oklahoma |
I have code that looks for a particular month and copies then pastes that same data in the same spot. This is bc the data changes to the next month every month and there are formulas that reference this other file. So instead of having to manually go though and copy paste values only I used the following code. But now I want to know if there's a way for excel to know what month it is and look for the previous month instead of me having to change the month in the code everytime.
Sub CopyMonth() 'Copy cells of cols C,D,G,H from rows containing "Significant" in 'col B of the active worksheet (source sheet) to cols Dim DestSheet As Worksheet Set DestSheet = Worksheets("Overtime Charts") Dim sRow As Long 'row index on source worksheet Dim sCount As Long sCount = 0 sRow = 1 For sRow = 1 To Range("B2500").End(xlUp).Row 'use pattern matching to find "Apr" anywhere in cell If Cells(sRow, "B") Like "*Apr*" Then sCount = sCount + 1 'copy cols C,D,G,H DestSheet.Cells(sRow, "C") = Cells(sRow, "C") DestSheet.Cells(sRow, "D") = Cells(sRow, "D") DestSheet.Cells(sRow, "G") = Cells(sRow, "G") DestSheet.Cells(sRow, "H") = Cells(sRow, "H") End If Next sRow MsgBox sCount & " Apr rows copied", vbInformation, "Transfer Done" End Sub |
|
|
|
Apr 6 2012, 01:31 PM
Post
#2
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
So you want it to look for the current month you're in? or the one before the one you're in? If the former, try:
CODE Sub CopyMonth()
dim myMonth as String myMonth=Format(Now,"Mmm") 'Copy cells of cols C,D,G,H from rows containing "Significant" in 'col B of the active worksheet (source sheet) to cols Dim DestSheet As Worksheet Set DestSheet = Worksheets("Overtime Charts") Dim sRow As Long 'row index on source worksheet Dim sCount As Long sCount = 0 sRow = 1 For sRow = 1 To Range("B2500").End(xlUp).Row 'use pattern matching to find "Apr" anywhere in cell If Cells(sRow, "B") Like "*" & myMonth & "*" Then sCount = sCount + 1 'copy cols C,D,G,H DestSheet.Cells(sRow, "C") = Cells(sRow, "C") DestSheet.Cells(sRow, "D") = Cells(sRow, "D") DestSheet.Cells(sRow, "G") = Cells(sRow, "G") DestSheet.Cells(sRow, "H") = Cells(sRow, "H") End If Next sRow MsgBox sCount & " Apr rows copied", vbInformation, "Transfer Done" End Sub This post has been edited by ipisors: Apr 6 2012, 01:31 PM |
|
|
|
Apr 6 2012, 01:34 PM
Post
#3
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
And if you really wanted LAST MONTH, then instead of this line:
CODE myMonth=Format(Now,"Mmm") use this line: CODE mymonth = Month(Now) - 1
mymonth = Left(MonthName(mymonth), 3) |
|
|
|
Apr 6 2012, 01:34 PM
Post
#4
|
|
|
UtterAccess Veteran Posts: 354 |
Hi,
If today is in May, you would be changing CODE If Cells(sRow, "B") Like "*Apr*" Then to CODE If Cells(sRow, "B") Like "*May*" Then ?
|
|
|
|
Apr 6 2012, 01:35 PM
Post
#5
|
|
|
UtterAccess Guru Posts: 594 |
Ipisors beat me to it, you can also consider using a combination of the MonthName and Date functions
|
|
|
|
Apr 6 2012, 01:36 PM
Post
#6
|
|
|
UtterAccess Addict Posts: 220 From: Oklahoma |
Thank you so much but I need it to look for the previouse month. So since we're in April right now I'd want it to say current month minus one so that'd it look for March.
|
|
|
|
Apr 6 2012, 01:39 PM
Post
#7
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
Ok, so use the code I posted here
|
|
|
|
Apr 6 2012, 01:40 PM
Post
#8
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
Oh and if you want to be really certain about the case, you can even enhance my code to convert to Proper, as in your original example.
So final would be: CODE mymonth = Month(Now) - 1
mymonth = Strconv(Left(MonthName(mymonth), 3),vbpropercase) This post has been edited by ipisors: Apr 6 2012, 01:40 PM |
|
|
|
Apr 6 2012, 01:47 PM
Post
#9
|
|
|
UtterAccess Addict Posts: 220 From: Oklahoma |
Worked thank you very much!
|
|
|
|
Apr 6 2012, 01:48 PM
Post
#10
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
I'm glad it worked out !! good luck with your project -
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 25th May 2013 - 12:11 AM |