无法解决错误55& VBA宏中的91问题

时间:2017-07-24 08:10:28

标签: excel vba excel-vba

我遇到了以下错误。

  1. 对象变量或未设置块变量(错误91)
  2. 文件已经打开
  3. 第一次运行宏时,会显示错误1。 第二次运行宏后,出现错误2。 我相信我已经设置并且还在代码中发布了该文件。 我在下面发布了部分代码。

    (If sheet_name = "sheet1" Then)内的一切顺利但错误2 显示在其他条件(If sheet_name = "sheet2 & 3" Then)一些样本或示例会很棒,我很乐意听取您的意见!

    Dim fileNo As Integer
    Dim sheet_name As String
    Dim csv_file As String
    
    Dim mainworkBook As Workbook
    Dim namesList As Object
    
    Dim 1Rng As range
    Dim 2Rng As range
    Dim 3Rng As range
    
    Set mainworkBook = ActiveWorkbook '
    Set namesList = mainworkBook.Sheets("sample_work")
    
    Set 1Rng = range(namesList.Cells(3, 1), namesList.Cells(9, 1)) 
    Set 2Rng = range(namesList.Cells(3, 3), namesList.Cells(49, 3))
    Set 3Rng = range(namesList.Cells(3, 5), namesList.Cells(1780, 5))
    
    
    
    
    fileNo = FreeFile
    Open csvFilePath For Output As #fileNo
    For iCount = 2 To maxRow
        For jCount = 2 To maxCol - 1
           cellValue = Cells(iCount, jCount)
                If sheet_name = "sheet1" Then
                ' convert the keys into the values
                    If jCount = 7 And Not iCount = 2 Then
                        cellValue = 1Rng.Find(cellValue).Offset(0, 1).value
                    End If
                    If jCount = 8 And Not iCount = 2 Then
                           cellValue = 2Rng.Find(cellValue).Offset(0, 1).value
                     End If
                End If
                    '====================================================-
                If sheet_name = "sheet2" Then
                    If jCount = 8 And Not iCount = 2 Then 
                        cellValue = 1Rng.Find(cellValue).Offset(0, 1).value
                    End If
                    If jCount = 8 And Not iCount = 2 Then 
                         cellValue = 2Rng.Find(cellValue).Offset(0, 1).value
                    End If
               End If
                If sheet_name = "sheet3" Then
                           cellValue = 3Rng.Find(cellValue).Offset(0, 1).value
                    End If
                End If
    
            Write #fileNo, cellValue;
            kCount = jCount
        Next jCount
         cellValue = Cells(iCount, kCount + 1)
        Write #fileNo, cellValue
    Next iCount
    Close #fileNo
    

0 个答案:

没有答案