This works
mySheet.Rows(CStr(activeRow) & ":" & CStr(activeRow + deletedRowAmount)).Delete
This does not work
mySheet.Columns(CStr(columDelete) & ":" & CStr(columDelete + deletedRowAmount)).Delete
What I am missing here?
ERROR is VBA Runtime Error 1004 “Application-defined or Object-defined error”
Columns are A,B,C,... when used in a string as you are using them. If they are numbers you will need to do it slightly different:
With mySheet
.Range(.cells(1,columDelete), .cells(1,columDelete + deletedRowAmount)).EntireColumn.Delete
End With
In general, what Scott Craner says is quite enough, but here is a way to go around it and make it easier:
Sub TestMe()
Columns(GetColumnRange(2, 5)).Delete
End Sub
Public Function GetColumnRange(colStart As Long, colEnd) As String
GetColumnRange = Split(Cells(1, colStart).Address(True, False), "$")(0) & _
":" & Split(Cells(1, colEnd).Address(True, False), "$")(0)
End Function
Try the TestMe function, it should delete the columns from 2(B) to 5(E).
Related
I'm trying to use if statement inside a For loop and it is giving Runtime Error 1004 “Application-defined or Object-defined error”. I am using the below code(I have commented where the error occurs). I think it is because of cell referencing that I've used for the for loop.
Please help.
Sub ifcheck()
Dim i As Integer
For i = 1 To 5
Cells(3, 2).Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(Availability!R3C2>=Interviewers!R[i]C[1],Availability!R3C1>=Interviewers!R[i]C[3]),Interviewers!R4C2,"""")"
'>>>The above line shows as error 1004 - “Application-defined or Object-defined error”
Range("B3").Select
Next i
End Sub
Thanks,
Jay.
Try splicing i into the formula with basic string concatenation.
Dim i As long
For i = 1 To 5
Cells(2+i, 2).FormulaR1C1 = _
"=IF(AND(Availability!R3C2>=Interviewers!R[" & i & "]C[1], Availability!R3C1>=Interviewers!R[" & i & "]C[3]), Interviewers!R4C2, text(,))"
Next i
Since cells(3, 2) is B3, I don't know what you are intending to do for successive loops. I've moved down one row for each loop.
The below code is working fine with me. I need your help and support to make it a function so I can for example write in any cell
=adj() or =adj(A1) and the formula will apply,
Sub adj()
Dim i, j As Integer
Sheet1.Select
With Sheet1
j = Range(ActiveCell.Offset(0, -2), ActiveCell.Offset(0, -2)).Value
For i = 1 To j
ActiveCell.Formula = "=" & Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, -1)) & i & "))" & "&char(10)"
Next i
End With
End Sub
It's hard for me to definitively understand what you're trying to do here. I think you're trying to concatenate x number of cells with a separator field.
So, I would do the following changes... obviously you can change accordingly.
Declare the Inputs as variants. If you don't and get a type mismatch the function wont call in debug. This also gives you the opportunity to deal with the Inputs.
Put in an error handler to prevent unwanted debug w.r.t. a failure.
You can't use Evaluate that way. I think you're trying to get an Evaluation of cells like A1 etc.
The function has to be called from a Cell on a sheet since it uses Application.Caller. You shouldn't really need this for the function to work, but I put in in there in case you are calling via F9 calculation.
You can also put in the line if you want the calculation to occur every time you recalculate. However this should be used with caution since you can get some unwanted calculation side effects with some spreadsheets when using this.
Application.Volatile
Public Function Adj(ByVal x As Variant, ByVal y As Variant) As String
On Error GoTo ErrHandler
Dim sSeparator As String, sCol As String
Dim i As Integer
'Get the column reference.
sCol = Split(Columns(y).Address(False, False), ":")(1)
'Activate the sheet.
Application.Caller.Parent.Select
sSeparator = Chr(10)
For i = 1 To x
Adj = Adj & Evaluate(sCol & i) & sSeparator
Next
'Remove the last seperator...
Adj = Left(Adj, Len(Adj) - 1)
Exit Function
ErrHandler:
'Maybe do something with the error return value message here..
'Although this is a string, Excel will implicitly convert to an error.
Adj = "#VALUE!"
End Function
If you wanted to pass change to a formula, and pass in the range, you would use something like:
Public Function Func(Byval MyRange as range) as variant
In this case, you're not specifying a return value so it will be ignored.
Public Function Func(Byval MyRange as range) as variant
Dim i, j As Integer
With MyRange.parent
j = .Range(MyRange.Offset(0, -2), MyRange.Offset(0, -2)).Value
For i = 1 To j
MyRange.Formula = "=" & .Range(MyRange.Offset(0, -1), MyRange.Offset(0, -1)) & i & "))" & "&char(10)"
Next i
End With
End Sub
It would be something like that..
I'm trying to insert a chart into my spreadsheet, as outlined in msdn here:
https://msdn.microsoft.com/en-us/library/bb238877(v=office.12).aspx
Unfortunately i get the error in the
Method 'SetSource Data' of Object' _chart' failed
on the line starting ActiveChart. Why is it doing this? i've tried both string and range variables here to no avail.
In addition to the fact that i can't get this method to work, i don't like the fact you need to select the select the graph, surely there is a better way?
Function TimeSeries(rngToPrint As Range)
Dim strRange As String
Dim rngChart As Range
lngstartrow = 8
lngendrow = Range("a10000").End(xlUp).Row
Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)).Select
Sheets(rngToPrint.Worksheet.Name).Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlLine
End Function
The PlotBy argument specifies whether to plot the data by rows or columns. So the argument should be set to either xlRows or xlColumns.
The error appears at the following line because there is no PlotBy:=xlLine
Modify it to:
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlRows
Or either:
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlColumns
I am trying to use the Lastcolumn from Sheet D in a formula in Sheet M. This is the code I have: D!R[8]C " & LastColumn1 & .
But it looks like the code is using the last column number in sheet M.
Can you please check what is wrong with this code?
Sub misc()
Dim LastColumn1 As Long
LastColumn1 = ActiveWorkbook.Worksheets("D").Range("a12").End(xlToRight).Column
End Sub
Sub try()
Sheet1.Select
Range("A4").Select
ActiveCell.End(xlToRight).Offset(-2, 1).Value = counter & "Qtr"
Do While ActiveCell.Value <> ""
ActiveCell.End(xlToRight).Offset(0, 1).Formula = "=IFERROR(D!R[8]C " & LastColumn1 & " *(1+INDEX(A!R3C4:R418C27,MATCH(M!R2C,A!R2C4:R2C27,0))),0)"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
You need a function for this.
Function LastColumn() as long
LastColumn = ThisWorkbook.Worksheets("D").Range("a12").End(xlToRight).Column
end function
And then you call that function directly from your code:
Sub try()
Sheet1.Select
Range("A4").Select
lastCol = LastColumn '## Calling the function from above
ActiveCell.End(xlToRight).Offset(-2, 1).Value = counter & "Qtr"
Do While ActiveCell.Value <> ""
ActiveCell.End(xlToRight).Offset(0, 1).Formula = "=IFERROR(D!R[8]C " & lastCol & " *(1+INDEX(A!R3C4:R418C27,MATCH(M!R2C,A!R2C4:R2C27,0))),0)"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Can you please check what is wrong with this code?
You have an undeclared/uninitialized variable called LastColumn1 which you are using in a formula string. This variable has no value at runtime, which causes the error.
How to resolve it:
If you had simply used Option Explicit, you would be informed of a compilation error. LastColumn1 variable is undeclared within the scope of the try() procedure.
Since you are not using Option Explicit (which requires that all variables be declared), the compiler initializes undeclared variables as type Variant, with an empty/0/null-string value.
So, what you have essentially is an obvious error with:
ActiveCell.End(xlToRight).Offset(0, 1).Formula = _
"=IFERROR(D!R[8]C " & EMPTY & " * _
(1+INDEX(A!R3C4:R418C27,MATCH(M!R2C,A!R2C4:R2C27,0))),0)"
Because the result of that concatenation is: D!R[8]C and that is an invalid Range (without a column index), it is undefined/cannot exist as such.
Using #iDevelop's function above will probably suffice for most ordinary, simple cases, however you may want to review the more robust methods described here:
Error in finding last used cell in VBA
I want to use a user input inside a formula as follows:
Sub example()
Dim StockDays As Integer
StockDays = InputBox(Prompt:="How many days?")
Range("AG2").FormulaR1C1 = "=ROUNDUP(RC[-6]*" & StockDays & "/90, 0)"
Range("AG2").Select
Selection.AutoFill Destination:=Range(Cells(2, 33), Cells(1500, 33))
End Sub
When run, the above code throws an error at the ROUNDUP line.
Run-time error 1004.
Application-defined or object-defined error.
I think the problem is related to the variable StockDays.
How can I arrange the code so that I can make it work?
I have commented the code so you shouldn't have any problem understanding it :)
Option Explicit
Sub example()
Dim StockDays As Integer
'~~> Type:=1 will ensure that the user enters only numbers
StockDays = Application.InputBox(Prompt:="How many days?", Type:=1)
'~~> No Need to autofill. You can fill all the range in one go
Thisworkbook.Sheets("Sheet1").Range("AG2:AG1500").FormulaR1C1 = _
"=ROUNDUP(RC[-6] * " & StockDays & "/ 90, 0)"
'OR this as mentioned in your comment
Thisworkbook.Sheets("Sheet1").Range("AG2:AG1500").FormulaR1C1 = _
"=ROUNDUP((RC[-6]* " & StockDays & "/90),0)"
End Sub
I think this should work
Range("AG2").FormulaR1C1 = "=ROUNDUP(RC[-6] * " & StockDays & " / 90, 0)"
the mistake u did was, you where applying a formula but forgot to replace the variable u used with the value in the varaiable