Я пытаюсь автоматизировать выбор определенного диапазона c со ссылкой на точки, такие как "метки строк" и другие 2 "Итого" в моей таблице Excel. Однако я сталкиваюсь с "несоответствием типов" в части Set newlocationgrand1 = locationgrand1, и если я удаляю set, он показывает "set object required error". Так что я могу сделать, чтобы решить эту проблему? В качестве альтернативы, есть ли другой способ выбрать диапазон со ссылкой из точек, как показано ниже?
sub test
Dim locationrow as range
Dim locationgrand1 as range
Dim locationgrand2 As Range
Dim newlocationrow as string
Dim newlocationgrand1 As String
Dim locationgrand2no as long
Dim extractedno As Long
Dim lastcolletter As String
Set locationrow = Range("A1:L6").Find(What:="Row Labels", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'row labels address is 'A4'
newlocationrow = locationrow.Offset(1, 1).Address 'newlocation row is B5
Set locationgrand1 = Range("A1:L6").Find(What:="Grand Total", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
Set newlocationgrand1 = locationgrand1.Offset(0, -1).Address 'set object error if i put set, it shows type mismatch error
'newlocationgrand1 is J5
lastcolletter = colLetter(newlocationgrand1.Column) 'lastcolletter = J
Set locationgrand2 = Range("A1:A15").Find(What:="Grand Total", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
locationgrand2no = locationgrand2.Row 'locationgrand2 is 13
extractedno = locationgrand2no - 1 'extractedno =12
With Selection
Range("newlocationrow" & ":" & "lastcolletter" & "extractedno").Select
обновленный код
Я сделал некоторые корректировки в коде, и теперь есть этот "метод выбора диапазона ошибка класса "ошибка в части выбора, хотя мне удалось получить диапазон (B5: J12), который является частью, которую я хочу выбрать, но express в терминах переменных
sub test
Dim locationrow As Range
Dim locationgrand1 As Range
Dim newlocationrow As Range
Dim newlocationgrand1 As Range
Dim locationgrand2 As Range
Dim locationgrand2no As Long
Dim extractedno As Long
Dim lastcolletter As String
Dim locationcolletter As String
Dim x As String
Dim y As String
Dim extractednorow As Long
Dim locationrowno As Long
Set locationrow = Range("A1:L6").Find(What:="Row Labels", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'row labels address is 'A4'
Set newlocationrow = locationrow.Offset(1, 1) 'newlocationrow is 'B5'
locationcolletter = colLetter(newlocationrow.Column) 'locationcolletter = B
locationrowno = locationrow.Row + 1 'locationrow=5
Set locationgrand1 = Range("A1:L6").Find(What:="Grand Total", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
Set newlocationgrand1 = locationgrand1.Offset(0, -1)
x = newlocationgrand1.Address 'set object error if i put set, it shows type mismatch error
'newlocationgrand1 is J5
lastcolletter = colLetter(newlocationgrand1.Column) 'lastcolletter = J
Set locationgrand2 = Range("A1:A15").Find(What:="Grand Total", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
locationgrand2no = locationgrand2.Row 'locationgrand2 is 13
extractedno = locationgrand2no - 1 'extractedno =12
With Selection
Sheets(1).Range(locationcolletter & locationrowno & ":" & lastcolletter & extractedno).Select
'^select method range of class fail
Function colLetter(col As Long) As String
colLetter = Split(Columns(col).Address(, 0), ":")(0)
End Function