繁体   English   中英

如何从 excel 表中的单元格值获取单元格范围?

[英]How to get a cells range from cell value in excel sheet?

我想从 excel 的单元格中的值中提取一个范围。 使用此代码,我每次都会出错,但当我手动输入行和列索引号时不会。 这是代码:

Sub Checker()
    With Application
        .DisplayAlerts = False
        .EnableEvents = False
    End With

    Dim folderpath As String
    Dim workbookname As String
    Dim filepath As String
    
    folderpath = Range("B3").Value
    workbookname = Range("B6").Value
    
    filepath = folderpath + workbookname
    
    Workbooks.Open Filename:=filepath
    
    Range("A1").Select
    
    Dim last_cell As Variant
    Dim last_column As Variant
    Dim last_row As Variant
        
    last_column = Range("B12").Value
    last_row = Range("E12").Value
    last_cell = Range("B15").Value
    
    Dim rng As Range, cell As Range
    
    Set rng = Range(Cells(1, 1), Cells(last_row, last_column))
    
    For Each cell In rng
        If cell.Locked = True Then

        Else
            cell.Value = "N/P"
        End If 
    Next cell

    With Application
        .DisplayAlerts = True
        .EnableEvents = True
    End With    
End Sub

最后一列应该是“13”,最后一行应该是“51”

但每次我收到错误1004。

问题出在set rng

将行/列变量定义为Long并为位于工作表中的每个object 指定工作簿和工作表。

Option Explicit

Public Sub Checker()
    With Application
        .DisplayAlerts = False
        .EnableEvents = False
    End With

    Dim folderpath As String
    folderpath = ThisWorkbook.Worksheets("Sheet1").Range("B3").Value

    Dim workbookname As String
    workbookname = ThisWorkbook.Worksheets("Sheet1").Range("B6").Value

    Dim filepath As String
    filepath = folderpath & workbookname 'concatenate strings with & not +
    
    Dim OpenedWb As Workbook  ' set the opened workbook as a variable so you can reference it later 
    Set OpenedWb = Workbooks.Open(Filename:=filepath)

    Dim ws As Worksheet  ' define the worksheet you want to use in that workbook
    Set ws = OpenedWb.Worksheets(1) 'select your sheet by tab position or
    'Set ws = OpenedWb.Worksheets("Sheet1") 'select your sheet by tab name
      
    Dim last_column As Long
    last_column = ws.Range("B12").Value

    Dim last_row As Long
    last_row = ws.Range("E12").Value
    
    Dim rng As Range, 
    Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(last_row, last_column))
    
    Dim cell As Range
    For Each cell In rng
        If Not cell.Locked = True Then
            cell.Value = "N/P"
        End If 
    Next cell

    'OpenedWb.Close SaveChanges:=True  'if you want to close and save

    With Application
        .DisplayAlerts = True
        .EnableEvents = True
    End With    
End Sub

请注意,如果您禁用事件,请确保在任何错误情况下使用错误处理来启用事件,否则它们将被关闭,直到您关闭 Excel。

例如

Public Sub Example()
    Application.EnableEvents = False
    On Error Goto SAVE_EXIT

    ' your code here …

    Application.EnableEvents = True
    On Error Goto 0  ' re-enable error reporting
   
    Exit Sub
SAVE_EXIT:  ' in case of error enable events
    Application.EnableEvents = True
    ' and show error message
    If Err.Number Then
        Err.Raise Err.Number
    End If
End Sub

暂无
暂无

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

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