How to select multiple ranges in Excel with vbs
Stefan
One way.
Remember to set a reference to the
Excel object library from the VBA-editor in Word with
Tools References
'Leo Heuser, 19-4-2005
Dim objExcel As Excel.Application
Dim SelRange As Excel.Range
Dim MyBook As Excel.Workbook
Set objExcel = New Excel.Application
Set MyBook = objExcel.Workbooks.Add
objExcel.Visible = True
With MyBook.ActiveSheet
Set SelRange = objExcel.Union(.Range("A1:A10"), .Range("C2:C8"))
End With
SelRange.Value = 1234
Set objExcel = Nothing
End Sub
--
Best Regards
Leo Heuser
Followup to newsgroup only please.
"Stefan Mueller" skrev i en meddelelse
...
The following vbs code opens Excel and selects the ranges A1-A10 and
C2-C8:
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
objExcel.Visible = True
objExcel.ActiveSheet.Range("A1:A10,C2:C8").Select
But how can I do such a selection with variables?
e.g.
SelectionPart1_Column = 1
SelectionPart1_RowFrom = 1
SelectionPart1_RowTo = 10
SelectionPart2_Column = 3
SelectionPart2_RowFrom = 2
SelectionPart2_RowTo = 8
The following command selects only the first part (A1-A10):
objExcel.ActiveSheet.Range(objExcel.Cells(Selectio nPart1_RowFrom,
SelectionPart1_Column), objExcel.Cells(SelectionPart1_RowTo,
SelectionPart1_Column)).Select
But how can I add to the already selected part 1 also part 2 (C2-C8)?
Any help is very appreciated
Stefan
|