Program Description I want this program to msgbox every time when cell is changing. For ex. If i have AAA in row1 and row2 BBB i want my code to recognize when cell is changing from one string to another string.
Problem I never used change function before so i don't know where to use it in my code. Can anyone help me out with how to use change function or any other ways to keep track of string changing. Somehow my change function not working.
Sub xym()
Dim x As String, dtext, lastrow As Long, ws1 As Worksheet, wb As Workbook
Dim rangnum As Range, i As Long
Set wb = ActiveWorkbook
Set ws1 = wb.Worksheets("Sheet1")
lastrow = ws1.UsedRange.Rows.Count + 1
Set rangenum = ws1.Range("A1:A" & lastrow)
dtext = rangenum.Value
For i = 1 To UBound(dtext, 1)
If dtext(i,1).change then msgbox "yes"
Next i
End Sub
Please try this:
Sub xym()
Dim i&, v
With Sheet1.[a1]
v = .Resize(.Item(.Parent.Rows.Count).End(xlUp)(2).Row)
End With
For i = 2 To UBound(v) - 1
If v(i, 1) <> v(i - 1, 1) Then
MsgBox "Yes." & vbLf & "Cell A" & i & " is different."
End If
Next
End Sub
You need to use the Worksheet Change Event - there is a clear explanation of how to use this event on the following page, including how to only respond to changes that happen in certain cells.
Excel VBA: Automatically Run Excel Macros When a Cell Changes/Enter Data. Worksheet Change Event
http://www.ozgrid.com/VBA/run-macros-change.htm
Related
I am trying to use the offset function and cannot figure out the problem with my synatx.
I copied this code from another question about offset on this website.
I want to look for a string in column X (starting at X9 always) and if present, I want to know the value of the cell that is two columns over and in the same row.
I would like to use the offset value in an additional part of the same code, so it needs to be named as a variable, but I decided to see if I could first get VBA to at least read the information I want, hence the message box.
Here is the code:
Private Sub CommandButton3_Click()
Dim LeftStrike As Range
Dim FrameLeftStrike As Range
Dim lastRow As Long
lastRow = Range("X" & Rows.Count).End(xlUp).Row
Set LeftStrike = Range("X9:X" & lastRow)
Set FrameLeftStrike = Range("LeftStrike").Offset(0, 4).Value
For Each FrameLeftStrike In LeftStrike
If InStr(1, LeftStrike.Value, "Foot Strike") > 0 Then
MsgBox FrameLeftStrike
End If
Next FrameLeftStrike
End Sub
The variable "FrameLeftStrike" is the problem.
I receive:
application defined or object defined error.
I tried different iterations.
If I change the line of code to,
Set FrameLeftStrike = Sheet4.Range("LeftStrike").Offset(0, 4).Value
I get the same error.
If I change it to
Set FrameLeftStrike = LeftStrike.Offset(0, 4).Value
I get
run-time error '424' Object required.
I want to use this code in the active sheet only, but the name of the active sheet will change as it will get copied as a template for other projects.
Loop through each cell (my_cell) in Column X (my_range)
Individually check if the cell contains Foot Strike
If so, return the value 2 cells to right in a MsgBox
Sub test()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update
Dim my_range As Range, my_cell As Range
Dim lr As Long
lr = ws.Range("X" & ws.Rows.Count).End(xlUp).Row
Set my_range = ws.Range("X9:X" & lr)
For Each my_cell In my_range
If InStr(my_cell, "Foot Strike") Then
MsgBox my_cell.Offset(0, 2)
End If
Next my_cell
End Sub
I have two worksheets in different workbooks. Each sheet can have only a few lines to thousands of lines. They never have the same number of lines.
In Column E of the Capital worksheet, I want to find any and all cells that contain ITS#### where #### are numeric characters. When a cell is identified, I want to go to column A of that row and identify that value. I then want to find the value I just identified (Column A) in column J of the Trans worksheet which is in a different workbook. If a match is found, I want the value of column I in the Trans workbook to be changed to "Cost of Goods Sold/Expense.
I have searched the Internet for weeks and have tried many different solutions to similar problems, but have found nothing that works. I believe I could figure it out if someone could get me past the indicated line. I keep getting a
Run-time error 1004 Method Range of object _worksheet failed.
The following code is one that I was working on, but I was just tying to get past the error so it doesn't even try to tackle the entire problem.
Thank you for any help you may provide.
Sub ITSTRANSCOM()
'
' ITSTRANSCOM Macro
'
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Variant
Dim C As Variant
Dim Lrow As Variant
Dim Lastrow As Variant
Set ws1 = Worksheets("Capital")
Set ws2 = Worksheets("Trans")
Lrow = ws1.Cells(ws1.Rows.Count, "A:A").End(xlUp).Row
Lastrow = ws2.Cells(ws2.Rows.Count, "j:j").End(xlUp).Row
'Run-time error occurs on next row.
For Each i In ws1.Range("A:A", Lrow)
For Each C In ws2.Range("J:J", Lastrow)
If i.Cells.Value = C.Cells.Value Then
If C.Cells.Value = "ITS####" Then
i.Cells.Interior.ColorIndex = xlNone
End If
End If
Next C
Next i
End Sub
Try something like this:
Dim I as Integer, C as Integer
Dim Tmp_Val as Variant
For I = 1 to LRow
If Left(UCase(Ws1.Range(“E” & I).Value),3) = “ITS” then
Tmp_Val = Ws1.Range(“A” & I).Value
For C = 1 to LastRow
If Ws2.Range(“J” & C).Value = Tmp_Val then
Ws2.Range(“I” & C).Value = “Cost of Goods Sold/Expense”
Exit For
End if
Next C
End if
Next I
Your solution looks like it is on the way there...just try replacing the line:
If C.Cells.Value = "ITS####" Then
With the line:
If UCase(Left(C.Cells.Value,3)) = "ITS" Then
I think that will allow you to identify the cells you want, and you seem like you should be capable of developing the code to shift those values between your sheets (based on your other code).
Try changing
For Each i In ws1.Range("A:A", Lrow)
to
For Each i In ws1.Range("A1", "A" & Lrow)
Same with For Each C In ws2.Range("J:J", Lastrow):
For Each C In ws2.Range("J1", "J" & Lastrow)
Looks simple. Perhaps I did not get your explanations right ?
Dim c As Range, r As Range
For Each c In ws1.Range("A:A").SpecialCells(xlCellTypeConstants, xlTextValues).Cells
If c.Value Like "ITS[0-9][0-9][0-9][0-9]" Then
Set r = ws2.Range("J:J").Find(c.Value)
If Not r Is Nothing Then
r.Offset(0, -1) = "Cost of Goods Sold/Expense"
Else
Debug.Print c.Value, " not found"
End If
End If
Next c
Debug.Print "Done"
I am trying to apply "=IFERROR" to a spreadsheet containing over 1000 rows of data. I already came up with a way to make the entries hard-coded. But is there a way to fill the cells with something like "=IFERROR(IFERROR(A1,B1),"")" rather than the value? Below is the hard-coded version:
Sub HardCodeIFERROR()
Dim a As Integer, xRecordCount1 As Integer
Set w(1) = Sheets("ABC")
xRecordCount1 = w(1).Cells(Rows.Count, 1).End(xlUp).Row
For a = 1 To xRecordCount1
w(1).Cells(a, 3).Value = Application.WorksheetFunction.IfError(Application.WorksheetFunction.IfError(Range("A" & a), Range("B" & a)), "")
Next a
Exit Sub
End Sub
Thank you in advance for your help!
You can instead just use .Formula:
w(1).Cells(a, 3).Formula = "=IFERROR(IFERROR(A" & a & ",B" & a & "),"""")"
Note you can skip the loop and just use a range:
Sub HardCodeIFERROR()
Dim ws1 As Worksheet
Dim a As Integer, xRecordCount1 As Integer
Set ws1 = Sheets("Sheet1")
xRecordCount1 = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
With ws1
.Range(.Cells(1, 3), .Cells(xRecordCount1, 3)).FormulaR1C1 = "=IFERROR(IFERROR(RC[-2],RC[-1]),"""")"
End With
End Sub
Note: Make sure to use the sheet with the Rows.Count whenever you use it, just like you do with Cells() and Range(). Also, I changed the sheet name because I wasn't sure if you intended to do a Sheet Array or not, so I used a more clear (IMO) variable name.
Just use the Formula property:
Sub HardCodeIFERROR()
Dim a As Integer, xRecordCount1 As Integer
'Need to declare the size of the array if you are going to assign worksheets to "w(1)"
Dim w(1 To 1) As Worksheet
Set w(1) = Sheets("ABC")
'Ensure you fully qualify "Rows.Count" by specifying which worksheet you are referring to
xRecordCount1 = w(1).Cells(w(1).Rows.Count, 1).End(xlUp).Row
'Apply formula to all cells
w(1).Range("C1:C" & xRecordCount1).Formula = "=IFERROR(IFERROR(S1,V1),"""")"
End Sub
I am trying to retrieve data from another file using the VLOOKUP function however this is only to happen depending on if any of the 3 items of data appear in column 8(H)
OLY
OLY - QUO
OLY - PRO
I have the following and know this is not correct
Sub BlockAllocationsVlookupAll()
Dim x As Long
For x = 1 To 65536
If InStr(1, Sheet1.Range("$H$" & x), "OLY") > 0 Then
Sheet1.Range("$I$" & x) = Sheet1.Range("$I$" & x) & "sometext"
End If
Next
End Sub
I know the above doesn't do exactly what I need can anyone help as to what needs to be edited to include the Vlookup below
=VLOOKUP(A21,'[001 - Allocations - Blocks.xls]CurrentDayAll'!$1:$65536,9,FALSE)
The other issue is that the cell the VLOOKUP points to first will also change due to the varying length of the report
Thank you for any help given
UPD:
As follows up from comments,
column H is in Allocations.xls workbook
there are a set of criterias
formula should be placed in cell only if corresponding cell in column H matches any of thouse criterias.
Working code:
Sub BlockAllocationsVlookupAll()
Dim x As Long
Dim lastrow As Long
Dim searchCriterias As String
Dim wb As Workbook
Dim ws As Worksheet
'specify correct path to your workbook
Set wb = Workbooks.Open("C:\Allocations.xls")
'If workbook is already opened use next line
'Set wb = Workbooks("Allocations.xls")
Set ws = wb.Worksheets("Current Day")
searchCriterias = "|OLY|SVC|SVC-PRO|SVC-QUO|EUR|EUR-PRO|EUR-QUO|"
With ws
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
For x = 4 To lastrow
If InStr(1, searchCriterias, "|" & .Range("H" & x) & "|") > 0 Then
.Range("I" & x).Formula = "=VLOOKUP(A" & x & ",'[001 - Allocations - Blocks.xls]CurrentDayAll'!$A:$I,9,FALSE)"
End If
Next
End With
'Comment next line if you don't want to close wb
wb.Close (True)
Set wb = Nothing
End Sub
I have the following piece of code for Vlookup. The function works fine but the found out value aint getting displayed in the cell. However if i had a used Msgbox function the found out value is shown. The question is doesnt VLOOKUP result be captured in a cell?
Sub Example_of_Vlookup()
Dim lookFor As Range
Dim rng As Range
Dim col As Integer
Dim found As String
Dim lastrowrange As Long
Dim area As Range
lastrowrange = [A65536].End(xlUp).Row
Set lookFor = Sheets("Sheet2").Range("b2")
Set rng = Sheets("Sheet2").Columns("t:u")
Set taxRange = Range("f2", Cells(lastrowrange, 22))
col = 2
On Error Resume Next
For i = 1 To lastrowrange
found = Application.VLookup("B2", "T1:U4", 2, True)
If IsError(found) Then
MsgBox lookFor & " not found"
Else
area.Cells(i, 2).Value = found
End If
Next i
On Error GoTo 0
End Sub
You did not set the range "area" equal to anything, so this line won't show your answer properly:
area.Cells(i, 2).Value = found
Change area.Cells(i,2).value to sheets("Sheet2").Cells(i,2).value or wherever you want your answer to show. Or, set area equal to something if you want to use area.cells.
Idea is simple - I have country names in Column B.Inention is to pull out the Area under which country belongs - My look up values are in column S(country) and T(area) and display the result in column F – Sayanth Sasidharan 25 mins ago
If my understanding is correct as per your explanation then you do not need to use a loop. Let Excel do the Dirty Work ;) You will end up with far less code.
Let's say your sheet looks like this
Logic:
Find the last row of Col B
Insert the Vlookup formula in F1:F & LastRow in one go
Convert them to values.
Code:
Option Explicit
Sub Example_of_Vlookup()
Dim ws As Worksheet
Dim lRow As Long
Set ws = ThisWorkbook.Sheets("Sheet2")
With ws
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
'~~> =IF(ISERROR(VLOOKUP(B1,S:T,2,0)),"",VLOOKUP(B1,S:T,2,0))
.Range("F1:F" & lRow).Formula = _
"=IF(ISERROR(VLOOKUP(RC[-4],C[13]:C[14],2,0)),"""",VLOOKUP(RC[-4],C[13]:C[14],2,0))"
.Range("F1:F" & lRow).Value = .Range("F1:F" & lRow).Value
End With
End Sub
Result: