根据日历周动态选择

时间:2018-03-20 15:14:59

标签: excel vba excel-vba dynamic

我对我目前的小项目遇到的问题有疑问。

我有一个团队表,我需要通过电子邮件每周五报告下周的预测。

ExcelSheet

所以我构建了一个为我创建自动电子邮件的宏。

Sub SendMail()

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim VBAWeekNum As Integer

Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection

Set rng = Sheets("Availability List").Range("A1:C7, D1:J7").SpecialCells(xlCellTypeVisible)

On Error GoTo 0

If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected" & _
           vbNewLine & "please correct and try again.", vbOKOnly
    Exit Sub
End If

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = "x@test.de"
    .CC = ""
    .BCC = ""
    .Subject = "X"
    .HTMLBody = "Guten Tag Herr X," & vbCrLf & "anbei wie besprochen die Übersicht für die kommende Woche." & vbCrLf & "Vielen Dank im Voraus." & vbCrLf & "Mit freundlichen Grüßen X" & RangetoHTML(rng)
    .Display 'or use .sent
End With
On Error GoTo 0

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = NothingEnd Sub

Function RangetoHTML(rng As Range)

Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial xlPasteValues, , False, False
    .Cells(1).PasteSpecial xlPasteFormats, , False, False
    .Cells(1).Select
    Application.CutCopyMode = False
    On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
    On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
     SourceType:=xlSourceRange, _
     Filename:=TempFile, _
     Sheet:=TempWB.Sheets(1).Name, _
     Source:=TempWB.Sheets(1).UsedRange.Address, _
     HtmlType:=xlHtmlStatic)
    .Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                      "align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = NothingEnd Function

现在我想自动完成整个过程。因此范围

Set rng = Sheets("Availability List").Range("A1:C7,D1:J7").SpecialCells(xlCellTypeVisible)

未正确定义。我希望第二部分

D1:J7").SpecialCells(xlCellTypeVisible)根据实际的日历周而移动。

E.g。本周它应该选择CW13(意味着K1:Q7)。

有人有想法吗?那太棒了!

提前致谢!

6 个答案:

答案 0 :(得分:1)

不是使用Range("A1:C7, D1:J7"),而是定义单独的范围变量,其中第二个范围(r2)可以根据需要根据i = 0,1,2的值进行偏移...

代码看起来像这样

dim r as range, r1 as range, r2 as range, i as integer
set r1=range("A1:C7")
set r2=range("D1:J7")
set r = range(r1,r2.offset(0,7*i))

答案 1 :(得分:0)

周数单元格是否合并了单元格?如果是,请使用OFFSETMATCH以及WEEKNUM,以OFFSET(MATCH(WEEKNUM(TODAY())为基础

答案 2 :(得分:0)

首先定义一个weeknumber变量。您可以使用= WEEKNUM(TODAY())执行此操作 让我们说这个变量叫做x。 然后我会像这样继续

Set rng1 = Sheets("Availability List").Range(Cells(1,1),Cells(7,3)).SpecialCells(xlCellTypeVisible)
Set rng2 = Sheets("Availability List").Range(Cells(1,x),Cells(7,x+6)).SpecialCells(xlCellTypeVisible)

请告诉我这是否是你想要的。 Viel Spass damit。

答案 3 :(得分:0)

你可以尝试

    With Sheets("Availability List")
        Set rng = Union(.Range("A1:C7"), _
                        .Rows(2).Find(what:=WorksheetFunction.WeekNum(Date), LookIn:=xlValues, lookat:=xlWhole).Offset(-1).Resize(7)). _
                        SpecialCells(xlCellTypeVisible)
    End With

答案 4 :(得分:0)

以下行将根据周数选取您的范围:

Set Rng = [2:2].Find(Application.WorksheetFunction.WeekNum(Date)).Resize(1, 7)

它的工作原理是在第2行搜索星期数,然后在7之后搜索整周的范围。

我要提出的唯一警告是确保周数的返回与周数的定义相匹配,但这可以使用详细here的参数进行更改。

此外,我会根据相关表格将[2:2]更改为对该行的更健壮的引用。

如果您需要在今年过去,例如进入第53周,那么我还会根据年份添加偏移量,但我会假设您在一张纸上的数据不会超过52周。

答案 5 :(得分:0)

在合并区域列上的行上使用“相交”。

Dim wn As Long, rng As Range
wn = 13
With Sheets("Availability List")
    Set rng = Union(.Range("A1:C7"), _
                    Intersect(.Rows("1:7"), .Cells(2, Application.Match(wn, .Rows(2), 0)).MergeArea.EntireColumn))
    Debug.Print rng.SpecialCells(xlCellTypeVisible).Address
End With

我不清楚为什么需要特殊电池。您的助手功能可能需要调整才能使用区域。