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
Related
Can someone help me with the following:
I have an issue with "Set ch1 ="
The file name (or rather the last few characters of its name change slightly everyday), thus I need to add wildcard (*) or something similar.
How do I do "Set ch1 =" with a file, which name changes?
Here is part of the code I have developed.
Option Explicit
Sub Open_Latest_Cheque()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Call Open_Latest_File
Dim ch1 As Workbook, AC As Workbook, temp As Workbook
Dim ch1ws As Worksheet, ACws As Worksheet
Dim ACwsLR As Long, tempName As String
**Set ch1 = Workbooks("Sum100123.csv")**
Set ch1ws = ch1.Sheets(1)
Set AC = Workbooks("Mon.xlsm")
Set ACws = AC.Sheets("Data")
Dim MyPath As String, MyFile As String, LatestFile As String
Dim LatestDate As Date, LMD As Date.............
Thanks
Try this:
Sub test_partial_name()
Dim pt_nm As Workbook
For Each pt_nm In Application.Workbooks
If (pt_nm.Name) Like "Sum1#123.csv" Then
Exit For
End If
Next pt_nm
If Not pt_nm Is Nothing Then
pt_nm.Activate
Else
MsgBox "The file has not opened!"
End If
With Sheets(1)
Cells(1, 1).Value = "its working? Yes"
End With
A way to do it is to loop through the collection of workbooks with For Each wkb in Workbooks and to check the name of every iterated workbook with Like. There, you may use *, #, ?, etc (see the screenshot). Once you find what you need assign it to the wbkSpecific and exit the collection with Exit For:
Sub TestMe()
Dim wkb As Workbook
Dim wkbSpecific As Workbook
For Each wkb In Workbooks
If wkb.Name Like "Sum1*???.csv" Then
Set wkbSpecific = wkb
Exit For
End If
Next wkb
If Not wkbSpecific Is Nothing Then
MsgBox wkbSpecific.Name
End If
End Sub
Like Operator MSDN
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
Going crazy here. I use this definition of worksheet all the time. Copied every string to avoid typing errors. Still, the code below produces "Nothing" when I try to set FR worksheet. Pls help!
Sub FindReplace()
Dim FRep As Worksheet
Dim c As Range
Dim cText As TextBox
Dim i As Integer
Set FRep = ThisWorkbook.Worksheets("FindReplace")
For i = 1 To 23
cText = FRep.Cells(i, 3).Text
FRep.Cells(i, 2).NumberFormat = "#"
FRep.Cells(i, 2).Value = cText
Next i
The code as is seems correct. Make sure that "FindReplace" worksheet is in ThisWorkbook.
Also, you can try to get "FindReplace" worksheet by CodeName instead of by the name of the sheet. The advantage is that if the user changes the name of the worksheet, the CodeName will remain the same and you won't need to update your code to the new worksheet name.
Public Function GetWsFromCodeName(codeName As String, wb As Workbook) As Worksheet
Dim ws As Worksheet
For Each ws In wb.Worksheets
If ws.CodeName = codeName Then
Set GetWsFromCodeName = ws
Exit For
End If
Next ws
End Function
Add this function in your code:
Sub FindReplace()
Dim FRep As Worksheet
Set FRep = GetWsFromCodeName("YourCodeName", ThisWorkbook)
The Worksheet object has a Paste method that has a parameter named Link. If this is set to be True a link is created into the Range selected by the cursor, to the Range currently on the clipboard (aka. that is sorrounded by marching ants). The resulting link in the cells will look like:
='\\DES001\Home folder\[the book.xlsx]sheet1'!$A$1
Now, is there a function that returns this string itself? (without the = sign)
You can read it into a string. Not sure how you have your link build with address and subaddress but you can do something like this.
My link is in cell C3. You will have to change it to whatever cell your link is in.
Private Sub CommandButton36_Click()
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("sheet1")
Dim s As String
s = ws.Range("C3").Hyperlinks(1).Address
'or
s = ws.Range("C3").Hyperlinks(1).SubAddress
MsgBox (s)
End Sub
I have used sample Range A2. Modify according to your requirement
Sub TestLink()
Dim ws As Excel.Worksheet
Dim sString As String
Set ws = ActiveWorkbook.Sheets("sheet1")
ws.Hyperlinks.Add Anchor:=ws.Range("A2"), _
Address:="http:\\www.google.com\", _
SubAddress:="google.com", _
TextToDisplay:="Link"
sString = ws.Range("A2").Value
MsgBox sString
End Sub
I want to import data from multiple workbooks, all from the same sheet index (3).
I'm new to vba, and I figured out how to open multiple files up, and also to copy data from one sheet to another sheet in a different workbook for a single file, but I can't seem to figure out how to do that for multiple files.
I highlighted where the error is, it tells me "object doesn't support this property or method"
Could you please help?
Thanks
Sub dataimport()
' Set Vars
Dim ArbinBook As Workbook, DataBook As Workbook
Dim i As Integer, j As Integer
Dim Caption As String
Dim ArbinFile As Variant, DataFile As Variant
' make weak assumption that active workbook is the target
Set DataBook = Application.ActiveWorkbook
' get Arbin workbook
Caption = "Please select an input file"
' To set open destination:
' ChDrive ("E")
' ChDir ("E:\Chapters\chap14")
' With Application
'Set "arbinfile" as variant, the "true" at end makes it into an array
ArbinFile = Application.GetOpenFilename(, , Caption, , True)
'Exit when canceled
If Not IsArray(ArbinFile) Then
MsgBox "No file was selected."
Exit Sub
End If
Dim targetSheet As Worksheet
Set targetSheet = DataBook.Sheets(1)
'Open for every integer i selected in the array "arbinfile"
For i = LBound(ArbinFile) To UBound(ArbinFile)
Set ArbinBook = Workbooks.Open(ArbinFile(i))
targetSheet.Range("A2", "G150").Value = ArbinBook.Sheets(3).Range("A2", "G150").Value
**ERROR at the line above**
Workbooks(DataSheet).Activate 'Reactivate the data book
Worksheets(1).Activate 'Reactivate the data sheet
ActiveWorkbook.Sheets(1).Copy _
after:=ActiveWorkbook.Sheets(1)
Workbooks(ArbinFile(1)).Activate 'Reactivate the arbin book(i)
ArbinBook.Close
Next i
Beep
End Sub
My instinct tells me that ArbinBook.Sheets(3) is a Chart-sheet, not a WorkSheet (or, at least, it is something other than a WorkSheet). It might be hidden as well, but it will still be indexed as (3).
If so, change Sheets(3) to Worksheets(3).
Added: BTW If true, this also demonstrates why using index-numbers is unreliable. If at all possible, refer to a worksheet by its name. (I appreciate that this may not always be possible.)
Added (from comments) There is nothing named DataSheet in your code. Add Option Explicit to the top of your module to indicate all such errors.
Try changing the line Set ArbinBook = Workbooks.Open(ArbinFile(i))
to Set ArbinBook = Workbooks(arbinfile(i))
I could be wrong, but I think it's trying to set your workbook object to become the action of opening another workbook, instead of labeling it as the workboook.
Sub Multiple()
Application.DisplayAlerts = False
Application.EnableEvents = False
Dim exlApp As Excel.Application
Dim exlWb1 As Excel.Workbook
Dim exlWb2 As Excel.Workbook
Dim exlWb3 As Excel.Workbook
Dim exlWs1 As Excel.Worksheet
Dim exlWs2 As Excel.Worksheet
Dim exlWs3 As Excel.Worksheet
Set exlApp = CreateObject("Excel.Application")
Set exlWb1 = exlApp.Workbooks.Open("C:\yourpath1\file1.xls")
Set exlWb2 = exlApp.Workbooks.Open("C:\yourpath2\file2.xls")
Set exlWb3 = exlApp.Workbooks.Open("C:\yourpath3\file3.xls")
Set exlWs1 = exlWb.Sheets("Sheet1")
Set exlWs2 = exlWb.Sheets("Sheet1")
Set exlWs3 = exlWb.Sheets("Sheet1")
exlWb1.Activate
exlWb2.Activate
exlWb3.Activate
'code
exlWb.Close savechanges:=True
exlWb.Close savechanges:=True
exlWb.Close savechanges:=True
Set exlWs1 = Nothing
Set exlWs2 = Nothing
Set exlWs3 = Nothing
Set exlWb1 = Nothing
Set exlWb2 = Nothing
Set exlWb3 = Nothing
exlApp.Quit
Set exlApp = Nothing
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub