I need help arranging different values using vba code - vba

So I have an excel sheet that has information that a user can put to look at different times. The problem I have is that I want the user to pick on cell c7 if they want to use the server or grid, depending on their answer they will get different responses on the cell (c9). In the code, I am having a problem that it doesn't know if the user picks grid or server. So I would like to add that to my code. Here is my code.
Sub findData()
Dim workflow As String
Dim server As String
Dim finalrow As Integer
Dim i As Integer
workflow = Sheets("Sheet1").Range("c5").Value
server = Sheets("sheet1").Range("c9").Value
Grid = Sheets("sheet1").Range("c9").Value
finalrow = Sheets("Sheet3").Range("c100").End(xlUp).Row
For i = 5 To finalrow
If Cells(i, 3) = workflow Then
If Cells(i, 4) = server Then
If Cells(i, 5) = Grid Then
Range(Cells(i, 2), Cells(i, 12)).Copy
Range("j42").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
End If
End If
Next i
End Sub

Why not this approach instead?
Sub findData()
Dim workflow As String
Dim finalrow As Integer
Dim i As Integer
workflow = Sheets("Sheet1").Range("c5").Value
finalrow = Sheets("Sheet3").Range("c100").End(xlUp).Row
For i = 5 To finalrow
If Cells(i, 3) = workflow Then ' 1. If Cells(i, 3) = workflow
If Cells(i, 4) = "Server" Then ' 2. If Cells(i, 4) = "Server"
Range(Cells(i, 2), Cells(i, 12)).Copy
Range("j42").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
ElseIf Cells(i, 4) = "Grid" Then ' 2. If Cells(i, 4) = "Server"
Else ' 2. If Cells(i, 4) = "Server"
'not handled
End If ' 2. If Cells(i, 4) = "Server"
End If ' 1. If Cells(i, 3) = workflow
Next i
End Sub

Related

VBA EXCEL Compare Columns and bring over the value

Image1
Hi, Referring to the image, I am trying to compare column G and Column K, if the value is the same then copy the value in column J to column F. However, my code doesn't copy the value from Column J to F.
Sub createarray1()
Dim i As Integer
Dim j As Integer
Dim masterarray As Range
Set masterarray = Range("D3:G12")
Dim sourcearray As Range
Set sourcearray = Range("H3:K26")
For i = 1 To 10
For j = 1 To 25
If masterarray(i, 4).Value = sourcearray(j, 4).Value Then
masterarray(i, 3) = sourcearray(j, 3).Value
Else
masterarray(i, 3).Value = ""
End If
Next
Next
End Sub
Function concatenate()
Dim nlastrow As Long
For i = 2 To Cells(Rows.Count, "D").End(xlUp).Row
Cells(i, "G").Value = Cells(i, "D").Value & "_" & Cells(i, "E").Value
Next i
Dim nnlastrow As Long
For i = 2 To Cells(Rows.Count, "H").End(xlUp).Row
Cells(i, "K").Value = Cells(i, "H").Value & "_" & Cells(i, "I").Value
Next i
End Function
Use variant arrays, that way you limit the number of calls to the sheet to only 3.
When your positive is found you need to exit the inner loop.
Sub createarray1()
Dim i As Long
Dim j As Long
Dim masterarray As Variant
Dim sourcearray As Variant
With ThisWorkbook.Worksheets("Sheet1") ' change to your sheet
masterarray = .Range("D3:G12")
sourcearray = .Range("H3:K26")
For i = LBound(masterarray, 1) To UBound(masterarray, 1)
masterarray(i, 3) = ""
For j = LBound(sourcearray, 1) To UBound(sourcearray, 1)
If masterarray(i, 4) = sourcearray(j, 4) Then
masterarray(i, 3) = sourcearray(j, 3)
Exit For
End If
Next j
Next i
.Range("D3:G12") = masterarray
End With
End Sub
But this can all be done with the following formula:
=INDEX(J:J,MATCH(G3,K:K,0))
Put it in F3 and copy/drag down.

excel vba search multiple criteria

