删除工作表的列,直到可变范围

时间:2014-06-16 15:24:34

标签: excel excel-vba vba

我有一个电子表格,其中C之后的列都与特定周数相关,例如第1周,第2周。我需要保留A-C列,但是我需要删除除CURRENT周数和右边两列之外的所有列。

这是一个相当大的电子表格,当前的周数现在高达Collumn CK,但这必须每周自我更新。

周没有。显示在第二行。

此外,我需要复制当前周的单元格以及与其相邻的两列。

请帮助我,我已经搜索了解决方案的所有地方并尝试了所有方法。

我可以使用

获取当前周数
iNumberOfTheWeek = DatePart("ww", Now()) 
'// current number of of the week in the year

但不知道如何利用这个

1 个答案:

答案 0 :(得分:0)

我认为SO建议在问题中显示更多的努力,但无论如何,这对你来说很重要:

Sub Sub1()
  Dim iNumberOfTheWeek&, FindCol&
  Dim FindRange As Range, delRange As Range, copyRange As Range
  iNumberOfTheWeek = DatePart("ww", Now())
  ' the range to search
  Set FindRange = Range(Cells(2, 4), Cells(2, Columns.Count))
  Debug.Print FindRange.Address
  ' find the current week
  Set FindRange = FindRange.Find(iNumberOfTheWeek, LookIn:=xlValues)
  If FindRange Is Nothing Then MsgBox "Not found": End
  Debug.Print FindRange.Address
  FindCol = FindRange.Column
  ' the first (left) range to delete
  Set delRange = Range(Columns(4), Columns(FindCol - 1))
  Debug.Print delRange.Address
  delRange.Delete
  ' the second (right) range to delete
  Set delRange = Range(Columns(7), Columns(Columns.Count))
  Debug.Print delRange.Address
  delRange.Delete
  ' the range to copy
  Set copyRange = Columns(3).Resize(Rows.Count, 3) ' 3, 4, and 5
  Debug.Print copyRange.Address
  copyRange.Copy
  '?? do what
End Sub