I wanted to recall a value from different worksheet that contains "-" (hyphen) in folder/file name without changing the name of the folder/the file. (want to use the file path)
Sub file_path()
Dim strFolder As String
Dim CopySheet As String
Dim po, iu As String
Dim DestinationSheet As Variant
Dim qwer As Workbook
Dim poi As Workbook
Dim por As Variant
CopySheet = InputBox("path of workbook you recall")
Set qwer = Workbooks.Open(CopySheet)
po = InputBox("sheet name")
iu = InputBox("Range")
por = qewr.Sheets(po).Range(iu).Value
ActiveCell.Value = vals
po.Close
End Sub
I tried using this code but I keep getting error when i put the value for "por" variant. How can I fix this?
Related
I am trying to figure out how to do this, any guidance would be great.
Setup: I work between 2 workbooks called Master and Daily Input
The Daily Input file contains 10 worksheets, each worksheet = 1 person's name. + 1 worksheet named "Input Template"
The Master workbook contains a bunch of different worksheets for different calculations. + 9 worksheet with the team member's names.
Assume currently there are 9 people in the team.
When new people join or leave the team, they will open / delete worksheets from the Daily Input workbook.
Therefore I want to:
New Team Member added a new worksheet scenario:
If Daily Input have a worksheet that Master does not (except Input Template), then create a new worksheet in Master with the same name. The new worksheet is copied from Output Template that is already in the Master file.
If Master have a worksheet that Daily Input does not (except a few worksheets for calculation), then just prompt a messagebox.
Currently I have written something that extracts all the sheet names from the Daily Input file, and then put it in the Master File but I am not sure how to make use of that...
Maybe load both sheet name lists into an array and compare?
Sub ObtainNameList()
Application.ScreenUpdating = False
Dim WkBk_Input As Workbook
Dim WkBk_Active As Workbook
Dim GetListFName As String
Dim GetListFPath As String
Dim FName As String
Dim FPath As String
Dim i As Integer
Set WkBk_Active = Application.ActiveWorkbook
FPath = WkBk_Active.Worksheets("Menu").Range("B1")
FName = WkBk_Active.Worksheets("Menu").Range("B2")
Set WkBk_Input = Application.Workbooks.Open(FPath & "\" & FName)
WkBk_Active.Worksheets("NameList").Range("A:A").ClearContents
For i = 1 To WkBk_Input.Sheets.Count
WkBk_Active.Worksheets("NameList").Range("A" & i).Value = WkBk_Input.Sheets(i).Name
Next i
WkBk_Input.Close
Application.ScreenUpdating = True
End Sub
This ought to work, but I'm on my phone so can't actually check it:
Sub CheckandCreate()
Dim Fpath As String
Dim Fname As String
Dim master As Workbook
Set master = ThisWorkbook 'assume running in master
Dim daily As Workbook
'set daily path and name here
Set daily = Workbooks.Open(Fpath & "\" & Fname)
Dim ws As Worksheet
For Each ws In daily.Worksheets
Select Case ws.Name
Case "Input Template" 'add ay other sheet names you want to ignore here
Case Else
If SheetNotExist(ws.Name, master) Then
AddSheet (ws.Name)
End If
End Select
Next ws
daily.Close False 'close daily without saving
End Sub
Function SheetNotExist(sheetname As String, where As Workbook) As Boolean
On Error GoTo nope
Dim ws As Worksheet
Set ws = where.Worksheets(sheetname) 'if sheet exists this will work
SheetNotExist = False
Exit Function
nope:
SheetNotExist = True 'will only get here if sheet doesn't exist
End Function
Sub AddSheet(sheetname As String)
Dim ws As Worksheet
ThisWorkbook.Worksheets("Output Template").Copy _ after:=ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count) 'copy to end of workbook
Set ws = ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count) 'new worksheet
ws.Name = sheetname
End Sub
I'm trying to set a range in VBA as the range of the inputted string that I am looking for. The full procedure is supposed to pop up a dialog box to look for a specific string, find the string, create a range called location, set this range to the range of the string that was found, move a finite amount of columns to the right, and with that new columns value, print a string into that range.
The problem right now is that for some reason It is not setting the range to the range of the string it finds.
There is only one instance of the string throughout the workbook.
I'm also relatively new to VBA so there are something commands I don't know or understand how they work.
Sub test()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim Inp As String
Dim Loc As Range
Dim Row As Integer
Dim Col As Integer
Dim NewLoc As Range
Dim Sh As Worksheet
Inp = InputBox("Scan ESD Tag")
For Each Sh In ThisWorkbook.Worksheets
With Sh.Columns("A")
Set Loc = .Find(What:=Inp)
End With
Next
Row = Loc.Row
Col = Loc.Column + (3 * 5)
Set NewLoc = Worksheets("Building 46").Cells(Row, Col)
NewLoc.Value = "Over Here"
Range("G2") = 6
End Sub
Your problem is probably that your final block should be inside the loop as otherwise Loc is likely to be Nothing (unless the term is found on the last sheet) and your code will error. You should also check first that it is found to avoid such errors.
Sub test()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim Inp As String
Dim Loc As Range
Dim Row As Integer
Dim Col As Integer
Dim NewLoc As Range
Dim Sh As Worksheet
Inp = InputBox("Scan ESD Tag")
For Each Sh In ThisWorkbook.Worksheets
With Sh.Columns("A")
Set Loc = .Find(What:=Inp)
If Not Loc Is Nothing Then
Row = Loc.Row
Col = Loc.Column + (3 * 5)
Set NewLoc = Worksheets("Building 46").Cells(Row, Col)
NewLoc.Value = "Over Here"
Range("G2") = 6 'should specify a sheet here
Exit Sub
End If
End With
Next
End Sub
HI there I am trying to put a VBA code to look for a specific string (text) I have defined in the code located in another workbook find the number of missing values text from the list I defined and produce a report how many values text are missing and if any values wrong do not match the text how many are wrong. this is some of the code I have put but I am struggling to find a code to look for the defined text and produce a report of the number of missing instrument and nr of wrong instrument: Any suggestions will be highly appreciated.
Dim mypath As String
Dim folderpath As String
Dim filename As String
Dim MyBook As Workbook, newbook As Workbook
Set MyBook = ThisWorkbook
Dim file As String, sheetdata As String
Dim ws As Worksheet
Dim x As Workbook
Dim y As Workbook
Dim FindString As String
Dim Rng As Range
Dim findrow As Integer
Dim i As Integer
Dim finalrow As Integer
Dim bottomA As Integer
Dim c As Range
Dim TESTMarketFI2015submitted As Worksheet
Dim FI As Worksheet
Dim a As Range
Dim col As Integer
Dim row As Integer
Dim cell As Integer
Dim inputbox As Variant
Dim application As String
Dim value As String
Dim bond As String
Dim promissoryNote As String
Dim loan As String
Dim certificatesOfDeposit As String
Dim embededOptionBond As String
Dim repo As String
Dim bondOption As String
Dim bondForward As String
Dim securedBond As String
Dim inflationLinkedBond As String
Set x = Workbooks.Open(filename:="Z:\Profiles\My Documents\MAPPING\TEST MAPPING TABLES\TEST_Market_FI_2015 - submitted.csv")
worksheets("TEST_Market_FI_2015 - submitted").Range("A:A").Select
For Each c In Sheets("TEST_Market_FI_2015 - submitted").Range("A:A")
Next
If value = ("bond,promissoryNote,loan,certificatesOfDeposit,embededOptionBond,repo,bondOption,bondForward,securedBond,inflationLinkedBond") Then
c.EntireRow.Copy Worksheets("TEST_Market_FI_2015 - submitted").Range("A:A" & Rows.Count).End(xlUp).Offset(1)
Worksheets("FI").Select
Range("A2").End(xlUp).Offset(i, 0).PasteSpecial
Else
MsgBox "Nothing found under instrument"
End If
End Sub
You need to put your next under the End If. When it checks the value, it does not know what it's actually checking. It should be:
For Each c In Sheets("TEST_Market_FI_2015 - submitted").Range("A:A")
If c.value = ("bond,promissoryNote,loan,certificatesOfDeposit,embededOptionBond,repo,bondOption,bondForward,securedBond,inflationLinkedBond") Then
c.EntireRow.Copy Worksheets("TEST_Market_FI_2015 - submitted").Range("A:A" & Rows.Count).End(xlUp).Offset(1)
Worksheets("FI").Select
Range("A2").End(xlUp).Offset(i, 0).PasteSpecial
Else
MsgBox "Nothing found under instrument"
End If
Next
So now what will happen is, it will loop through each cell, in your selected range and check the value of c (the cell it's presently looking at). If the value of c matches your string then it will do stuff.
However, I doubt the cell value you're looking for is truly a comma delimited string. Are you trying to search for all of those strings individually, meaning that if c.value is ANY of the words in your () then it should do stuff. If so, one option to pursue would be creating a string array, and having the c.value look in the array instead.
I'd like to replace a cell's text in case a condition is fullfilled, but the data is in another file. How can I do it?
I used something like this:
Sub test()
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
If targetSheet.Range("Q19", "BL23").Text = "OK" Then
sourceSheet.Range("GB38", "GH38").Text = "Agreed"
End If
End Sub
You need to:
test Q19 and BL23 separately
use .Value rather than .Text since .Text is read-only
EDIT#1:
Here is the reason I suggest testing the cells separately. Consider:
Sub qwerty()
MsgBox Range("C3,A1") = "X"
End Sub
On an empty worksheet, we get FalseIf we set C3 to an X we get TrueIf we clear C3 and set A1 to an X we get False!
It turns out that for a dis-joint range, only the first element is examined........the others are ignored!
here an example how you can achieve replacement by condition
'''''
If targetSheet.[Q19, BL23].text= "ok" Then
sourceSheet.Cells.Replace what:="Search string", replacement:="Replacement String"
End If
'''''
the cell's content doesn't change to "Agreed"
here your updated code, but this is not replacement as written in title
''''''
If targetSheet.[Q19, BL23].text = "OK" Then
sourceSheet.[GB38, GH38].Value = "Agreed"
End If
'''''
for verification of the multiple range content use .text, but to insert value in multiple range you need to use .value
test in screenshots below
wrong way of the multiple range verification using .value
wrong way of the multiple range verification without specified range property
right way of the multiple range verification using .text
Try adding
customerWorkbook.Close customerWorkbook.Saved = True
before your End Sub statement. That should fix it.
Got it! Thanks for the support
Sub test()
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
Dim x As String
If sourceSheet.Range("Q19").Text = "OK" Then
x = "Agreed"
End If
targetSheet.Range("GB38", "GH38") = x
End Sub
Basically I'm creating a excel app that, when run, will prompt the user to point at the a specific excel file, and it will take the location in as a string, that works fine. What I am not sure how to do is choose a range in the active worksheet and take the value in each cell and combine them into 1 string.
This is my code so far:
Option Explicit
Sub locate_file()
Dim file As String
Dim sheet1_95 As String
Dim theRange As Range
'prompt user for location of other excel sheet'
file = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")
'test input of location'
Workbooks("testing input file.xlsx").Sheets("location").Activate
Range("A1") = file
'activate the workbook and sheet'
Workbooks("95%.xlsx").Sheets("DT").Activate
'Testing retrieving cells as string'
Set theRange = Range("A2:A4")
'how do i retrieve values in this range and combine them into 1 string?'
End Sub
What I am not sure how to do is choose a range in the active worksheet
By using the Application.InputBox function (as opposed to VBA.InputBox):
dim r as range
set r = application.inputbox("Select a range", Type:=8)
and take the value in each cell and combine them into 1 string.
By looping through the cells:
dim c as range
dim s as string
for each c in r.cells
s = s & c.value
next
'Testing retrieving cells as string'
Set theRange = Range("A2:A4")
'how do i retrieve values in this range and combine them into 1 string?'
Dim c as int, x as int
Dim strValue as string
c = therange.rows.count
strValue = vbnullstring
for x = 1 to c
strValue = strValue & theRange.cells(x,1).value
next x