when I run this VBA macro I get the same result despite putting in different nurse id thanks, this code came from a video that I watch and has been modified to work with multiple criteria
Sub finddata()
Dim nursenumber As String
Dim finalrow As Integer
Dim i As Integer
Dim course As Integer
Dim nurserow As Integer
nursenumber = InputBox("please enter nurse number")
nurserow = InputBox("please enter nurse row")
finalrow = Sheets("S1").Range("A10000").End(xlUp).Row
course = ADORIE
'fire update
For i = 2 To finalrow
Worksheets("S1").Activate
If Cells(i, 1) = nursenumber & Cells(i, 7) = "FIRE" Then
Cells(i, 9).Copy
Worksheets("database").Activate
Cells(nurserow, 2).PasteSpecial
End If
'cpr
If Cells(i, 1) = nursenumber & Cells(i, 7) = "CPRNURL4" Or _
Cells(i, 7) = "BUCPRBYS" Or Cells(i, 7) = "BUCPREMS" Or _
Cells(i, 7) = "CPRACLSR" Or Cells(i, 7) = "CPRADULT" Or _
Cells(i, 7) = "CPRALIED" Or Cells(i, 7) = "CPRBASIC" Or _
Cells(i, 7) = "CPRBYST" Or Cells(i, 7) = "CPRCO567" Or _
Cells(i, 7) = "CPRMANHA" Or Cells(i, 7) = "CPRMCORP" Or _
Cells(i, 7) = "CPRNURL4" Then
Cells(i, 9).Copy
Worksheets("database").Activate
Cells(nurserow, 3).PasteSpecial
Next i
End Sub
Following my comments above, also, your code is screaming for a Select Case instead of your multiple Or.
Code
Sub finddata()
Dim nursenumber As String
Dim finalrow As Integer
Dim i As Integer
Dim course As Integer
Dim nurserow As Integer
nursenumber = InputBox("please enter nurse number")
nurserow = InputBox("please enter nurse row")
finalrow = Sheets("S1").Range("A10000").End(xlUp).Row
course = ADORIE
With Worksheets("S1")
For i = 2 To finalrow
If .Cells(i, 1) = nursenumber Then
Select Case .Cells(i, 7).Value
Case "FIRE"
.Cells(i, 9).Copy Destination:=Worksheets("database").Cells(nurserow, 2)
Case "CPRNURL4", "BUCPRBYS", "CPRACLSR", "CPRADULT", "CPRALIED", "CPRBASIC", "CPRBYST", "CPRCO567", "CPRMANHA", "CPRMCORP", "CPRNURL4"
.Cells(i, 9).Copy Destination:=Worksheets("database").Cells(nurserow, 3)
End Select
End If
Next i
End With
End Sub

excel vba finding with like or regex

I'm writing a VBA program.
I have a problem with finding this string [BLOCKED] in one column
For j = 0 To 4
For i = 2 To lastrow
If Cells(i, 12).Value = groupnames(j) And Cells(i, 8).Value Like "*" & "[BLOCKED]" & "*" Then
groupsum(j) = groupsum(j) + 1
End If
Next i
Next j
The problem is I have 96 cells for this string but the program found 500 how can I do this to going work?
Thanks for help
The syntax of your Like operation is incorrect. Use:
... Like "*[[]BLOCKED]*"
[...] is a Character class. So, the way you have it written in your question, it will find any single character in the set of BLOCKED. That is not what you want, apparently.
To match the [ character, you enclose it within a character class, as I have shown. To match the ] character, it must be outside of a character class.
here is my code
Sub blocked()
Dim SfileUsers As String
Dim path As String
Dim pathread As String
Dim sFileread As String
Dim lastrow As Long
Dim keres() As Variant
Dim groupadd() As String
Dim groupnames(4) As String
Dim groupsum(4) As Long
path = "C:\Users\uids9282\Desktop\"
SfileUsers = "Users.xlsx"
Workbooks.Open path & SfileUsers
Dim hossz As Long
hossz = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
ReDim keres(hossz)
ReDim groupadd(hossz)
For i = 2 To hossz
keres(i) = Sheets(1).Cells(i, 2).Value
groupadd(i) = Sheets(1).Cells(i, 4).Value
Next i
'fájlmegnyitás
pathread = "C:\Users\uids9282\Desktop\20170703\"
sFileread = "open.xml"
If Dir(pathread & sFileread) = sFileread Then
Workbooks.Open pathread & sFileread
lastrow = Workbooks(sFileread).Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
Else
MsgBox ("Nincs ilyen nevű excel táblázat. Kérem próbálkozzon újra")
End If
'groupok hozzáadása a fájlhoz
Dim user As String
For j = 2 To hossz
For i = 2 To lastrow
user = Trim(Cells(i, 5).Value)
If user = keres(j) Then
Cells(i, 12).Value = groupadd(j)
End If
Next i
Next j
'group szummázása és átírása
ThisWorkbook.Activate
For i = 2 To 6
groupnames(i - 2) = Cells(i, 1).Value
Next i
Workbooks(sFileread).Activate
For j = 0 To 4
For i = 2 To lastrow
If Cells(i, 12).Value = groupnames(j) And Cells(i, 8).Value Like "*[[]BLOCKED[]]*" Then 'itt van benne a hiba!! groupsum(j) = groupsum(j) + 1
End If
Next i
Next j
ThisWorkbook.Activate
For j = 2 To 6
Cells(j, 4).Value = groupsum(j - 2)
Next j
Workbooks(SfileUsers).Close SaveChanges:=False
Workbooks(sFileread).Close SaveChanges:=True
End Sub
this is my excel file where i want to searching

