在程序级别设置变量以在公共场合使用? "对象要求错误"

时间:2017-12-11 22:29:28

标签: excel excel-vba vba

MODULE01

2 : undefined
2 : undefined
2 : undefined
...
1 : data
1 : data
1 : data
...
3 ---------------------------------------------

MODULE02

Option Explicit
Public target_des As Range, target_tot As Range
Private Sub get_columns_letters()
'
'
' alot of lines which is irrelevant

' to set the target column as the total column

If Not tot_tot_col_U Is Nothing Then
    target_tot = tot_tot_col_U
ElseIf Not tot_totalcell Is Nothing Then
    target_tot = tot_totalcell
Else
    target_tot = ActiveSheet.Range("U")
End If
' to set the target column as description col
If Not description_col_U Is Nothing Then
    target_des = description_col_U
ElseIf Not Descell Is Nothing Then
    target_des = Descell
Else
    target_des = ActiveSheet.Range("B")
End If

End Sub

用于标识target_tot的所有变量都在模块1中定义,因此当我在模块1中使用debug时,它会显示target_tot的正确地址,但在第二个模块中,它似乎又回到了无关或不再被识别。 一些用于使程序找到目标列的公共声明" target_tot"是:

Private Sub Organize_other()

        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False

        Sheets("Project cost proposal").Activate
        Dim fstcell As Range
        Dim lstcell As Range
        Set fstcell = Cells(1, 1)
        Set lstcell = Cells(ActiveSheet.Rows.Count, ActiveSheet.Columns.Count)
        Range(fstcell, lstcell).Select
        Application.CutCopyMode = False
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False

        Dim Firstrow As Long
        Dim lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long

        With Sheets("Project cost proposal")
            .Select
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
            .DisplayPageBreaks = False

            Firstrow = .UsedRange.Cells(1).Row
            lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
            For Lrow = lastrow To Firstrow Step -1
                Select Case Lrow
                    Case 1, 2, 3
                    Case Else

                    With .Cells(Lrow, target_tot.Column) ' HERE IS MY PROBLEM
                        If Not IsError(.Value) Then

                            If .Value = "0" Or .Value = "" Then 
                               .EntireRow.Delete
                        End If

                    End With
                End Select
            Next Lrow
            End With

            ActiveWindow.View = ViewMode

            ' RUN OTHER MACROS TO COMPLETE ORGANIZING SHEET
            Range("A1").Select
            Application.Run "module07.AssignUnique"
            Application.Run "module11.assign_formula_to_projectoverall"

            ' then adjust column and rows
            Worksheets("project cost proposal").Activate

            With Application

                ' to adjust cell size and auto fit
                Cells.Select
                Selection.Rows.AutoFit
                Selection.UnMerge

                Range(Cells(1, 1), Cells(ActiveSheet.Rows.Count, ActiveSheet.Columns.Count)).Select
                Selection.WrapText = False
                Selection.Rows.AutoFit
            End With
End Sub

它总是给我第二个模块中所需的错误对象target_tot什么都没有任何帮助?

0 个答案:

没有答案