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.
Related
I want to trigger a macro when I change multiple cells with CTRL+ENTER for each cell separately.
The code sometimes skips rows. What could be causing this some cells scattered within the range?
ChangeCell = Target.Address(0, 0)
' In case of multiple changed cells, RowChangeCell provides the 1st row of the changed range
RowChangeCell = Range(ChangeCell).Row
Range(ChangeCell).Select
With Selection
ChangeStart = .Cells(1, 1).Address
ChangeEnd = .Cells(.Rows.Count, .Columns.Count).Address
ChangeStartRow = Range(ChangeStart).Row
ChangeEndRow = Range(ChangeEnd).Row
End With
Application.ScreenUpdating = False
'If Not Intersection Is Nothing Then
'ChangeCell = Target.Address(0, 0)
'ActiveRowMain = Range(ChangeCell).Row
For ActiveRowMain = ChangeStartRow To ChangeEndRow Step 1
'If ActiveRowMain = 51055 Then
'
' Chck = Worksheets("Main_Report").Range("A" & ActiveRowMain).Value
' MsgBox "Status = " & Chck
'
'End If
'Chck = Worksheets("Main_Report").Range("A" & ActiveRowMain).Value
'MsgBox "MainLine = " & ActiveRowMain & " Status = " & Chck & " ChangeStartRow = " & ChangeStartRow & " ChangeEndRow = " & ChangeEndRow
Call Events_Update_Main_Caller(ActiveRowMain)
Next ActiveRowMain
'MsgBox "Updates are completed"
'End If
Application.ScreenUpdating = True
End Sub
I keep getting the compile error resposne Expected Sub, Function or Property, when trying to run below VBA code. The problem is with the called sub procedure. Any insight as to why will be appreciated.
Result5iii = Result6i - Result4
ResultGi = Result5iii - Range("G2").Value
Range("W3").Value = ("ResultGi")
Range("X3").Value = (" Can Use: [ " & ResultGi & " ]")
Range("Y3").Value = ("2 Nxt will not be " & ResultGi)
Range("Z3").Value = (" 19|15|5ii ")
'Minus1Rule_OriginalsG'
If Result5iii = 5 And Range("G2").Value = 1 And ResultGi = 4. Then
Call Minus1Rule_5OriginalsG
The called Procedure is;
Public Sub Minus1Rule_5OriginalsG()
Dim ResultG As Double, Result5i As Double, Result1 As Double, Result1a As Double
'1. -1 Rule'
'For Originals'
Range("W2").Value = (" 0, 5 [ " & ResultG & " ]")
Range("X2").Value = (" Can Use: 0, 5 [ " & ResultG & " ]")
Range("Y2").Value = ("2 Nxt will not be " & "0, 5" & "Reason: -1 Rule For Originals")
Range("Z2").Value = (Result1 & "|" & Result1a & " 5 - 1" & "|" & "5i ")
End Sub
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.
I'm trying to figure out if there is a quicker way in my program to save to pdf. When I run the following code, it takes about 25 seconds for it to save each pdf file I have. I have narrowed the bottleneck to the save to pdf code by stepping through the code. I tried turning the automatic calculation to manual but that didn't help.
Basically, what this script is doing is checking how many loan borrowers there are (customer packages may be less than but will never be more than 4), then populating some data and then printing a some worksheets.
Here is my code:
`customerpackages = 0
For counter = 1 To 19
If Sheets("Loan Data").Cells(2, counter) <> "" Then
customerpackages = customerpackages + 1
counter = counter + 6
End If
Next counter
For j = 1 To (customerpackages * 2)
Sheets("Certification").Range("G43").Value = ssn_array(j - 1)
Sheets("Certification").Range("G46").Value = ssn_array(j)
Sheets("Anti-steering").Cells(3, 1).Value = borrower_array(j - 1)
Sheets("Anti-steering").Cells(4, 1).Value = borrower_array(j)
If borrower_array(j) = " " Then
Sheets("Rate Lock").Cells(10, 1).Value = "NAME : " & borrower_array(j - 1)
Else
Sheets("Rate Lock").Cells(10, 1).Value = "NAME : " & borrower_array(j - 1) & " and" & borrower_array(j)
End If
Worksheets(common_disclosures).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\users\" & environ_user & "\desktop\" & borrower_array(j - 1) & "
disclosures.pdf" _
, Quality:=xlQualityMinimum, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
j = j + 1
Next j`
Thanks in advance for your help.
Background
As I am trying to run a set of technical indicators on a large number of csv data files, I patched together a 3 macros which was intended to
Macro 1: RunAnalysis > Open every individual excel file in specific
folder
Macro 2: DoWork > Run Macro Macro 3: IndicatorData > To
populate the individual csv files opened by Macro 1 with technical
indicator formulas.
The codes are found below.
Problem/Issue
The problem lies with Macro 3 [Indicator Data]
The value of variable EMAWindow1 which captures a value from the sheet called ControlPanel found in the same Workbook is used selectively by vba when populating column H.
In the first two lines of formulas
ActiveSheet.Range("h1") = EMAWindow1 & " Day EMA"
ActiveSheet.Range("h" & EMAWindow1 + 1) = "=average(R[-" & EMAWindow1 - 1 & "]C[-3]:RC[-3])"
You will find the macro working perfecting. However
When you reach the third line
ActiveSheet.Range("h" & EMAWindow1 + 2 & ":h" & LastRowCheck).FormulaR1C1 = "=R[0]C[-3]*(2/(EMAWindow1 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow1 + 1)))"
For some reason, the value of EMAWindow1 is not used and is treated as a text causing error and resulting in the #NAME? as the formula is unable to calculate with a text inside it.
The subsequent codes encounter the same problem. Anyone faced the same problem before? I have been at this problem for awhile, and I hope for some enlightenment.
Codes
Sub RunAnalysis()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "C:\Users\gabri_000\Desktop\Trading Data\Stocks - 1 - 100\"
Filename = Dir(Pathname & "*.csv")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
DoWork wb
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
Sub DoWork(wb As Workbook)
With wb
Call IndicatorData
End With
End Sub
Sub IndicatorData()
Dim LastRowCheck As Long
Dim EMAWindow1, EMAWindow2, EMAWindow3 As Long
Dim Pathname, Filename, FileNameIndex, FinalFileName As String
'Converting file path into a string
Pathname = "C:\Users\gabri_000\Desktop\Trading Data\Stocks - 1 - 100\"
'Full file name
Filename = Dir(Pathname)
'Removing the .csv portion of the file name
FileNameIndex = InStr(1, Filename, ".csv")
FinalFileName = Left(Filename, FileNameIndex - 1)
'Selecting Recently Opened File
Sheets(FinalFileName).Select
'Counting Number of Rows in the worksheet
With Worksheets(FinalFileName)
LastRowCheck = .Range("A" & .Rows.Count).End(xlUp).Row
End With
'EMA Inputs
'Obtaining data from masterfile
With ThisWorkbook.Sheets("ControlPanel")
EMAWindow1 = .Range("EMAWindow1")
EMAWindow2 = .Range("EMAWindow2")
EMAWindow3 = .Range("EMAWindow3")
End With
'EMA Calcuations
ActiveSheet.Range("h1") = EMAWindow1 & " Day EMA"
ActiveSheet.Range("h" & EMAWindow1 + 1) = "=average(R[-" & EMAWindow1 - 1 & "]C[-3]:RC[-3])"
ActiveSheet.Range("h" & EMAWindow1 + 2 & ":h" & LastRowCheck).FormulaR1C1 = "=R[0]C[-3]*(2/(EMAWindow1 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow1 + 1)))"
ActiveSheet.Range("i1") = EMAWindow2 & " Day EMA"
ActiveSheet.Range("i" & EMAWindow2 + 1) = "=average(R[-" & EMAWindow2 - 1 & "]C[-4]:RC[-4])"
ActiveSheet.Range("i" & EMAWindow2 + 2 & ":i" & LastRowCheck).FormulaR1C1 = "=R[0]C[-4]*(2/(EMAWindow2 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow2 + 1)))"
ActiveSheet.Range("j1") = EMAWindow3 & " Day EMA"
ActiveSheet.Range("j" & EMAWindow3 + 1) = "=average(R[-" & EMAWindow3 - 1 & "]C[-5]:RC[-5])"
ActiveSheet.Range("j" & EMAWindow3 + 2 & ":i" & LastRowCheck).FormulaR1C1 = "=R[0]C[-5]*(2/(EMAWindow3 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow3 + 1)))"
End Sub