简体   繁体   中英

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

I want to extract a range from values in cells in 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"

but every time I get error 1004.

the problem is in the set rng

Define your row/column variables as Long and specify a workbook and worksheet for every object that is located in a worksheet.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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