[英]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.