Why doesn't Intellisense appear after typing Worksheets("Sheet1").? - vba

I understand why it wouldn't pop up for ActiveSheet since it can return a worksheet or chart sheet so it wouldn't know which set of properties/methods to list, but if I'm explicitly referencing a worksheet within the Worksheets collection, why doesn't Intellisense get triggered?
I also want to extend this question to predefined constants. Sometimes a list of the predefined constants pops up for a property but sometimes it doesn't. Why is that? For example:
Application.Calculation=
will trigger a list of the predefined constants.
Worksheets("Sheet1").PageSetup.Orientation=
will NOT trigger a list of predefined constants.
Thanks for your answers, folks! :)

The Intellisense needs to know what is returned to give you the proper selection.
Have a look at the Object-Browser:
Application.Calulation returns XlCalculation. You get the selection by Intellisense
Worksheets("Sheet1") returns an unspecified Object, because it can be a Worksheet, but doesn't has to. That's why the Intellisense breaks at this level.

the Intellisense works if Excel know exactly what you want.
If you do this in your Sub or Function it works.
Dim sh As Excel.Worksheet
Set sh = ThisWorkbook.Worksheets("DUMMY")
sh.PageSetup.Orientation = here comes the Intellisense
Set sh = Nothing

If you declare each object as a variable and clarify the kind of object to the program, Intellisense will work normally.
See examples below.
Intellisense does not work for Charts (1), Worksheets ("Sheet1"), Cells (1, 1).
But Ws, rng, myChart work.
Sub test()
Dim Ws As Worksheet
Dim rng As Range
Dim myChart As Chart
Set myChart = Charts(1)
Set Ws = Worksheets("Sheet1")
Set rng = Cells(1, 1)
End Sub

Related

VBA RBG - "Wrong number of arguments or invalid property argument"

I know there are other questions which deal with the "Wrong number of arguments or invalid property assignment", but none which deal with this error being generated on a basic line assigning an RGB colour to a cell interior.
This error is generating on line 6 of the below. I don't understand how this is possible, as RGB clearly has three arguments.
The only thing I can think of is that, earlier today, I wrote a macro in a different (now closed spreadsheet), where I used the initials "rgb" as a variant name. I know that, in R, it's possible to redefine a base function accidentally by replacing it with a UDF, and I'm wondering whether perhaps I've now unwittingly changed "rgb" to be something other than the function in the VBA base code. But that doesn't sound plausible - I've never heard of that happening in VBA, and it's more of a "for dummies" type language which I wouldn't have thought would give users that optionality.
EDIT Yes - this issue is resolved by changing the name of the macro, which is a stupid error. However, I would point out that this error is also happening in a different subroutine, which I have not named in such a stupid fashion. I will need to investigate why it's happening in that sub too.
Option Explicit
Sub RGB()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = Sheets("Sheet1")
ws.Range("G20").Interior.Color = RGB(255, 51, 204)
End Sub
In case the comments haven't explained it fully.. you state in your question:
earlier today, I wrote a macro in a different (now closed
spreadsheet), where I used the initials "rgb" as a variant name.
However, in the example you provide, you've actually called the Subroutine you're calling RGB as well. This is the cause of the issue. Renaming it would fix it but while I'm here I can share with you some other points:
This would work:
Option Explicit
Sub Set_G20_RGB()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = Sheets("Sheet1") ' should really be Set ws = wb.Sheets("Sheet1")
ws.Range("G20").Interior.Color = RGB(255, 51, 204)
End Sub
However, to make it a little more useful, you might want to use something like:
Option Explicit
Sub Set_RGB(clr_rng As Range, red_value As Integer, grn_value As Integer, blu_value As Integer)
clr_rng.Interior.Color = RGB(red_value, grn_value, blu_value)
End Sub
Which could be called like so:
Set_RGB ThisWorkbook.Sheets("Sheet1").Range("G20"), 255, 51, 204
or..
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
Set_RGB ws.Range("G20"), 255, 51, 204

VBA Word: Change Data of charts

