我的代码部分如下;
Dim Column1 As String
Worksheets("Graph").Activate
Column1 = Cells(1, "P").Value
Dim Column2 As String
Worksheets("Graph").Activate
Column2 = Cells(2, "P").Value
Cells.Find(What:=Column1).Activate
StartP3Column = ActiveCell.Column
Cells.Find(What:=Column2).Activate
StartP4Column = ActiveCell.Column
EndP3Column = StartP4Column - 1
Cells.Find(What:="End Column").Activate
StartEndColumn = ActiveCell.Column
EndP4Column = StartEndColumn - 1
我正在努力使P1和P2中的值确定运行宏时选择了哪些列。因此,在前两个Cell.Find(What:=)
函数中,我希望“Column1”获取Column1变量(P1中的值)。
当我运行宏时,我收到错误“运行时错误:91。没有设置块变量的对象变量”,当我调试时,问题出在行Cells.Find(What:=Column1).Activate
有什么想法吗?
答案 0 :(得分:0)
如果您在工作表Sheet1
上搜索这些数据,则会运行:
Dim wsG As Worksheet
Dim wsS As Worksheet
Dim Rg As Range
Dim Column1 As String
Dim Column2 As String
Set wsG = ThisWorkbook.Sheets("Graph")
Set wsS = ThisWorkbook.Sheets("Sheet1")
wsG.Activate
Column1 = wsG.Cells(1, "P").Value
Column2 = wsG.Cells(2, "P").Value
Set Rg = wsS.Cells.Find(What:=Column1)
If Not Rg Is Nothing Then
StartP3Column = Rg.Column
Else
MsgBox Column1 & " not found in " & wsS.Name
End If
Set Rg = wsS.Cells.Find(What:=Column2)
If Not Rg Is Nothing Then
StartP4Column = Rg.Column
EndP3Column = Rg.Column - 1
Else
MsgBox Column2 & " not found in " & wsS.Name
End If
Set Rg = wsS.Cells.Find(What:="End Column")
If Not Rg Is Nothing Then
StartEndColumn = Rg.Column
EndP4Column = Rg.Column - 1
Else
MsgBox "End Column" & " not found in " & wsS.Name
End If