简体   繁体   English

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

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

I want to extract a range from values in cells in excel.我想从 excel 的单元格中的值中提取一个范围。 with this code i get a error every time, but not when i put the row and column index numbers manually.使用此代码,我每次都会出错,但当我手动输入行和列索引号时不会。 This is the code:这是代码:

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

the last column should be "13" and the last row should be "51"最后一列应该是“13”,最后一行应该是“51”

but every time I get error 1004.但每次我收到错误1004。

the problem is in the set rng问题出在set rng

Define your row/column variables as Long and specify a workbook and worksheet for every object that is located in a worksheet.将行/列变量定义为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

Note that if you disalbe events, then make sure you use error handling to enable events in any case of error or they will be turned of until you close Excel.请注意,如果您禁用事件,请确保在任何错误情况下使用错误处理来启用事件,否则它们将被关闭,直到您关闭 Excel。

For Example例如

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.

相关问题 如何在Excel工作表中获取占用单元格的范围 - How to get the range of occupied cells in excel sheet 如果单元格在Excel中匹配,则从范围中获取值 - Get value from range if cells match in Excel 如何获取单元格值作为Excel工作表的货币 - How to get the cell value as a currency of excel sheet 如何从Excel VBA中获取其他表格中的数据的一系列单元格? - How to get a range of cells in excel vba taking data from other sheet? Macro Excel可根据单元格匹配将范围单元格从一张纸复制到另一张纸,如果不匹配,则跳过单元格 - Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match Excel从搜索值在单元格内的范围中查找单元格 - Excel find cells from range where search value is within the cell 从范围中获取Excel中的单元格值 - Get Cell Value in Excel from Range 如何计算值小于excel中另一个单元格的范围内的单元格? - How to count cells in a range with a value less than another cell in excel? Excel 从垂直单元格表引用到水平单元格获取值 - Excel get value from vertical cell sheet reference to horizontally cell 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM