Excel vba if the statements and auto update - vba

im looking for some basic help with VBA. I have created a userform for fellow members to fill out information when we go on a call. When ok is pressed, the data is sent to sheet2 and laid in the next open row. In sheet 3 i have the printable form of the userform that i made out. I am trying to write the vba code in the printable form, so when the call # changes in cell b2, it will automatically change all other cells in that form, with the corresponding data in the same row from sheet 2.
Im new to the forum so im not sure how to upload etc. but i will try and give an example below.
Sheet2 looks something like this, for the example each cell seperated by "."
18-170001.01/02/17.Accident."current address"."call info"
18-170002.01/02/17.Training."current address"."call info"
in sheet4 this is the code i am using and it is not working
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
For i = 5 To Row.Count
If sheet4 .cells(b2).value=sheet2.Cells(i,1).value then
sheet4.cells(e2).value=sheet2.cells(i,1).value
End If
Next i
End Sub
I would add additional if then statments for each cell in sheet 4 to match each cell in sheet 2.
Please any help is appreciated

You can't use Cells(b2), it needs to be Range("B2") or Cells(2, "B") or Cells(2, 2).
So your code could be:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
For i = 5 To Row.Count
If sheet4.cells(2, "B").value=sheet2.Cells(i,1).value then
sheet4.cells(2, "E").value=sheet2.cells(i,1).value
End If
Next i
End Sub
But you would be better off just using a formula for this purpose instead. For example, the above code could be replaced by having the following formula in cell E2 of sheet4:
=VLOOKUP(B2,Sheet2!A:A,1,FALSE)
(That assumes that sheet2 has a sheet name of "Sheet2".)

Related

Run VBA when cell changes on a different worksheet

I am very new at VBA, but have managed to make a code which links the header of my worksheet to a cell on another sheet.
The code runs every time I click a cell in the active worksheet. I'd like for the code to only run when a certain cell in the other worksheet changes.
"Report" has the header
"Input" has the cells which the code refers to (B18) and the cell I want the code to run on when changed (B3).
The following is the code that I already have.
Private Sub Worksheet_selectionChange(ByVal Target As Range)
ActiveSheet.PageSetup.RightHeader = "&28" & Format(Worksheets("Input").Range("b18").Text)
End Sub
Any help is much appreciated!!!
Not sure about your second paragraph; my understanding is that you want to monitor cell B3 on the Input worksheet, and when it changes, adjust the RightHeader of the PageSetup of worksheet Report so it reflects what's in cell B18 on the Input worksheet. If that's correct, you could put the following code behind the Input worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("B3")) Is Nothing Then
ThisWorkbook.Worksheets("Report").PageSetup.RightHeader = "&28" & Me.Range("B18").Text
End If
End Sub
...and remove your Worksheet_SelectionChange procedure.
The idea is to monitor for changes on the Input worksheet, and act upon those involving its B3 cell.
Ideally, you'd give names to the cells above, so that your code would continue working even if you add or delete rows and columns on the Input worksheet. See Define and use names in formulas. If you were to define Inputs!B3 as MyMonitoredValue and Inputs!B18 as MyPageSetupValue, the code above would become:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("MyMonitoredValue")) Is Nothing Then
ThisWorkbook.Worksheets("Report").PageSetup.RightHeader = "&28" & Me.Range("MyPageSetupValue").Text
End If
End Sub
...and you could re-organize the layout of the Input worksheet without worries.
Finally, instead of referring to worksheets by their tab's name (as seen from Excel), you can refer to them directly using their CodeName, as seen from the VBA editor, in the Properties window, under the worksheet's (Name) property (press Ctrl+R to show the Project Explorer, click on the worksheet under the Microsoft Excel Objects node, then press F4 to display the Properties window). You can change this value; I'd typically change it to shtReport. Yet another version of the code would then be:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("MyMonitoredValue")) Is Nothing Then
shtReport.PageSetup.RightHeader = "&28" & Me.Range("MyPageSetupValue").Text
End If
End Sub
...which is able to withstand both changes in the Report worksheet's tab name, and reorganization of the Input worksheet.

