簡體   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