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.
Related
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 need help with my puzzle. I have written a code that works fine. Now a user wants to see not just the final result but also the formula written in VBA (for audit purposes) to verify that the results are OK. Is there a way to show BOTH results and formula in the same cell?
My code so far:
Sub Vlookup_Condition_VAT_table()
Dim Rng As Range
Dim i As Long
Application.ScreenUpdating = False
Workbooks("GST_recovery_overclaim.xlsm").Worksheets("MonthlyData_Raw").Activate
'Identify the Destination location to start populating vlookuped values
Range("AK2").Activate
With Worksheets("MonthlyData_Raw").Cells
Set Rng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
For i = 2 To Rng.Rows.Count
'populate the destination range with
'vlookup values from the list vlookup table
Rng.Cells(i, 37) = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)
Next
End With
Application.ScreenUpdating = True
End Sub
Thank you very much,
Russ
If you aren't looking at thousands of rows, you could try the following.
Instead of using this line to print the result of your vlookup formula:
Rng.Cells(i, 37) = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)
You could instead put the actual formula in the cell with this change
Rng.Cells(i, 37).Formula = "=VLOOKUP(" & _
.Cells(i, 28).Address & "," & _
"'" & Sheets("VAT_apportionment_table_201310").name & "'!D:G," & _
"4," & _
"False)"
--------------UPDATE BASED ON OP COMMENT-----------------------------
Adding error trapping via an ISERROR function (untested)
Dim errMsg as String
errMsg = "Not in exception list"
Rng.Cells(i, 37).Formula = "=ISERROR(VLOOKUP(" & _
.Cells(i, 28).Address & "," & _
"'" & Sheets("VAT_apportionment_table_201310").name & "'!D:G," & _
"4," & _
"False), " & errMsg & ")"
I created a variable for the error message for a couple of reasons.
Easier to debug later
Dealing with text in a formula that is being sent to the cell like this can be tricky. And using a variable gets around the trickiness.
Maybe add text to the result with the answer and the formula.
Rng.Cells(i, 37) = "Answer: " & Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False) & ", Formula: = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)"
If I undestood you correctly, you want to display the formula entered in a cell with the result. How about this?
'Assume that you have a Range("A1") equals to = cos(20)
Sub GetFormula()
Dim MyFormula As String
MyFormula = Replace(Range("A1").Formula, "=", "")
MsgBox ("Formula is : " & MyFormula _
& " Result is :" & Range("A1"))
End Sub
I'm still quite new to VBA and struggling with the following code!
What I am trying to do is have the search function look through all cells within column 1 and find the criteria of Cell 1 matches PickerName2.Text and offset cell 5 is <>0 and then offset cell 6 = Blank
The problem I have is there a duplicates all the way down column 1 and as soon as it finds the matching name to PickerName2 Then it checks the offset cells 5 & 6 and as these don't match the criteria it gives the message they aren't currently picking.
This is even though further down the sheet there is a record that matches the criteria. I want it to look through all records until it find the criteria or if it has checked all populated cells in column A and nothing matches then it will give the message that they aren't currently picking.
I do hope someone can help :-)
Al
Private Sub CommandButton3_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim strSearch As String
Dim aCell As Range
Dim rng As Range, i As Long
'~~> Set the sheet where you want to search the IMEI
Set ws = Sheets("PickData")
With ws
'~~> Get the value which you want to search
strSearch = PickerName2.Text
'~~> Column A is Column 1 so Column B is 2. This is where we are searching
'~~> xlWhole is used in the code below so that we find a complete match
'~~> xlPart is supposed to be used when you are finding a partial match.
Set aCell = .Columns(1).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> get the row of the cell where we found the match and add data to
If Not aCell Is Nothing And aCell.Offset(0, 5).Value <> "" And aCell.Offset(0, 6).Value = "" Then
MsgBox " " & PickerName2.Text & " is currently picking - " & aCell.Offset(0, 1) & " " & aCell.Offset(0, 2) _
& " " & aCell.Offset(0, 3) _
& " "
UserForm1.Hide
Else
MsgBox " " & PickerName2.Text & " has no outstanding PIK!", vbExclamation
PickerName2.Value = ""
Exit Sub
End If
End With
PickNo2.Value = ""
PickerName2.Value = ""
UserForm1.Hide
End Sub
I have done some searching online and tried something similar to the following code but keep getting a object required error but cannot see where I am going wrong?
Dim rng As Range
Dim i As Integer
Dim finalrow As Integer
finalrow = Sheets("PickData").Range("A10000").End(xlUp).Row
For i = 2 To finalrow
If Cells(i, 1).Value = UserForm1.PickerName2.Text And (Cell.Offset(i, 5) <> "") And (Cell.Offset(i, 6) = "") Then
MsgBox " " & PickerName2.Text & " is currently picking - " & Cell.Offset(i, 1) & " " & Cell.Offset(i, 2) _
& " " & Cell.Offset(i, 3) _
& " "
End If
Next i
Your first code attempt using the Range.Find method is a good start.
Now extend this approach by using the Range.FindNext method.
This link should help you.
In your second block of code:
The references to Cell.Offset... should probably be something like Cells(i,1).Offset(0,5) and Cells(i,1).Offset(0,1) and Cells(i,1).Offset(0,3) respectively.
Also the second reference to PickerName2 should be qualified with UserForm1, as in UserForm1.PickerName2
I am trying to calculate the sum of changing cell range in vba. Unfortunately the cell values are variables. I can't seem to get the following formula to work.
Private Sub calcOverheadRate(startCell As Integer, endCell As Integer)
Total = endCell + 1
Range("D" & Total).Formula = "=SUM("D" & startCell & ":" & "D" & endCell)"
End Sub
I get compile error: "Expected: end of statement
To solve this problem I changed the function to,
Private Sub calcOverheadRate(startCell As Integer, endCell As Integer)
Dim start As String
Dim endC As String
start = "D" & CStr(startCell)
endC = "D" & CStr(endCell)
Total = endCell + 1
Range("D" & Total).Formula = "=SUM(start:endC)"
End Sub
The function compiles fine, when I run it, the value in the cell is "#NAME" where it references SUM(start:endC) not SUM(D5:D23)....
Any thoughts on how to solve this would be appreciated.
The quotes are the issue:
Range("D" & Total).Formula = "=SUM(" & startCell & ":" & endCell & ")"
I have figured out the problem the & needs to be inside the quotation for string literals
Range("D" & Total).Formula = "=SUM(" & start & ":" & endC & ")"
How about you try using a table?
Here is a 1 min video on how to make a table in Excel:
http://www.screenr.com/VvZ8
My Excel sheet has multiple used ranges. I want to copy each range value and concatenate them. What I did is
Set tempRange = Union(SrcWkb.Worksheets("mysheet").Range("F1:H1"), SrcWkb.Worksheets("mysheet").Range("I1:J1"), SrcWkb.Worksheets("NWP").Range("K1:L1"))
For Each eachRange In tempRange
tempString = tempString & eachRange & "/"
MsgBox tempString
Next eachRange
I want to copy the value in merged cells F1:H1 and concatenate a "/" and value from I1:J1 (also merged) and K1 to L1. However, Excel throws "subscript out of range" error. How could I achieve this?
It is not quite clear from your original post what output you need. Here is one option which may help you get started:
Sub ConcatRanges()
Dim rangeOne As Range, rangeTwo As Range, rangeAll As Range, cl As Range, str As String
Set rangeOne = Worksheets("mysheet").Range("I27:K27")
Set rangeTwo = Worksheets("mysheet").Range("L27:N27")
Set rangeAll = Union(rangeOne, rangeTwo)
For Each cl In rangeAll
str = str & cl & " / "
Next cl
Debug.Print str //Output: 1 / 2 / 3 / 4 / 5 / 6 /
End Sub
Updated Post
Dealing with merged ranges can be tricky. For example, the merged range F1:H1 has value 36M. To access the value you have to refer to the first cell in the merged range. Example:
Sub MergedRangeDemo()
Dim rng As Range, cl As Range
Set rng = ActiveSheet.Range("F1:H1")
For Each cl In rng
Debug.Print cl.Value, cl.Address
Next cl
//Output: 36M $F$1 <-- Only first cell contains the value
// $G$1
// $H$1
End Sub
Given this you can concatenate the values by using the rowindex (1) of the range:
Sub ConcatRangesUpdated()
Dim rangeOne As Range, rangeTwo As Range, rangeThree As Range, str As String
Set rangeOne = ActiveSheet.Range("F1:H1")
Set rangeTwo = ActiveSheet.Range("I1:J1")
Set rangeThree = ActiveSheet.Range("K1:L1")
str = rangeOne(1) & " / " & rangeTwo(1) & " / " & rangeThree(1)
Debug.Print str 'Output: 36M / 40M / 36M
End Sub
It appears that you want to concatenate I27 and L27 with a forward slash between and put the results on a different worksheet. This example does just that: concatenates I27 & L27, J27 & M27, K27 & N27 and puts the results in cells A27:C27 on the destination sheet. Note that the formula uses R1C1 notation with relative column positions; adjust as necessary.
Sub ConcatCells()
Dim sSource As String
sSource = "'" & SrcWkb.Worksheets("mysheet").Name & "'!"
DstWks1.Range("A27:C27").FormulaR1C1 = "=" & sSource & "RC[8] & " _
& Chr$(34) & "/" & Chr$(34) & " & " & sSource & "RC[11]"
End Sub