I'm trying to delete select ranges and shift the rest of the cells up but keep getting stuck on the following error
Run-time error '1004':
Unable to debug "Delete method of Range class failed"
I can't see what exactly I'm doing wrong, would appreciate any pointers.
Mainfile = "Travelex Order.xls"
SecFile = "EURUSD.xls"
SheetOne = "Bulk Order"
SheetTwo = "EURUSD Breakdown"
SheetThree = "PDF Gen2"
SheetFour = "EURUSD"
SheetFive = "Rate Check"
Workbooks(Mainfile).Worksheets(SheetOne).Range("A23:C96").Copy
Workbooks(Mainfile).Worksheets(SheetFive).Range("A2").PasteSpecial Paste:=xlPasteValues
Count = 2
Do
If Workbooks(Mainfile).Worksheets(SheetFive).Range("A" & Count).Value <> "" And Workbooks(Mainfile).Worksheets(SheetFive).Range("B" & Count).Value = "" And Workbooks(Mainfile).Worksheets(SheetFive).Range("C" & Count).Value = "" Then
Workbooks(Mainfile).Worksheets(SheetFive).Range("A" & Count & ":C" & Count).Delete Shift:=xlToUp
End If
Count = Count + 1
Loop Until Count > 80
I tried doing the following change but have had no luck
Workbooks(Mainfile).Worksheets(SheetFive).Range("A" & Count & ":C" & Count).Delete Shift:=xlToUp
to
Range("A" & Count & ":C" & Count).Delete Shift:=xlToUp
I always keep getting an error on that line.
I've messed up the Syntax as commented by Rory, it should be xlUp.
Related
I have an Excel sheet in which I want to add an Conditional Formula by VBA. While I'm trying to do so, Excel is throwing "Invalid procedure call or argument" and I can't find why.
The problem is with exactly this line:
Set cf = shG.Range("E" & (i - 3) & ":AI" & (i - 2)).FormatConditions.Add(type:=xlExpression, Formula1:="=OR(ISNUMBER(SEARCH(""holiday"",E$" & i & ")),ISNUMBER(SEARCH(""l4"",E$" & i & ")))")
What it should do?
It should change font color to white for 1st and 2nd rows in case if 4th row will contain "holiday" or "l4"
Full Sub:
Sub AddCondForm(shG As Worksheet)
Dim r As Range
Dim cf
Set r = shRoles.Cells(2, 1)
Do
For i = 8 To 204 Step 4
If Not IsEmpty(shRoles.Cells(r.row, 2)) Then
Set cf = shG.Range("E" & i & ":AI" & i).FormatConditions.Add(type:=xlTextString, String:=r.Value, _
TextOperator:=xlBeginsWith)
Else
Set cf = shG.Range("E" & i & ":AI" & i).FormatConditions.Add(type:=xlCellValue, Operator:=xlEqual, _
Formula1:=r.Value)
End If
cf.Interior.Color = r.Next.Next.Next.Interior.Color
cf.Font.Color = r.Next.Next.Next.Font.Color
cf.SetFirstPriority
'HERE IS PROBLEM
Set cf = shG.Range("E" & (i - 3) & ":AI" & (i - 2)).FormatConditions.Add(type:=xlExpression, Formula1:="=OR(ISNUMBER(SEARCH(""holiday"",E$" & i & ")),ISNUMBER(SEARCH(""l4"",E$" & i & ")))")
'END OF PROBLEM
cf.Font.ColorIndex = 2
'Coloring C Column
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1")
cf.Interior.ThemeColor = xlThemeColorAccent6
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=3/4")
cf.Interior.ThemeColor = xlThemeColorAccent6
cf.Interior.TintAndShade = 0.2
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1/2")
cf.Interior.ThemeColor = xlThemeColorAccent6
cf.Interior.TintAndShade = 0.4
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1/4")
cf.Interior.ThemeColor = xlThemeColorAccent6
cf.Interior.TintAndShade = 0.6
Next
Set r = shRoles.Cells(r.row + 1, 1)
Loop Until IsEmpty(r.Value)
End Sub
PS. I checked the formula itself and it seems working properly. Other lines are also working well. Only this one and I don't know why :'(
If you're not English user, this may be important for you.
In case of FormatCondition.Add you need to add formula exactly the same as if you were entering it manually. So if separator in your language is not a comma, like in Poland (we have semicolon) you would have to use that separator and translate your formulas to that language.
In my case it means I had to change formula to
=LUB(CZY.LICZBA(SZUKAJ.TEKST(""holiday"";E$" & i & "));CZY.LICZBA(SZUKAJ.TEKST(""l4"";E$" & i & ")))"
I'm trying to put the data source from one sheet to the Chart in the other sheet. I'm able to achieve the data collection, but at the end, when we set the Source Data, it is throwing Run time error 1004 - Method 'Range' of object'_Global' failed.
Following is the code I'm using
Sub UBCharts()
Set Wb = ThisWorkbook
Set WsCharts = Wb.Sheets("Trend Charts")
Set UBMainChart = WsCharts.ChartObjects("UBMainChart")
Set UBMonthlyYTDSht = Wb.Worksheets("UM - Monthly & YTD Trend")
YearValue = WsCharts.Range("A1").Value
'LookupDate = CDate("" & "01/01/" & YearValue & "")
Xrows = UBMonthlyYTDSht.Range("A" & Rows.Count).End(xlUp).Row
MatchStartRow = Application.WorksheetFunction.Match(CLng(CDate(DateSerial(YearValue, 1, 1))), UBMonthlyYTDSht.Columns("A:A"), 0)
MatchEndRow = Application.WorksheetFunction.Match(CLng(CDate(DateSerial(YearValue, Month(CLng((DateAdd("m", -1, Date)))), 1))), UBMonthlyYTDSht.Columns("A:A"), 0)
UBMainChart.Activate
'ActiveChart.SetSourceData Source:=Range("'UM - Monthly & YTD Trend'!$A$1:$L$1,'UM - Monthly & YTD Trend'!$A$14:$L$23")
UBMainChart.Chart.ChartArea.ClearContents
On Error Resume Next
UBMonthlyYTDSht.Activate
Set ChartRange = UBMonthlyYTDSht.Range(Cells(MatchStartRow, 1), Cells(MatchEndRow, Lcols))
Set ChartRngTitles = UBMonthlyYTDSht.Range(Cells(1, 1), Cells(1, Lcols))
On Error GoTo 0
UBMainChart.Activate
RngStr = """'" & UBMonthlyYTDSht.Name & "'!" & ChartRngTitles.Address & "," & "'" & UBMonthlyYTDSht.Name & "'!" & ChartRange.Address & """"
ActiveChart.SetSourceData Source:=Range(RngStr), PlotBy:=xlColumns 'I'm getting error here
'"'UM - Monthly & YTD Trend'!$A$1:$L$1,'UM - Monthly & YTD Trend'!$A$14:$L$23"
End Sub
Appreciate your help!
It might be easier to just use Union here:
UBMainChart.SetSourceData Source:=Union(ChartRangeTitles, ChartRange), PlotBy:=xlColumns
I want to add two variable into my vba's range function.
For exemple I would like to set a range like this but I don't know if it is possible :
Range("BY" & FirstVariable &":BY" & SecondVariable)
In order to use WorksheetFunction. Median Method :
Application.Median(Range("BY" & FirstVariable &":BY" & SecondVariable))
I tried this :
Range("BY" & ActiveCell.Row - CptM & ":BY" & ActiveCell.Row)
Where CptM is a Number
But I got the error : "Execution code 91: Object Variable Or Bloc Variable With not defined.
What i want to do :
I am using a For Loop to Browse J Column and to check Value Of J Cells
For Cpt = 4 To Cells(Rows.Count, 10).End(xlUp).Row
MyString= Cells(Cpt - 1, 10).Value
If Cells(Cpt, 10).Value = MyString Then
CptM = CptM + 1
End If
If Cells(Cpt, 10).Value <> Metier Then
MyVar= Application.Median(Range("BY" & ActiveCell.Row - CptM & ":BY" & ActiveCell.Row))
Cells(Cpt, 81).Value = MyVar
CptM = 0
End If
Next Cpt
There i except to get the Median of the range R1 , R2 ect ...
Thanks for your time and consideration.
This will work:
Application.Median(Range("BY" & ActiveCell.Row - CptM & ":BY" & ActiveCell.Row))
You can put this as a value in any variable.
The problem was due to ActiveCell.Row because i didn't have Active Cells.
To handle this i just used Cpt in stead of ActiveCell.Row
The full line code is :
Application.Median(Range("BY" & Cpt - CptM & ":BY" & Cpt))
I am important data using a soap call, my problem is that under certain circumstances I may end up with duplicate data.
I can easily remove this data using
WS.Range("A6:O200").RemoveDuplicates Columns:=(2)
However Id like to alert the user when this happens via a MsgBox. Currently I am trying to get this working with some code adapted from another post on here.
Dim dict As Object
' Let Col be the column which warnDupes operates on.
Dim Col As String
Col = "B"
Set dict = CreateObject("scripting.dictionary")
dupeRow = Range(Col & Rows.Count).End(xlUp).Row
On Error Resume Next
For i = dupeRow To 1 Step -1
If dict.Exists(UCase$(Range(Col & i).Value)) = True Then
'range("Y" & i).EntireRow.Delete
WS.Range("A6:O200").RemoveDuplicates Columns:=(2)
'MsgBox ("Hmm...Seems to be a duplicate of " & Range(Col & i).Value & _
" in Cell " & Col & i)
End If
dict.Add UCase$(Range(Col & i).Value), 1
Next
MsgBox ("Duplicate unfullfilled requests where removed")
The problem is of course that this either displays the message for every duplicate value deleted in the loop or even if there are no duplicates (as it does now). Ideally what I want is the remove duplicates to run completely and then alert the user via a message.
Regards
Sam
Dim dict As Object
' Let Col be the column which warnDupes operates on.
Dim Col As String
Dim bCount as Boolean
Col = "B"
Set dict = CreateObject("scripting.dictionary")
dupeRow = Range(Col & Rows.Count).End(xlUp).Row
On Error Resume Next
For i = dupeRow To 1 Step -1
If dict.Exists(UCase$(Range(Col & i).Value)) = True Then
'range("Y" & i).EntireRow.Delete
WS.Range("A6:O200").RemoveDuplicates Columns:=(2)
bCount = True
'MsgBox ("Hmm...Seems to be a duplicate of " & Range(Col & i).Value & _
" in Cell " & Col & i)
End If
dict.Add UCase$(Range(Col & i).Value), 1
Next
If bCount Then
MsgBox ("Duplicate unfullfilled requests where removed")
End If
After uploading the data in Excel file and running the macro code in it to publish the report, getting the
Runtime error 1004 - Method "Select of Object"_Worksheet" Failed
MailBCC = ""
MailSubject = "Stack Ranking Result : " & Sheet3.Cells(EmployeeDetailRowNo, 2) & "(" & Sheet3.Cells(EmployeeDetailRowNo, 1) & ")"
Sheet4.Select
Sheet4.Range("A1").Select
Sheet4.Range("C4").Value = Sheet2.Cells(StackRowNo, 1)
Call SendingMail(MailTo, MailCC, MailBCC, Sheet4.Range("A1:I33"), MailSubject)
StackRowNo = StackRowNo + 1
Loop
Sheet4.Select
Sheet4.Range("A1").Select
End Sub
Take out all those Selects and it should work:
MailBCC = ""
MailSubject = "Stack Ranking Result : " & Sheet3.Cells(EmployeeDetailRowNo, 2) & _
"(" & Sheet3.Cells(EmployeeDetailRowNo, 1) & ")"
Sheet4.Range("C4").Value = Sheet2.Cells(StackRowNo, 1)
Call SendingMail(MailTo, MailCC, MailBCC, Sheet4.Range("A1:I33"), MailSubject)
StackRowNo = StackRowNo + 1
Loop
End Sub
I'll leave this here. You should definitely read it before doing further coding.