I want to change the data of a chart in a Word Document, but I can't find the right way to address my charts. I tried several techniques, but nothing worked. (I´d love to open a ExcelSheet in which I can just change the Data)
So to put it all together: I want to change the data (not the source), of a MS Word chart, which looks like that:
Edit(13.8.):
After request, I try to give you some "reference Code" to work with.
Sub ChangeChart()
Dim aktDocument As Document
Dim chrt As Chart
Dim SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set SourceSheet = aktDocument.Shapes(1).Chart.OpenSourceData 'I know it´s not that easy
SourceSheet.Range("B5") = newStuff
aktDocument.Shapes(1).Chart.SetSourceData = SourceSheet
End Sub
I know this may sounds utopic and ridiculous, but I just don´t know, how to address the chart in the right way, or to even work with it properly.
Edit(15.08):
Even after recreating the old charts, the following code is not able to find a shape which has a chart. And therefore it stops when the index is out of range.
Sub Test()
i = 0
Do While i < 100
i = i + 1
If ActiveDocument.Shapes(i).HasChart Then
MsgBox "found one!"
End If
Loop
End Sub
Solution(30.08.):
The answer from #Cindy Meister was the solution to my problem. After further working with it, I came to the problem, that the ChartData always opens on the screen, while running the code.
Just for reference this question was my workaround.
All Office applications use the Excel engine to create and manage charts. In Word, charts can be formatted in-line with the text or with text wrap formatting. In the former case, a chart object needs to be addressed via the InlineShapes collection, in the latter via the Shapes collection.
Since your sample code uses Shapes(1) I've used that in the code snippet below. If it's not certain that the first Shape in the document is the chart, but you've assigned the Shape a name, you can use that as the index value (for example Shapes("MyChart"). Or you can loop the Shapes collection and check HasChart.
HasChart returns True if the Shape (or InlineShape) is a Chart. It's then possible to set Shape.Chart to an object variable. The chart's data can be accessed using Chart.ChartData.Activate - if you don't use Activate it's not possible to access the data when the chart's worksheet is stored in the Word document. Only then can Chart.ChartData.Workbook return a workbook object, and through that the worksheet can be accessed using ActiveSheet. From that point on, it's like working with the Excel object model.
Sub ChangeChart()
Dim aktDocument As Document
Dim shp As Word.Shape
Dim chrt As Word.Chart
Dim wb As Excel.Workbook, SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set shp = aktDocument.Shapes(1)
If shp.HasChart Then
Set chrt = shp.Chart
chrt.ChartData.Activate
Set wb = chrt.ChartData.Workbook
Set SourceSheet = wb.ActiveSheet
SourceSheet.Range("B5").Value2 = newData
End If
End Sub

Why need to specify activesheet for chartobjects.add

Sub smarterway()
Dim chartfirst As ChartObject
Dim rngChart As Range
Set rngChart = Range("D3:J20")
Set chartfirst = ActiveSheet.ChartObjects.Add(Left:=rngChart.Left, Top:=rngChart.Top, Width:=rngChart.Width, Height:=rngChart.Height)
[....]
For this line:
Set chartfirst = ActiveSheet.ChartObjects.Add(..)
if I dont add in Activesheet in. I get a Object Required error.
I don't understand why though. Isnt it assumed it is Activesheet, cause I usually don't put Activesheet in my codes and it works as long as I have the required sheet opened and activated.
I'm clearly missing and misunderstanding something very fundamental in vba, please advice

Store COPY of a worksheet in worksheet variable

What I want to achieve:
I want to assign copy of a worksheet to variable, for later use.
What I tried and results
First : The code below works fine. Something like this I would like to achieve, but using worksheet.copy.
Sub DuplicateSheetRenameFirst()
Dim wsDuplicate As Worksheet
Set wsDuplicate = Worksheets.Add
wsDuplicate.Name = "Duplicate"
End Sub
Second : Using the copy method, creates a worksheet in current workbook, but generates a Runtime error 424 - Object required.
Sub DuplicateSheetRenameSecond()
Dim wsDuplicate As Worksheet
Set wsDuplicate = Worksheets("Sheet1").Copy(after:=Worksheets(Worksheets.Count))
'above line : runtime error 424 object required, but the sheet is created
wsDuplicate.Name = "Duplicate"
End Sub
Third : Creates a worksheet in new workbook (so creates book, then sheet), but still generates the same Runtime error 424 - Object required.
Sub DuplicateSheetRenameThird()
Dim wsDuplicate As Worksheet
Set wsDuplicate = Worksheets("Sheet1").Copy
'above line : runtime error 424 object required, but the sheet is created in new workbook
wsDuplicate.Name = "Duplicate"
End Sub
Workaround : I can modify any of the second or third way to at first copy the sheet and then set the variable to activesheet, but I was wandering if there is a one step way of doing this. I'm not sure if this would work all the time, since the activesheet may not be the one just copied, maybe.
The Question:
Is there a simple (one step) way to store the copy of a worksheet in a variable? Preferably without errors or without filtering the error with error handler.
This is maybe ok?
Sub copySheet()
Dim ws As Excel.Worksheet
Excel.ThisWorkbook.Sheets("Sheet1").Copy After:=Sheets(1)
Set ws = Excel.ThisWorkbook.ActiveSheet
End Sub
It is unfortunate that in this case you need to use an Active... object. Generally it is good practice to avoid Active... objects.
You cannot do this though as the method .copy is not returning an object of the worksheet class:
Sub copySheet()
Dim ws As Excel.Worksheet
Set ws = Excel.ThisWorkbook.Sheets("Sheet1").Copy(After:=Sheets(1))
End Sub
Some further explanation is in this previous post:
Why does Worksheet.Copy not return a reference to the new workbook created
In MSDN it is not altogether obvious that the method returns nothing:
https://msdn.microsoft.com/EN-US/library/office/ff837784.aspx
...but in your friend Excel's Object Explorer it is more obvious. If it returned a worksheet object then by the arrow would read:
Sub Copy([Before], [After]) as Worksheet

