Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 范围上的VBA运行时错误1004。清除_Excel_Vba_Runtime Error - Fatal编程技术网

Excel 范围上的VBA运行时错误1004。清除

Excel 范围上的VBA运行时错误1004。清除,excel,vba,runtime-error,Excel,Vba,Runtime Error,有很多关于这个错误的线程,但是无论我怎么做都无法让它工作。大多数人说,当您试图调用非活动工作表上的方法时,就会发生这种情况,但您不应该这样做。错误在第28行。谢谢 Private Sub CommandButton1_Click() Dim x As Integer Dim boisePaste As Integer Dim jrgPaste As Integer Dim

有很多关于这个错误的线程,但是无论我怎么做都无法让它工作。大多数人说,当您试图调用非活动工作表上的方法时,就会发生这种情况,但您不应该这样做。错误在第28行。谢谢

            Private Sub CommandButton1_Click()

            Dim x As Integer 
            Dim boisePaste As Integer
            Dim jrgPaste As Integer
            Dim master As Integer
            Dim lastRow As Integer
            Dim bookCount As Integer
                bookCount = Application.Workbooks.Count


            For x = 1 To bookCount
             If Left(Application.Workbooks(x).Name, 14) = "ITEM_INVENTORY" Then
                boisePaste = x
             ElseIf Left(Application.Workbooks(x).Name, 6) = "report" Then
                jrgPaste = x
             ElseIf Left(Application.Workbooks(x).Name, 8) = "Portland" Then
                master = x
             End If
            next x


            'Unhide sheets and delete Boise range'

            Application.ActiveWorkbook.Sheets("BoisePaste").Visible = True
            Sheets("JRGpaste").Visible = True
            lastRow = Sheets("BoisePaste").Cells(Rows.Count, "B").End(xlUp).Row
            Sheets("BoisePaste").Range(Cells(1,2), Cells(lastRow, 23)).Clear


            'Open Boise file and copy range, paste in master'

            Application.Workbooks(boisePaste).Activate
                With ActiveSheet
                    .Range(.Cells(1,1), .Cells((.Cells(Rows.Count, "A").End(xlUp).Row),22)).Copy
                End With

            Application.Workbooks(master).Sheets("BoisePaste").Range(B1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Application.CutCopyMode = False


            'Open JRG report and copy range, paste in master'

            Application.Workbooks(jrgPaste).Activate
            ActiveSheet.Cells.Copy
            Application.Workbooks(master).Sheets("JRGpaste").Range(A1).Paste
            Application.CutCopyMode = False


            'Refresh pivot tables; hide sheets'
            Application.Workbooks(master).Activate

            With ActiveWorkbook
                .RefreshAll
                .RefreshAll
                .Sheets("BoisePaste").Visible = False
                .Sheets("BoisePaste").Visible = False
            End With

            End Sub

您需要明确说明希望使用
行的工作表。计数和其他此类范围使用(
单元格
,等等)将打开

试试这个:

Sheets("BoisePaste").Range(Sheets("BoisePaste").Cells(1,2), Sheets("BoisePaste").Cells(lastRow, 23)).Clear
所以,仔细检查你的代码,确保你在任何地方都这样做…例如。在
.Range(.Cells(1,1),.Cells((.Cells(Rows.Count,“A”).End(xlUp.Row),22)).Copy
中,您没有对
Rows.Count
执行此操作,因此也要在那里添加工作表,以防止任何意外操作

或许可以这样想,用这句话
myVariable=Sheets(“mySheet”).范围(单元格(1,1)、单元格(1,2)).值

VBA将其解读为

在mySheet
中,查找范围。什么范围?嗯,用户说的是
单元格(1,1)
单元格(1,2)
,但是他想要什么表格呢?当前的活动表名为
yourSheet
…他指定了
范围的位置(表名为
mySheet
),但他没有在
单元格()上,所以我不知道他想要什么
mySheet单元格(1,1)
yourSheet单元格(1,1)

(是的,这正是计算机的思维方式:p)

编辑:我浏览了一遍,试图帮助你收紧代码。但是,正如你可能看到的,我对你想做什么并不十分肯定,但这应该会给你一些帮助/见解:

Private Sub CommandButton1_Click()

Dim x       As Integer
Dim boisePaste As Integer
Dim jrgPaste As Integer
Dim master  As Integer
Dim lastRow As Integer
Dim bookCount As Integer
bookCount = Application.Workbooks.Count

' Create variables to hold the workbook and sheet names.
Dim jrgWS As Worksheet, boiseWS As Worksheet
Dim masterWB As Workbook
Set masterWB = Workbooks(master)
Set jrgWS = Sheets("JRGPaste")
Set boiseWS = Sheets("BoisePaste")

For x = 1 To bookCount
    If Left(Application.Workbooks(x).Name, 14) = "ITEM_INVENTORY" Then
        boisePaste = x
    ElseIf Left(Application.Workbooks(x).Name, 6) = "report" Then
        jrgPaste = x
    ElseIf Left(Application.Workbooks(x).Name, 8) = "Portland" Then
        master = x
    End If
Next x


'Unhide sheets and delete Boise range'

Application.ActiveWorkbook.Sheets("BoisePaste").Visible = True
jrgWS.Visible = True
With boiseWS
    lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    .Range(.Cells(1, 2), .Cells(lastRow, 23)).Clear
End With

'Open Boise file and copy range, paste in master'
'' DONT USE ACTIVE SHEET! Use your variables instead
'Application.Workbooks(boisePaste).Activate
With boiseWS
    'Since you want values (xlPasteValues), just set the two ranges equal instead of copy/paste
    .Range("B1").Value = .Range(.Cells(1, 1), .Cells((.Cells(.Rows.Count, "A").End(xlUp).Row), 22)).Value
End With

'Open JRG report and copy range, paste in master'
' The below just pastes into the same sheet, no??
jrgWS.Cells.Copy
jrgWS.Range("A1").Paste
Application.CutCopyMode = False


'Refresh pivot tables; hide sheets'
Application.Workbooks(master).Activate

With ActiveWorkbook
    .RefreshAll
    .RefreshAll
    .Sheets("BoisePaste").Visible = False
End With

End Sub

您需要明确说明希望使用
行的工作表。计数和其他此类范围使用(
单元格
,等等)将打开

试试这个:

Sheets("BoisePaste").Range(Sheets("BoisePaste").Cells(1,2), Sheets("BoisePaste").Cells(lastRow, 23)).Clear
所以,仔细检查你的代码,确保你在任何地方都这样做…例如。在
.Range(.Cells(1,1),.Cells((.Cells(Rows.Count,“A”).End(xlUp.Row),22)).Copy
中,您没有对
Rows.Count
执行此操作,因此也要在那里添加工作表,以防止任何意外操作

或许可以这样想,用这句话
myVariable=Sheets(“mySheet”).范围(单元格(1,1)、单元格(1,2)).值

VBA将其解读为

在mySheet
中,查找范围。什么范围?嗯,用户说的是
单元格(1,1)
单元格(1,2)
,但是他想要什么表格呢?当前的活动表名为
yourSheet
…他指定了
范围的位置(表名为
mySheet
),但他没有在
单元格()上,所以我不知道他想要什么
mySheet单元格(1,1)
yourSheet单元格(1,1)

(是的,这正是计算机的思维方式:p)

编辑:我浏览了一遍,试图帮助你收紧代码。但是,正如你可能看到的,我对你想做什么并不十分肯定,但这应该会给你一些帮助/见解:

Private Sub CommandButton1_Click()

Dim x       As Integer
Dim boisePaste As Integer
Dim jrgPaste As Integer
Dim master  As Integer
Dim lastRow As Integer
Dim bookCount As Integer
bookCount = Application.Workbooks.Count

' Create variables to hold the workbook and sheet names.
Dim jrgWS As Worksheet, boiseWS As Worksheet
Dim masterWB As Workbook
Set masterWB = Workbooks(master)
Set jrgWS = Sheets("JRGPaste")
Set boiseWS = Sheets("BoisePaste")

For x = 1 To bookCount
    If Left(Application.Workbooks(x).Name, 14) = "ITEM_INVENTORY" Then
        boisePaste = x
    ElseIf Left(Application.Workbooks(x).Name, 6) = "report" Then
        jrgPaste = x
    ElseIf Left(Application.Workbooks(x).Name, 8) = "Portland" Then
        master = x
    End If
Next x


'Unhide sheets and delete Boise range'

Application.ActiveWorkbook.Sheets("BoisePaste").Visible = True
jrgWS.Visible = True
With boiseWS
    lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    .Range(.Cells(1, 2), .Cells(lastRow, 23)).Clear
End With

'Open Boise file and copy range, paste in master'
'' DONT USE ACTIVE SHEET! Use your variables instead
'Application.Workbooks(boisePaste).Activate
With boiseWS
    'Since you want values (xlPasteValues), just set the two ranges equal instead of copy/paste
    .Range("B1").Value = .Range(.Cells(1, 1), .Cells((.Cells(.Rows.Count, "A").End(xlUp).Row), 22)).Value
End With

'Open JRG report and copy range, paste in master'
' The below just pastes into the same sheet, no??
jrgWS.Cells.Copy
jrgWS.Range("A1").Paste
Application.CutCopyMode = False


'Refresh pivot tables; hide sheets'
Application.Workbooks(master).Activate

With ActiveWorkbook
    .RefreshAll
    .RefreshAll
    .Sheets("BoisePaste").Visible = False
End With

End Sub

计算机是按性别思考的吗?@findwindow-这是一个二进制变量,除非另有通知,否则默认为男性PDo计算机以性别思考?@findwindow-这是一个二进制变量,除非另有通知,否则默认为男性P