This is my first time writing anything in VBA- I'm trying to loop through a column (RRlist) and if the value is more than 20, add info about that cell from its row into another cell (rcomments). I want all of the info to add into that cell so it's a list.
When I try to run this code, nothing happens in my spreadsheet.
Option Explicit
Public rcomments As String
Public RRlist As Range
Public RRcell As Range
Sub WrittenMacro()
rcomments = ""
Set RRlist = Range("G33:G420")
For Each RRcell In RRlist
If RRcell.Value > 20 Then
rcomments = rcomments & "CUSIP " & RRcell.Offset(, -5).Value & ", " & RRcell.Offset(, -4).Value & "has a return of " & RRcell & "; "
End If
Next RRcell
End Sub
Related
I have:
Set QuanityRange = Sheets("Raw_Data").Range("F2:F" & lastDataRow)
MsgBox "This is the Data Range: " & vbNewLine & "Quanities: " & QuanityRange.Address()
This displays $F$2:$F$1838 in the message box.
Then if I have:
Total = WorksheetFunction.Sum(QuanityRange)
MsgBox "Total is: " & Total
This displays 15170 in the message box.
My problem is that I currently have:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(Raw_Data!$F$2:$F$10000)"
This works, it puts 15170 in each cell from B2 to BO114 which happens to be the location of (lastMatrixRow, lastMatrixCol) in this case.
However, what I want to do is:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(QuanityRange)"
instead of:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(Raw_Data!$F$2:$F$10000)"
but it doesn't work. How can I do this?
Can you try with:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(" & QuanityRange.Address() & ")"
If you are trying to put the address of a range variable in a formula - this is one solution:
Public Sub TestMe()
Dim varRange As Range
Set varRange = Range("F1:F5")
Range("A1", Cells(3, 3)).Formula = "=Sum(" & varRange.Address & ")"
End Sub
The idea is that the formula to the right should be "translatable" to a string.
Trying to use an Excel-macro that will automatically save the workbook once certain cells are filled in. The macro will check when changes are made to specific cells, then use variable data to save the workbook through a folder system organized by year and quarter, while giving the Workbook a name based on the Current date and a cell number. The macro will also check to see if the network path (it being on a server) is connected, and if not, exit the sub. I am getting a compile error "Expected: end of statement" at
Set mTitle = Year(Now)," & . & ", Month(Now), " &.& ", Day(Now), " & - & ", ActiveWorkbooks.Sheets("Control").Cells(1, "C")
I want to save the workbook with the following format: Year.Month.Day - CellValue, but it looks like VBA doesn't like periods. How can I solve this? Full code below.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cYear As String
Dim Quarter As String
Dim fdObj As Object
Dim mTitle As String
Dim sCheck
Application.ScreenUpdating = False
Set cYear = Year(Now)
Set Quarter = (Month(Now) + 2) \ 3
Set fdObj = CreateObject("Scripting.FileSystemObject")
sCheck = "S:\Estimating Data\Estimates\test.txt"
Set mTitle = Year(Now)," & . & ", Month(Now), " &.& ", Day(Now), " &.& ", ActiveWorkbooks.Sheets("Control").Cells(1, "C")
If Intersect(Target, Range("C1:C5")) Is Nothing Then
Exit Sub
Else
If WorksheetFunction.CountA(Range("C1:C5")) = 0 Then
Exit Sub
Else
Shell ("Net View \\S:\ > " & vsFileName)
If FileLen(vsFileName) = 0 Then
Exit Sub
Else
If fdObj.FolderExists("S:\Estimating Data\Estimates\" & cYear & "\""Q" & Quarter & ".*xlsm") Then
ActiveWorkbook.SaveAs Filename:="S:\Estimating Data\Estimates\" & cYear & "\""Q" & Quarter & "\" & mTitle & ".*xlsm"
Else
fdObj.CreateFolder ("S:\Estimating Data\Estimates\" & cYear & "\""Q" & Quarter & ".*xlsm")
End If
End If
End If
End If
End Sub
I have an excel-workbook containing two worksheets, and I have written code to transfer data from sheet No.1 to sheet No.2.
What I need is to include a condition that checks if the column G does not contain a certain value. In that case I would like a MsgBox to display "Check..".
The interested range in the Sheet 1 is (A3:J50), so the condition would interest cells G3 to G50.
My current code is:
Sub kk()
Dim lastrow As Integer
lastrow = [b50].End(xlUp).Row
Range("b3:J" & lastrow).Copy Sheets("Daily Rec.").Range("b" & Sheets("Daily Rec.").[b1000].End(xlUp).Row + 1)
Range("b3:j" & lastrow).ClearContents
MsgBox ("Date Posted")
Sheets("Daily Rec.").Activate
MsgBox ("Check..")
End Sub
please advice
This should help get you started.
But like others have mentioned, we need more info to help.
Sub Okay()
Dim source As Range
Dim target As Range
Dim found As Range
Dim cell As Range
Set source = ThisWorkbook.Worksheets("Sheet 1").Range("A3:J50")
Set target = ThisWorkbook.Worksheets("Sheet 2").Range("G3:G50")
For Each cell In source.Cells
Set found = target.Find(cell.Value)
If found Is Nothing Then
MsgBox "Check.." & vbNewLine _
& "Cell [" & cell.Address(0, 0) & "] on sheet [" & cell.Parent.Name & "]" _
& vbNewLine _
& "was not found within " & vbNewLine _
& "cell range of [" & target.Address(0, 0) & "] on sheet [" & target.Parent.Name & "]"
End If
Next cell
End Sub
I have a pivot table where I have applied a date filter:
I am looking for a way to display the filter information in a cell.
e.g. between 1/1/2015 and 10/3/2015
I have tried the following to just get it to display the information in a message box:
Sub DisplayRange()
With ActiveSheet.PivotTables("OrdersPerSlot").PivotFields("PickDate").PivotFilters(1)
MsgBox "FilterType: " & .FilterType & vbCr _
& "Value1: " & .value1 & vbCr _
& "Value2: " & .value2
End With
End Sub
I get the following error:
Next I moved the code into the "ThisWorkBook" Object in case there was some referencing issue and got this error:
I think you need VBA for this. By running the Macro Recorder while adding a date filter I came up with:
Sub GetPivotFilterDates()
Dim pvt As Excel.PivotTable
Dim pvtField As Excel.PivotField
Set pvt = Worksheets(1).PivotTables(1)
Set pvtField = pvt.PivotFields("Date Range")
With pvtField.PivotFilters(1)
If .FilterType = xlDateBetween Then
Worksheets(1).Range("A1").Value = "Filter is between " & .Value1 & " and " & .Value2
End If
End With
End Sub
I have been trying this code out and If I use direct cell reference in the formula it works fine but when I substitute the cell reference for a variable it doesn't work.
Can you tell me where im going wrong.
The aim is to add up all the cells which contain a date in november
This is the code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim last As Long
Dim cussat As Variant
Dim Cussatrange As String
With ActiveSheet
last = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
If Target.Address = "$C$" & last + 1 Then
Range("$B$" & last + 1).Value = Date
Range("$A$" & last + 1).Value = "Moss"
Cussatrange = "J1:J" & last
' I would like to substitue the cell reference in the above formula to use Cussatrange or last
cussat = [=SUMPRODUCT(--(TEXT(J1:J43,"mmm yyyy")="Nov 2014"))]
MsgBox "Last used row number in column A is " & last & " " & cussat & " " & Cussatrange
End If
End Sub
If I understand your question correctly, then you can use something like this:
Dim myFormula As String
Dim template As String
template = "=SUMPRODUCT(--(TEXT({0},""mmm yyyy"")=""Nov 2014""))"
myFormula = Replace(template, "{0}", Cussatrange)
cussat = Application.Evaluate(myFormula)
MsgBox "Last used row number in column A is " & last & " " & cussat & " " & Cussatrange
In the code a template for the formula is created and then the substring (in this case it is {0}) is repalaced with the address which is stored in the string Cussatrange.
For more info about Application.Evaluate method have a look here:
http://msdn.microsoft.com/en-us/library/office/ff193019%28v=office.15%29.aspx.
(Using square brackets (for example, "[A1:C5]") is identical to calling
the Evaluate method with a string argument)