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
Related
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
I have 2 workbooks open, and I am trying to copy one range of cells from one workbook into the other workbook based on a condition. The program keeps on breaking at the first For Each loop with the
Subscript out of range
error and I am lost as to why.
I looked at other threads here, and they said that the error comes from not having an Open workbook. I implemented that, and it still gives me this error.
I am new to VBA. Any ideas?
Sub TransferCells()
Dim aggrange As Range
Dim AnalyticalCell As Range
Dim BatchCell As Range
Dim analyticalwb, batchwb As Excel.Workbook
Dim SEHPLC, CultureDay As Worksheet
Set analyticalwb = Workbooks.Open("\\ntucsmafps06.na.jn.com\Hom$\APachall\Ta Big Data\Cas tical Results (4).xlsm")
Set batchwb = Workbooks.Open("\\nctusmafp0s6.na.jn.com\Hom$\APachall\Ta Big Data\20180420_Fed Batch All Data_0.xlsx")
For Each AnalyticalCell In analyticalwb.Worksheets("SE-HPLC").Range("A1:A87")
For Each BatchCell In batchwb.Worksheets("Sheet3").Range("A2:A125271")
If AnalyticalCell.Value = BatchCell.Value Then
Set aggrange = Range(ActiveCell.Offset(0, 11), ActiveCell.Offset(0, 13))
aggrange.Copy (Destination = Application.Workbooks("20180420_Fed Batch All Data_0.xlsx").Worksheets("Sheet3").Range(ActiveCell.Offset(0, 3), ActiveCell.Offset(0, 5)))
End If
Next BatchCell
Next AnalyticalCell
End Sub
Change the problematic code to the following. There are 2 errors there:
With Worksheets(ActiveCell.Parent.Name)
aggrange.Copy Destination:=Application.Workbooks("20180420_Fed Batch All Data_0.xlsx").Worksheets("Sheet3").Range(.Cells(ActiveCell.Offset(0, 3)), .Cells(ActiveCell.Offset(0, 5)))
End With
Destination is a named parameter, thus it should be passed with := and not with =;
To pass a range, based on two cells, you need to pass:
Range(.Cells(ActiveCell.Offset(0, 3)), .Cells(ActiveCell.Offset(0, 5))) and not Range(). Range() takes string as arguments.
Further ideas - the Dim should be done per variable. In other languages (C++, etc) it is ok, in vba it is a bit problematic:
Dim analyticalwb As Excel.Workbook, batchwb As Excel.Workbook
Dim SEHPLC As Worksheet, CultureDay As Worksheet
How to avoid using Select in Excel VBA
Write Option Explicit on the top of the Module and see whether it compiles.
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
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
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.