0

我正在尝试使用 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
4

1 回答 1

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

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

于 2014-05-21T13:11:59.087 回答