Vba 根据日历周进行动态选择

Vba 根据日历周进行动态选择,vba,excel,dynamic,Vba,Excel,Dynamic,我有一个关于我目前的小项目所面临的问题的问题 我有一份团队工作表,我需要每周五通过电子邮件报告下周的预测 因此,我建立了一个宏,为我创建一个自动电子邮件 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

我有一个关于我目前的小项目所面临的问题的问题

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

因此,我建立了一个宏,为我创建一个自动电子邮件

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”)。特殊单元格(xlCellTypeVisible)
根据实际日历周移动

例如,本周应选择CW13(表示K1:Q7)

有人有主意吗?那太好了


提前感谢!

周数单元格是否合并了单元格?如果是,请使用
OFFSET
MATCH
WEEKNUM
OFFSET(MATCH(WEEKNUM(TODAY())
为基础,而不是使用
范围(“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))
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)

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

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))
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)
你可以试试

    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

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

Set Rng = [2:2].Find(Application.WorksheetFunction.WeekNum(Date)).Resize(1, 7)
它的工作原理是搜索第2行的周数,然后按7进行偏移以捕获整个周数范围

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

此外,我将根据所讨论的工作表将
[2:2]
更改为对行的更可靠引用


如果您需要移过今年,例如移到第53周,那么我也会根据年份添加一个偏移量,尽管我会假设您在一张工作表中的数据不会超过52周。

使用合并区域列上的行上的相交

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

我不清楚为什么需要特殊单元格。您的助手功能可能需要调整以适应区域。

第13周开始后,第12周会发生什么情况?是否删除列并将数字转换?