How to add a new spreadsheet with VBA-Code, using VBA

I am creating a macro and part of the macros function is to make VBA create a new spreadsheet. Because of the nature of distribution the name will change. I need to add code to this spreadsheet. Is there anyway I can do this?
Jook has already explained how it works. I will take it a step further.
The syntax of adding a worksheet is
expression.Add(Before, After, Count, Type)
If you check inbuilt Excel's help then you can see what Before, After, Count, Type stands for
FROM EXCEL"S HELP
Parameters (All 4 parameters are Optional)
Before - An object that specifies the sheet before which the new sheet is added.
After - An object that specifies the sheet after which the new sheet is added.
Count - The number of sheets to be added. The default value is one.
Type - Specifies the sheet type. Can be one of the following XlSheetType constants: xlWorksheet, xlChart, xlExcel4MacroSheet, or xlExcel4IntlMacroSheet. If you are inserting a sheet based on an existing template, specify the path to the template. The default value is xlWorksheet.
Once the sheet is created then you need to use .insertlines to create the relevant procedure and to also embed the code that you want to run.
NOTE - IMP: If you want the code to embed code in the VBA project, you need to ensure that you have "Trust Access to the VBA Project Object Model" selected. See snapshot.
Here is an example where I am creating a sheet and then embedding a Worksheet_SelectionChange Code which will display a message "Hello World"
CODE - TRIED AND TESTED
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim nLines As Long
Dim VBP As Object, VBC As Object, CM As Object
Dim strProcName As String
Set ws = Worksheets.Add
Set VBP = ThisWorkbook.VBProject
Set VBC = VBP.VBComponents(ws.Name)
Set CM = VBC.CodeModule
strProcName = "Worksheet_SelectionChange"
With ThisWorkbook.VBProject.VBComponents( _
ThisWorkbook.Worksheets(ws.Name).CodeName).CodeModule
.InsertLines Line:=.CreateEventProc("SelectionChange", "Worksheet") + 1, _
String:=vbCrLf & _
" Msgbox ""Hello World!"""
End With
End Sub
This is how the new sheet code area looks once you run the above code.
the following code will add you a spreadsheet.
Public Sub Workbook_Add()
Dim wks As Worksheet
Set wks = ThisWorkbook.Worksheets.Add(, , 1, xlWorksheet)
With wks
'set codename of wks
ThisWorkbook.VBProject.VBComponents(.CodeName).Name = "tblWhatever"
'set tablename of wks
.Name = "whatever"
'add code (untested demo)
'ThisWorkbook.VBProject.VBComponents(.CodeName).CodeModule.InsertLines 1, "Option Explicit"
'add code (as of example from excel-help)
'Application.VBE.CodePanes(1).CodeModule.InsertLines 1, "Option Explicit"
End With
End Sub
If you need to add VBA-Code to this specific spreadsheet, you should further inspect the VBProject object - look for CodeModule and then i.e. InsertLines.
A further hint for you - I would try to use the CodeNames of your tables. It is less likely to be changed - BUT it might be not that comfortable to use in your code at first. I had to get used to it, but for me it has many advantages against using a tables name.
Hope this helps ;)
The default .Add method adds a sheet at the start of the list. Often you want to add it at the end before adding the code lines, as explained by Siddarth Rout. To do that anywhere you can use:
ActiveWorkbook.Worksheets.ADD After:=ActiveWorkbook.Sheets(ActiveWorkbook.Worksheets.Count)
It is easier to read if you have defined and set WB:
Dim WB as Excel.workbook
Set WB = ActiveWorkbook
WB.Sheets.ADD After:=WB.Sheets(WB.Sheets.Count)
Set VBC = ActiveSheet 'If using in Siddarth Rout's code above
Sheets and Worksheets are interchangeable, as illustrated.