VBA - Is it possible to transform a cell into a cutom hyperlink?

I am wondering if it could be possible to use VBA in order to be able to transform a cell which already contains a part of the url into an active hyperlink.
To be more specific here is an example of what i've managed to work with so far:
On column B, in cell B14 i have the following formula
=HYPERLINK("https://google.ro/"&D14,"search") -> this is only for row 14
now on cell D14 i have the last part of the URL, for example "dogs". now when i press on cell D14 it open a new browser page and takes me to that specific address.
So my question is, can i do this on a single cell using VBA, on this example in D14, without using Column B anymore? Basically is there a way to integrate the formula used in column B into VBA?
Thanks.
Add an Event-Routine for your sheet. This one will open the browser immediately when clicking on it (in the example, it is limited to column 'D')
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Check if only 1 cell is selected
If Target.Rows.Count > 1 Or Target.Columns.Count > 1 Then Exit Sub
If Target.Column = 4 And Target.Value <> "" Then
ThisWorkbook.FollowHyperlink "https://www.google.de/search?source=hp&q=" & Target.Value
End If
End Sub
If I've understood correctly, you need the ThisWorkbook.FollowHyperlink method
Sub TestHyperlink()
ThisWorkbook.FollowHyperlink "https://google.ro/" & Range("D14")
End Sub
You may need to qualify the Range("D14") with the worksheet name depending where you're calling your code from.

Excel: Copy Dynamic Range from one worksheet to another

