简体   繁体   English

选择带变量的范围,运行时错误1004

[英]Selecting Range with variables, Run time error 1004

VBA keeps giving run time error'1004': Application defined or object defined error for the code snippet below; VBA不断给出运行时错误“ 1004”:以下代码段的应用程序定义或对象定义错误;

Sub deneme()

    Dim a As Long
    Dim b As Integer

    For a = 12 To 13

        For b = 2 To 3

            Sheets(2).Select

            ActiveSheet.Cells(a, 1).Select

            Sheets.Add After:=Sheets(Sheets.Count)

            ActiveSheet.Name = Worksheets(2).Cells(a, 1).Value & " " & Left(Worksheets(2).Cells(a, 2).Value, 18)

            ActiveSheet.Range("A1:D1").Select

            Selection.Merge

            ActiveCell.Select

            ActiveCell.FormulaR1C1 = Worksheets(2).Cells(a, 1).Value

            With Selection
                .HorizontalAlignment = xlCenter
            End With

            Sheets(Sheets.Count).Select

            ActiveSheet.Range(Cells(b, 1), Cells(b, 2)).Select

            Selection.Merge

            ActiveCell.Select

            ActiveCell.FormulaR1C1 = Worksheets(2).Cells(8, b + 1)

            With Selection
                .HorizontalAlignment = xlCenter
            End With

            ActiveSheet.Range(Cells(b, 3), Cells(b, 4)).Select

            Selection.Merge

            ActiveCell.Select

            ActiveCell.FormulaR1C1 = Worksheets(2).Cells(a, b + 1).Value

            With Selection
                .HorizontalAlignment = xlCenter
            End With

        Next b
    Next a
End Sub

Two different variables a and b is working normally when seperated, however when i wrote them down together vba gives 1004 error. 分开时,两个不同的变量a和b正常工作,但是当我将它们一起写下时,vba给出1004错误。

I use the code for taking different values and arranging them in a new worksheet. 我使用代码来获取不同的值并将它们安排在新的工作表中。 This is only a small part of it which was working normally before. 这只是以前正常工作的一小部分。

This line is the problem: 这行是问题所在:

 ActiveSheet.Name = Worksheets(2).Cells(a, 1).Value & " " & Left(Worksheets(2).Cells(a, 2).Value, 18)

You have it inside: 您在里面:

For b = 2 To 3

 Next b

As you don't account for the second loop of b you are trying to create a sheet with the same name and hence the error. 由于您没有考虑b的第二个循环,因此您试图创建一个具有相同名称的工作表,从而导致错误。 You need to find a way to account for the fact you are doing two loops with b instead of the outer loop for a - which you do account for. 您需要找到一种方法来说明您正在使用b进行两个循环,而不是您要说明的-的外部循环。

I think you probably want to move everything that doesn't involve b, including the sheet add and name, to before the b loop. 我想您可能想将不涉及b的所有内容(包括工作表的add和name)移到b循环之前。

Something like, though hard without seeing your data: 尽管很难看到您的数据,但有些类似的东西:

Option Explicit

Public Sub deneme()

    Dim a As Long
    Dim b As Integer

    For a = 12 To 13

        Sheets(2).Select

        ActiveSheet.Cells(a, 1).Select

        Sheets.Add After:=Sheets(Sheets.Count)

        ActiveSheet.Name = Worksheets(2).Cells(a, 1).Value & " " & Left(Worksheets(2).Cells(a, 2).Value, 18)

        ActiveSheet.Range("A1:D1").Select

        Selection.Merge

        ActiveCell.Select

        ActiveCell.FormulaR1C1 = Worksheets(2).Cells(a, 1).Value

        With Selection
            .HorizontalAlignment = xlCenter
        End With

        For b = 2 To 3

            Sheets(Sheets.Count).Select

            ActiveSheet.Range(Cells(b, 1), Cells(b, 2)).Select

            Selection.Merge

            ActiveCell.Select

            ActiveCell.FormulaR1C1 = Worksheets(2).Cells(8, b + 1)

            With Selection
                .HorizontalAlignment = xlCenter
            End With

            ActiveSheet.Range(Cells(b, 3), Cells(b, 4)).Select

            Selection.Merge

            ActiveCell.Select

            ActiveCell.FormulaR1C1 = Worksheets(2).Cells(a, b + 1).Value

            With Selection
                .HorizontalAlignment = xlCenter
            End With

        Next b
    Next a
End Sub

Primarily: You should include a test for the existence of a sheet name before trying to name a sheet. 主要是:在尝试命名工作表之前,应包括一个工作表名称是否存在的测试。 What happens if different rows in column A lead to you trying to create a sheet with the same name? 如果A列中的不同行导致您尝试创建具有相同名称的工作表会怎样?

After that, you should start looking at using worksheet variables, avoiding .Select , using With statements and Option Explicit , looking at ways .FormulaR1C1 can be applied to an entire range rather than in a loop to the current ActiveCell and pulling out repeated lines of code into functions. 之后,您应该开始着眼于使用工作表变量,避免使用.Select ,使用With语句和Option Explicit ,而是使用.FormulaR1C1可以应用于整个范围,而不是循环到当前ActiveCell并退出重复的行。代码转换成函数。 Could also used typed function Left$ rather than Left . 也可以使用类型化的函数Left$而不是Left

The code I posted also depends on what you actually wanted to happen if the same sheet name came up.. did you want to create a new name or did you want to use the existing sheet. 我发布的代码还取决于如果出现相同的工作表名称时您实际想要发生的情况。.是否要创建新名称或要使用现有工作表。 All stuff error handling on sheet and a test for sheet exists could deal with. 可以处理工作表上的所有填充错误并进行工作表测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM