I'm analyzing survey responses and just want to recode them from the assigned numerical value back to their original meanings using a command button (is this even the most efficient format?). I'd like to escape the tedium of lots of clicking and "Find/Replace". Here's a sample of what I'm working with:
Sub Button1_Click()
Dim response As Integer, result As String
response = ActiveSheet.Range("A3:A58").Value
If response = 1 Then result = "Individual Public School"
...
If response = 8 Then result = "Museum (or other science-rich institution)"
ActiveSheet.Range("B3:B58").Value = result
End Sub
Excel is stopping me at the third line, prompting me with a debug option. Where am I going wrong? Is there a more elegant solution for this?
I'm on a Mac computer, and I'm using Microsoft Office 2010 version, if this helps at all.
Any and all tips are welcome! Thanks.
You need something like:
Sub Button1_Click()
For Each r In Range("A3:A58")
If r.Value = 1 Then r.Offset(0, 1) = "Individual Public School"
'...
If r.Value = 8 Then r.Offset(0, 1) = "Museum (or other science-rich institution)"
Next r
End Sub
Related
I'm trying to concatenate Tier Code and Cap Code where Cap Level = 2. I was able to successfully do the concatenation earlier using a case statement, without the condition that Cap Level = 2. However, now that I'm trying to add that condition, I keep getting the error "Type Mistmatch". I've tried setting Range("E3:E24").Value = 2 and = "2". Both ways, I still get the error.
My data currently looks like this:
I've been playing around with this for at least an hour now and reading other questions about case statements with multiple conditions/criteria, but I haven't been able to get my code to work properly.
Sub Concat_ParentCode_Cap1_001()
With Worksheets("PD Code Structure")
Dim ParentCode As Range
Dim TierCode As String
Dim CapCode As String
CapCode = "FS_CAP_1_001"
TierCode = "FS_Tier_1"
Set ParentCode = Range("F3:F24")
Select Case True
Case CapCode = "FS_CAP_1_001" And Range("E3:E24").Value = "2"
ParentCode = TierCode & "." & CapCode
End Select
End With
End Sub
I would like my data to look like this:
There are a few things in the code provided that will cause it not too work, but the main thing is a loop to check each cell.
Give this a shot:
Option Explicit
Sub Concat_ParentCode_Cap1_001()
Dim tierCode As String
tierCode = "FS_TIER_1."
With Worksheets("PD Code Structure")
Dim capCode As Range
For Each capCode In .Range("F2:F24")
If Len(capCode.Offset(, -1)) Then
Dim capParent As String
capParent = capCode.Value
End If
If capCode.Offset(, -2).Value = 2 Then
capCode.Offset(, -1).Value = tierCode & capParent
End If
Next
End With
End Sub
Do you really need to use VBA for this ? This looks like something you can do with just an if statement on excel. =if(B2=2,CONCAT(A1,".",D1),""). This is of course assuming the columns are listed A-D above.
I need to change the status and write comments for about 100 lists in SharePoint every week. I tried to automate it. I know how to open them in edit mode with a macro, but I don't know how to change status or how to write a comment with a macro, any ideas?
Here is my code:
Sub TT()
Dim ie(40) As Object, obj As Object
Dim cislo As String
For i = 0 To 40
If Cells(i + 2, 1).Value = "" Then
Exit Sub
End If
Set ie(i) = CreateObject("Internetexplorer.Application")
ie(i).Visible = True
ie(i).Navigate "http://adress of sharepoint list .com"
Do While ie(i).Busy
Loop
Next i
End Sub
These tutorials should give you what you need to know...excellent and well done, they show you how to do it via Listobjects and via SQL
https://www.youtube.com/watch?v=nM-gq3N6f2E
There is a series of 13 videos.
I found an old solution made by a user named n1ghthawk (2012!), using shapes to form a flowchart that can be filtered by selected shape(s).
Exactly what I need except that the code fails in a certain scenario with connectors. I have failed to fix this myself, so I would appreciate if someone could help me.
I will send a link to the file to respondents instead of posting code, as I think that will make it much easier to help. In the file, I have setup the shapes to show the failing scenario in the simplest possible form.
Thanks John, for helping out.
A collegue of mine just pointed me in the right direction and these 3 lines stopped an infinite loop and made the script move on the the next shape, fixing the failing scenario:
For j = 1 To UBound(MyNames())
If thisshape.Name = MyNames(j) Then Exit Sub
Next
So the entire recursive Sub looks like this now:
Sub Get_LegUp(thisshape As Shape)
Dim con As Variant
Dim i As Long
Dim j As Integer
Dim dependentshape As Shape
'***
For j = 1 To UBound(MyNames())
If thisshape.Name = MyNames(j) Then Exit Sub
Next
'***
namecount = namecount + 1
MyNames(namecount) = thisshape.Name
For i = 1 To shpconlist.Item(thisshape.Name).up.Count
con = shpconlist.Item(thisshape.Name).up(i)
namecount = namecount + 1
MyNames(namecount) = con
Set dependentshape = ActiveSheet.Shapes(con).ConnectorFormat.BeginConnectedShape
Get_LegUp dependentshape
Next i
End Sub
If someone wants the code to recreate the functionality, just post back here and I will put it all in here.
Again, apologies for not following guidelines.
I'm trying to open UserForms based on the values of cells in one row of a sheet. There are 17 UserForms so I don't want to have to use 17 if statements for each form like this:
If ActiveCell.Value = 1 Then
UserForm1.Show
End If
Is there a way that I can use a variable to show the forms?
I was thinking something along the lines of:
Dim i
Do
If ActiveCell.Value = "" Then
Exit DO
End If
i = ActiveCell.Value
UserForms("UserForm" & i).Show ****THIS is what doesn't work
ActiveCell.Offset(0,1).Select
Loop
Paste the code from the link Harvey provided, then adjust this line in your code:
UserForms("UserForm" & i).Show ****THIS is what doesn't work
to:
ShowAnyForm ("UserForm" & i)
That's a great link Harvey, I've bookmarked it!
You can use the often overlooked VBA.UserForms object. See this link which fully descibes what you need to do.
There's no point in my explaining it here.
Harvey
I've never come accross the method mentioned by #Harvey (I like it, though) so would have used some sort of Select Case statement:
Select Case .Cells(1,1).Value
Case 1:
FormOne.Show
Case 2:
FormTwo.Show
' And so on and do forth...
Case Else:
MsgBox ("Invalid entry")
End Select
Simpler than 17 If statements, at least.
The easiest way is this one:
Dim activeuf as Object
Set activeuf = UserForm & i
activeuf.show
Perhaps it will not work for the person who asked, but I'm sure it will help people who check this question in the future
Get UserForm object defined by its string name
Function Form(Name As String) As Object
Set Form = CallByName(UserForms, "Add", VbMethod, Name)
End Function
Sub Test()
Dim strFormName As String
strFormName = "UserForm1" ' <-- replace by your lookup code instead
Form(strFormName).Show
End Sub
Here a "faster" code to open the form (Rao-Haribabu evolution):
Dim forMy
Set forMy = CallByName(UserForms, "Add", VbMethod, formName) ' formName is the form name to open
forMy.Show
I used Data Validation to create some drop down lists. Anyone know how to use VBA to select the first iterm in a drop down list?
I tried 'Split' function:
cell.Value = Split(cell.Validation.Formula1, ",")(0)
but it did not work well, it will only work if I put like "option1, option2" in the source in Data Validation window. If I refer the source to a range of options, then it will return with errors.
I guess there should some smarter ways.
Thanks!
Sub test()
Dim adr As String
With Range("c4")
adr = Mid(.Validation.Formula1, 2)
Debug.Print Range(adr).Cells(1, 1)
End With
End Sub
so your answer is:
set c = range("c4")
c.Value = Range(Mid(c.Validation.Formula1, 2)).Cells(1, 1).Value