子例程中的已定义变量(长)更改未更新

时间:2017-04-09 18:56:44

标签: excel vba variables dimension

我有一个用户表单可以在工作表上移动对象(员工姓名),以便管理员可以在必要时将员工移动到不同的部门(工作表上的不同行分组)。我遇到的问题是,当我定义属于员工姓名的初始行,然后通过Selection.Cut和Selection.Insert Shift:= xlDown移动该行时,我无法再调用该位置的新位置。 employee row,因为它已更改,并且后面的任何代码行仍然识别userform代码中标识的初始员工行。

Dim UserRow As Long
Dim UserMatrix As String

Dim CCIRow As Long
Dim CCIRange As String
Dim CEMRow As Long
Dim CEMRange As String
Dim MANRow As Long
Dim MANRange As String

UserRow = Application.WorksheetFunction.Match(UserName, Range("A1:A300"), 0)
UserMatrix = "B" & UserRow

    CCIRow = Application.WorksheetFunction.Match("CCI / MDI", Range("A1:A300"), 0) + 1
    Let CCIRange = "A" & CCIRow
    CEMRow = Application.WorksheetFunction.Match("CE / Marketing", Range("A1:A300"), 0) + 1
    Let CEMRange = "A" & CEMRow
    MANRow = Application.WorksheetFunction.Match("Management", Range("A1:A300"), 0) + 1
    Let MANRange = "A" & MANRow
    OTHRow = Application.WorksheetFunction.Match("Other", Range("A1:A300"), 0) + 1

Sheet3.Rows(UserRow).Select
Selection.Cut

If UserDept = "CCI / MDI" Then
Sheet3.Rows(CCIRow).Select
    Selection.Insert Shift:=xlDown

    Sheet3.Range(UserMatrix).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Clear                                                             

    Sheet3.Range("MATRIX_ROW").Select
    Selection.Copy                                                              

    Sheet3.Range(UserMatrix).Select
    Selection.PasteSpecial PASTE:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Sheet3.Range(CCIRange).Select
    Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Sort _
        key1:=Range(CCIRange), order1:=xlAscending
End If

If UserDept = "CE / Marketing" Then
Sheet3.Rows(CEMRow).Select
    Selection.Insert Shift:=xlDown

    Sheet3.Range(UserMatrix).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Clear                                                             

    Sheet3.Range("MATRIX_ROW").Select
    Selection.Copy                                                              

    Sheet3.Range(UserMatrix).Select
    Selection.PasteSpecial PASTE:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Sheet3.Range(CEMRange).Select
    Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Sort _
        key1:=Range(CEMRange), order1:=xlAscending
End If

If UserDept = "Management" Then
Sheet3.Rows(MANRow).Select
    Selection.Insert Shift:=xlDown

    Sheet3.Range(UserMatrix).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Clear

    Sheet3.Range("MATRIX_ROW").Select
    Selection.Copy

    Sheet3.Range(UserMatrix).Select
    ActiveCell.Offset(0, 1).Select
    Selection.PasteSpecial PASTE:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Sheet3.Range(MANRange).Select
    Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Sort _
        key1:=Range(MANRange), order1:=xlAscending
End If

我的问题:有没有什么办法可以在子程序中更新定义维度变量(即Dim)的值?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

如果您要求可以更改UserRow的值,则答案为“是”。 您所要做的就是为其分配粘贴位置的行号值。

我无法理解包含这么多SelectActiveCellSelection位的代码,但它可能类似于UserRow = CCIRow

(或者,您可以在移动后再次使用UserRow = Application.WorksheetFunction.Match(UserName, Range("A1:A300"), 0)语句重置它。)