I am trying to copy a dynamic range (B12:Lxx)from one worksheet to another. I need the range being copied to stop at the first empty row (there is additional data further down the sheet which I don't want copied).
I am a very basic VBA user so if you could explicitly set out your instructions that would be handy.
Source: Worksheet "MyKPIs" with the dynamic range B12:Lxx (column L is set, row numbers are variable BUT must end at the first empty row)
Target: Worksheet "Month Template", cell B5
Trigger would be a command button
I have trawled through other articles but have failed to find anything that I could use.
Thanks,
Hayley
this will work. insert a command button on your worksheet. double click the button. paste in this code between sub and end sub.
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
it should look like this when you are through. then go to your worksheet on developer tab toggle off design mode then click the button.
Private Sub CommandButton1_Click()
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
End Sub
for those inexperienced with currentregion please look at the 2 samples below that have blank cells but the region is selected and you can easily see the beginning and ending points in the range and how an entire blank row or column forms the range.
Place a command button from the Forms toolbar on your "MyKPIs" sheet.
Then add this code:
Sub Button1_Click()
Dim myrange
Set myrange = Sheets("MyKPIs").Range("B12:L12")
myrange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Worksheets("Month Template").Range("B5")
End Sub
Considering all cells in column B have data, this will copy all the cells in the range. It will stop at the first empty cell in column B. This should help you to start.

How do i insert a new blank cell before current cell that has just been populated

I have a two (very long) TO-DO lists- one going across and the other going down.
What i want to achieve is for a blank cell to appear at the start of the list instead of having to scroll to the end of the lists to enter a new item.
So then when i have entered an item in a cell and hit enter, i want the cell just populated to move down the list (or across if i hit tab) and a new empty cell to appear at the start of the list.
It would be useful for the new blank cell to be pre-populated with the current date but that is not essential.
Thanks for your help.
NOT FOR POINTS.
Piggy-backing on Gary's answer, the mistake is that you set A to Range("C4:C6"). What happens is, when you enter data into any of C4, C5, and C6, they are all moved to the right because of A.Insert, which refers to all the cells assigned to A.
The trick here is to fully qualify your requirements for Target. Let's say you have a table from B1:E3, like below:
Now, let's say you want to move row 1 if you enter something into A1, row 2 if A2, etc. The following macro should do it (notice the difference with Gary's macro):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim QualifyingRange As Range
'Dim OrigRng As String
Set QualifyingRange = Range("A1:A3")
If Intersect(Target, QualifyingRange) Is Nothing Then Exit Sub
Application.EnableEvents = False
'OrigRng = Target.Address
Target.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Range(OrigRng).Value = Date
Application.EnableEvents = True
End Sub
What is the difference in the above? Very simple but very important. When a Worksheet_Change is in a sheet's code, every time you do a valid change to the sheet, the macro fires. The range you just edited will be known to the macro as Target. Now, usually, if you don't declare what the qualifications for Target are, the Worksheet_Change macro just fires indiscriminately. How do we qualify Target properly then?
We use Intersect. First, we declare a range of cells that we want to track. These cells, when changed, should fire the macro. Otherwise, macro is kaput. This line: If Intersect(Target, QualifyingRange) Is Nothing Then Exit Sub basically reads: If Target is not inside my desired range, then nothing happens.
This is the reason why I declared A1:A3 as my QualifyingRange. This way, if my change is to any of the cells above, the macro will fire. HOWEVER, .Insert should not be applied to the whole range but to Target alone. This is because if we do QualifyingRange.Insert, every time a change is detected in any cells in A1:A3, all three rows will move. This is what happened when you set A to three cells and kept A.Insert.
Hopefully, this clears up the confusion. Let us know if this helps.
Here is a partial solution. The following event macro monitors entry to cell A1 . Once you have entered a value in A1, the macro "pushed" the values in column A down by one. This means that value you just entered has been pushed down to A2 and A1 is empty:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
Set A = Range("A1")
If Intersect(A, Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
A.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Application.EnableEvents = True
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
EDIT#1
To push across rather than down:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
Set A = Range("A1")
If Intersect(A, Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
A.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Application.EnableEvents = True
End Sub
To handle multiple cells, you must specify which cells get pushed across and which cells get pushed down.

Excel, 2 sheets, 2 columns, same value?

I have 2 sheets sheet1 and sheet2 in an excel 2007 file.
In sheet2 I have a column that is managed by a form/macro(with a tree view control). When an element has been selected, the cell is filled with an "x", when it has been unselected, the cell is filled with "" (nothing).
In sheet1 I want to create a column equal to the sheet2 column.
So for example: if sheet2!C24 = "x" then sheet1!c24 should also be "x"
I also would like it to work both ways. If the user changes sheet1!c24 to "x", then I want sheet2!c24 to take the same value.
Problems:
- in Sheet1, I tried sheet1!c24 = sheet2!c24, but then when sheet2!c24 = "", sheet1!c24 displays 0 instead of nothing
- in Sheet2, I tried sheet2!c24 = sheet1!c24, but then the cells display the formula (='sheet1!c24') instead of the value...
So basically, what I want is that whatever change you do, in sheet1 or in sheet2, both columns in sheet1 and sheet2 are updated...
How can I achieve this?
What I think you need to do is use the Worksheet_Change events for both sheets and if a change is made in the column you are interested in, then you update the same cell in the other sheet.
Something like this would go in the worksheet code module:
Private Sub worksheet_change(ByVal target As Range)
Dim c As Range
'Test to see if the cell just changed is
'in the column we are interested in
Set c = Application.Intersect(target, Range("A:A"))
If Not c Is Nothing Then
'Copy across to other sheet
If Not beingEdited Then
beingEdited = True
Sheet1.Range(target.Address) = target.Value
beingEdited = False
End If
End If
End Sub
You'd need a beingEdited variable to be declared somewhere else with larger scope so that you could avoid the events triggering themselves and Excel getting stuck in a loop.
In the other sheet you'd basically have the same procedure, except that it would reference the first worksheet, e.g. Sheet1.Range(target.Address) = target.Value.
Obviously, you'd have to tweak this to your ranges/sheets.
You've got the right idea, but you probably need to turn off events before making the change, otherwise you'll end up in a loop
Private Sub worksheet_change(ByVal target As Range)
application.enableevents = false
sheet1.range("c24").value = sheet2.("c24").value
application.enableevents = true
end sub
Just make sure you enable events again at the end.
i did something like this where i had a summary sheet and a tests sheet. When I added a new value in tests sheet and it passed (P) a cell in summary sheet will keep increment. This is to keep a count of how many tests passed. here it is:
COUNTIF(tests!$C$5:$C$1017, "P");
hope this helps.