使用SELECT CASE管理生产表

时间:2014-05-21 12:35:36

标签: vba excel-2007 increment select-case

我尝试使用Select Case来记录生产时间表。 我尝试了几个角度 - 一个是Select Case。 每次运行宏我都会得到默认的错误"错误的模型输入...."

Private Sub Transfer()

Dim ModR As String
Dim Count1 As Integer
Dim Count2 As Integer
Dim Count3 As Integer
Dim Count4 As Integer
Dim Count5 As Integer

Count1 = 0
Count2 = 0
Count3 = 0
Count4 = 0
Count5 = 0

ModR = Worksheets(1).Range("B3").Value

Select Case ModR
  Case "Model 1"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B3:B5").Offset(Count1, 0).PasteSpecial
    Count1 = Count1 + 1
  Case "Model 2"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B7:B9").Offset(Count2, 0).PasteSpecial
    Count2 = Count2 + 1
  Case "Mode1 3"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B11:B13").Offset(Count3, 0).PasteSpecial
    Count3 = Count3 + 1
  Case "Model 4"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B15:B17").Offset(Count4, 0).PasteSpecial
    Count4 = Count4 + 1
  Case "Model 5"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B19:B21").Offset(Count5, 0).PasteSpecial
    Count5 = Count5 + 1
  Case Else
    MsgBox "Wrong Model Entered / Model Does Not Exist"
  End Select
End Sub

Public Sub call_Transfer()
  Call Transfer
End Sub

我认为问题在于

ModR = Worksheets(1).Range("B3").Value

1 个答案:

答案 0 :(得分:0)

为了使增量起作用,您必须使用全局变量。此外,您的偏移会导致单元格向下移动而不是向右移动。我改变了你的代码。它将增加偏移并在新的宏执行时将其移到右侧。

Dim Count1 As Integer
Dim Count2 As Integer
Dim Count3 As Integer
Dim Count4 As Integer
Dim Count5 As Integer

Private Sub Transfer()

Dim ModR As String

ModR = Worksheets(1).Range("B3").Value

Select Case ModR
  Case "Model 1"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B3:B5").Offset(0, Count1).PasteSpecial
    Count1 = Count1 + 1
  Case "Model 2"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B7:B9").Offset(0, Count2).PasteSpecial
    Count2 = Count2 + 1
  Case "Model 3"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B11:B13").Offset(0, Count3).PasteSpecial
    Count3 = Count3 + 1
  Case "Model 4"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B15:B17").Offset(0, Count4).PasteSpecial
    Count4 = Count4 + 1
  Case "Model 5"
    Worksheets(1).Range("B4:B6").Copy
    Worksheets(2).Range("B19:B21").Offset(0, Count5).PasteSpecial
    Count5 = Count5 + 1
  Case Else
    MsgBox "Wrong Model Entered / Model Does Not Exist"
  End Select
End Sub

Public Sub call_Transfer()
  Call Transfer
End Sub

只需将其粘贴到您的模块中即可。我希望它能做你想要的。