I have two excel sheets. Sheet Providers contains data of my contractors. Column C…
- User
I have two excel sheets. Sheet Providers contains data of my contractors. Column C contains the contractor company name, Column D contains a Sheet Name, Column F a contact name and Column G the contacts email address all in the same row. Occasionally, a contractor might not have a contact name or an email and these cells will be blank.
In my other Sheet Job Request, I want to be able to select in A2 the contacts name from a list, find the matching value in Column C in Sheet Providers then from the same row, automatically populate the relevant contact name into G2, the relevant email address into H2 and the relevant Sheet Name into I2 using formulas that will do this for each of the cells G2, H2 and I2 of Sheet Job Request.
Values in cells B2, C2, D2, E2 and F2 in Sheet Job Request, data will be entered manually. When F2 in Sheet Job Request is entered, which will be a date, I want a VBA code that will open and select the Specific Sheet using the text value in I2 of Sheet Job Request but only if cells A2, B2, C2, D2 and E2 are not blank .then when the new sheet is opened and selected, find in it the next empty row and populate the empty cell in Column B with the value from B2 in Sheet Job Request, populate the empty cell in Column C of the same row with the value from F2 in Sheet Job Request, populate the empty cell in Column F of the same row with the value from C2 in Sheet Job Request, populate the empty cell in Column J of the same row with the value from E2 in Sheet Job Request.
When this has been completed, I want to return to Sheet Job Request, open a text document form this location G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Request0.txt and paste the values of A2, B2, C2, D2 and E2 into the text document each value on a different line.
- Assistant
As an AI language model, I cannot do manual work but I can provide the logical steps that can be followed to achieve your requirements in Excel VBA.
- User
The formula =IF(A2<>“”,VLOOKUP(A2,Providers!C:G,5,FALSE),“”) does not work. I want a formula that will search all Cells A2 to A100 in column C of Sheet Providers for what matches the value in Cell A2 of Sheet Job Request and from the same row where the match is found use the value in column D to populate I2 in the Sheet Job Request
- Assistant
To search for a match in column C of Sheet Providers from cells A2 to A100, you can use the following formula in I2 of the Sheet Job Request:
=IF(A2<>"",VLOOKUP(A2,Providers!2:100,2,FALSE),"")
This formula searches the range C2:D100 in the Sheet Providers for a match with the value in cell A2 of the Sheet Job Request. If a match is found, it returns the corresponding value from the same row in column D of the Sheet Providers. If a match is not found, it returns an empty string ("").
Note that the range of cells being searched is specified as 2:100, assuming that there are no more than 100 rows of data in Sheet Providers. You can adjust the range as needed to include all of the cells containing your data.
- User
=VLOOKUP(A2,Providers!C3:G80,5,FALSE) can this be converted to Index Match
- Assistant
Yes, you can use the INDEX MATCH combination to achieve the same result as VLOOKUP.
The equivalent of
=VLOOKUP(A2,Providers!C3:G80,5,FALSE)using INDEX MATCH is:=INDEX(Providers!G$3:G$80,MATCH(A2,Providers!C$3:C$80,0))This formula searches for the value in A2 in column C of the range C3:C80 of Sheet Providers. It returns the corresponding value from column G of the same row where the value is found.
Note that
MATCH(A2,Providers!C$3:C$80,0)returns the row number of the match, and that row number is used as the index number in theINDEXformula to return the correct value from column G.Also, note that you can adjust the range
C3:C80andG3:G80as per your actual data range. - User
=INDEX(Providers!G80,MATCH(A2,Providers!C80,0)) i am getting an error
- Assistant
If you are getting an error with the formula, there could be several reasons for that. Here are some common errors and their solutions:
#REF! error: If you get a
#REF!error, check if the sheet name (Providers) and the range address (C$3:C$80andG$3:G$80) are correctly spelled and refer to the correct range of cells where your data is located. Make sure that the range of cells you're referencing does not contain any error values or invalid characters.#N/A error: If the formula returns
#N/Aerror, it means that it could not find a match for the value in cell A2 in the rangeC3:C80. Double-check that the value in A2 matches exactly with a value in column C of your data table. You can also try increasing the range to include more cells if necessary.To help you further, if you could provide more details about the error message you're getting or provide a screenshot of the error along with your data, we can provide more specific solutions to your error.
- User
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If ActiveCell.Column = 6 And Range("A2") = "" Or Range("B2") = "" Or Range("C2") = "" Or Range("D2") = "" Or Range("E2") = "" Then MsgBox "Please fill in all the required fields.", vbExclamation, "Missing Information" Exit Sub End If
Dim ws As Worksheet Set ws = Worksheets(Range(“I2”).Value) ws.Select
Dim lastrow As Long lastrow = ws.Cells(ws.Rows.Count, “B”).End(xlUp).Row + 1 ws.Range("B" & lastrow).Value = Range("B2").Value ws.Range("C" & lastrow).Value = Range("F2").Value ws.Range("F" & lastrow).Value = Range("C2").Value ws.Range("J" & lastrow).Value = Range("E2").Value
Dim FilePath As String Dim FileContents As String FilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Request0.txt" Open FilePath For Append As #1 FileContents = Range(“A2”) & vbNewLine & Range(“B2”) & vbNewLine & Range(“C2”) & vbNewLine & Range(“D2”) & vbNewLine & Range(“E2”) & vbNewLine Print #1, FileContents Close #1
End Sub correct the code that triggers the message so that after entering a value in F2 the rest of the code will run if A2 B2 C2 D2 E2 are not blank
- Assistant
To trigger the message after entering a value in F2, and run the remaining code if cells A2, B2, C2, D2, and E2 are not blank, you can modify the Worksheet_SelectionChange event macro as follows:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 6 And Range("A2") <> "" And Range("B2") <> "" And Range("C2") <> "" And Range("D2") <> "" And Range("E2") <> "" Then
Dim ws As Worksheet Set ws = Worksheets(Range("I2").Value) ws.Select
Dim lastrow As Long lastrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
ws.Range("B" & lastrow).Value = Range("B2").Value ws.Range("C" & lastrow).Value = Range("F2").Value ws.Range("F" & lastrow).Value = Range("C2").Value ws.Range("J" & lastrow).Value = Range("E2").Value
Dim FilePath As String Dim FileContents As String FilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Request0.txt" Open FilePath For Append As #1 FileContents = Range("A2") & vbNewLine & Range("B2") & vbNewLine & Range("C2") & vbNewLine & Range("D2") & vbNewLine & Range("E2") & vbNewLine Print #1, FileContents Close #1
- User
Please correct this code Dim ws As Worksheet Dim wss As Worksheet Set ws = Worksheets.Sheet("Job Request") Set wss = Worksheets(Range("I2").Value)
- Assistant
To correct the code, you should replace the
.Sheet("Job Request")with just the sheet name("Job Request"), like this:Dim ws As Worksheet Dim wss As Worksheet Set ws = Worksheets("Job Request") Set wss = Worksheets(Range("I2").Value)This code defines two worksheet objects
wsandwss, setswsto the sheet "Job Request", and then setswssto the sheet specified in cell I2 of the "Job Request" sheet.Make sure that the sheet name in cell I2 is spelled exactly as the name of one of the worksheets in your workbook, otherwise you may get a run-time error '9': Subscript out of range.
- User
Sub Request() Dim ws As Worksheet Dim wss As Worksheet Set ws = Worksheets("Job Request") Set wss = Worksheets(Range("I2").Value)
wss.Select
Dim lastrow As Long lastrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1 wss.Range("B" & lastrow).Value = ws.Range("B2").Value wss.Range("C" & lastrow).Value = ws.Range("F2").Value wss.Range("F" & lastrow).Value = ws.Range("C2").Value wss.Range("J" & lastrow).Value = ws.Range("E2").Value
ws.Select
Dim FilePath As String Dim FileContents As String FilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Request0.txt" Open FilePath For Append As #1 FileContents = Range("A2") & vbNewLine & Range("B2") & vbNewLine & Range("C2") & vbNewLine & Range("D2") & vbNewLine & Range("E2") & vbNewLine Print #1, FileContents Close #1
ElseIf Target.Column = 6 And Range("A2") = "" Or Range("B2") = "" Or Range("C2") = "" Or Range("D2") = "" Or Range("E2") = "" Then MsgBox "Please fill in all the required fields.", vbExclamation, "Missing Information""" Exit Sub End If End If End Sub there is an error with the elseif
- Assistant
Based on the code you have provided, it seems that there is an error in the
ElseIfstatement.Here is the corrected code for
Request()macro:Sub Request() Dim ws As Worksheet Dim wss As Worksheet If Range("A2") = "" Or Range("B2") = "" Or Range("C2") = "" Or Range("D2") = "" Or Range("E2") = "" Then MsgBox "Please fill in all the required fields.", vbExclamation, "Missing Information" Exit Sub End If Set ws = Worksheets("Job Request") Set wss = Worksheets(Range("I2").Value) wss.Select Dim lastrow As Long lastrow = wss.Cells(wss.Rows.Count, "B").End(xlUp).Row + 1 wss.Range("B" & lastrow).Value = ws.Range("B2").Value wss.Range("C" & lastrow).Value = ws.Range("F2").Value wss.Range("F" & lastrow).Value = ws.Range("C2").Value wss.Range("J" & lastrow).Value = ws.Range("E2").Value ws.Select Dim FilePath As String Dim FileContents As String FilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Request0.txt" Open FilePath For Append As #1 FileContents = Range("A2") & vbNewLine & Range("B2") & vbNewLine & Range("C2") & vbNewLine & Range("D2") & vbNewLine & Range("E2") & vbNewLine Print #1, FileContents Close #1 End Sub - User
Dim FilePath As String Dim FileContents As String FilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Request0.txt" Open FilePath For Append As #1 FileContents = Range("A2") & vbNewLine & Range("B2") & vbNewLine & Range("C2") & vbNewLine & Range("D2") & vbNewLine & Range("E2") & vbNewLine Print #1, FileContents Close #1 does not work
- Assistant
The code you have provided will not work because it appears outside of any VBA procedures (Sub or Function). You should put it into a subroutine or into an existing subroutine in order to run it.
Here is what the full subroutine should look like:
Sub WriteToFile() Dim FilePath As String Dim FileContents As String FilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Request0.txt" FileContents = Range("A2") & vb
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.