I need to grayed a cell out depending on user input using vba

So I have this list of information I need the user to submit so that it can be included in a different sheet.(see pic below) I want this code to gray out a row if another is picked. So if the user inputs a "starting" time, then I want the row with "dependency" to gray out, and the user won't be able to use it. IF the user clicks "dependency" then the "starting time" needs to gray out. Here is my current code.
Sub findData()
Dim finalrow As Integer
Dim i As Integer
Dim workflows As String
With Sheets("Sheet1")
workflows = .Range("C5").Value
servergri = .Range("C9").Value
gridf = .Range("C9").Value
StartTime = .Range("c11").Value
End With
With Sheets("Sheet3")
finalrow = .Range("C" & Rows.Count).End(xlUp).Row
For i = 5 To finalrow
If .Cells(i, 3) = workflows And (.Cells(i, 4) = servergri Or .Cells(i, 5) = gridf) Then
.Rows(i).Insert
'Add new information to the new row.
'The new row number is still = i
.Cells(i, 3) = workflows
.Cells(i, 4) = servergri
.Cells(i, 6) = StartTime
.Cells(i, 3).Resize(2, 4).Interior.ColorIndex = 8
'If you only want to add one row then your should exit the loop
Exit For
End If
Next
End With
Sheets("Sheet1").Range("c9").ClearContents
End Sub

Loop Crashing Excel VBA

i have been having problems with getting my code to run through its conditional loop and stopping. Here is what I have:
Do While True
Dim i As Integer
i = 2
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
Range(Cells(i, 1), Cells(i, 22)).Copy
Range(Cells(i, 1), Cells(i, 22)).PasteSpecial
i = i + 1
Else
Exit Do
End If
Loop
What I'm trying to do is to get the program to check if one cells isn't empty and if another doesn't have an error in it, if that condition is met, then the program would copy a certain row and re-paste it as just it's values since some of the cells in the row is a formula. For some reason the loop doesn't exit and Excel crashes, am I missing something?
the i = 2 should be outside
Dim i As Integer
i = 2
Do While True
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
Range(Cells(i, 1), Cells(i, 22)).Copy
Range(Cells(i, 1), Cells(i, 22)).PasteSpecial
i = i + 1
Else
Exit Do
End If
Loop
Two points :
The i = 2 must be outside the while loop.
Don't use Copy and PasteSpecial. Using the clipboard will give lots of problems later on. Additionally PasteSpecial likes you to be specific with "what" PasteSpecial action you're using. Rather assign values directly.
Dim i As Integer, Dataset As Variant
i = 2
Do While True
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
'This seems silly, but it overwrites the cell with its value.
Dataset = Range(Cells(i, 1), Cells(i, 22)).Value
Range(Cells(i, 1), Cells(i, 22)).Value = Dataset
i = i + 1
Else
Exit Do
End If
Loop